Arduino/Clodiuno – command a LED from the clojure REPL

Share

Now that the setup hell is gone, here i will describe “my first interaction” to make my arduino uno talk with a simple led.

Source: https://github.com/ardumont/arduino-lab

Circuit

First we create a simple circuit.

Plug your LED on the pin 13 of the arduino board.

Beware, the LED is polarized, so

  • the longer branch (+) goes into the 13 pin
  • and the other one goes inside the GND just beside.

No Led If you have no LED, it’s no big deal as there is already one in it.

Pre-requisites

Plug your arduino board in your usb

Upload the firmata sketch

Open up the arduino IDE and load the appropriate Firmata sketch from File > Examples. I used StandardFirmata22forUNO03 because I have an Uno board, but your version may vary.

In Ubuntu GNU/Linux

Make sure you have correctly linked your device with an appropriate name

tony@dagobah(0.44,) 18:06:22 ~/org/clodiuno (master) $ ls -l /dev/ttyS42
lrwxrwxrwx 1 root root 12 2012-06-03 15:34 /dev/ttyS42 -> /dev/ttyACM0

Else it will not be seen.

Setup

Here is my project’s setup:

(defproject arduino-lab "1.0.0-SNAPSHOT"
  :description "Mess around with arduino from the comfort of the repl"
  :dependencies [[org.clojure/clojure "1.3.0"]
                 [clodiuno "0.0.3-SNAPSHOT"]
                 [serial-port "1.1.2"]]
  :native-dependencies [[org.clojars.samaaron/rxtx "2.2.0.1"]]
  :dev-dependencies [[native-deps "1.0.5"]]
  :jvm-opts ["-Djava.library.path=./native/linux/x86/"
             "-d32"])

Fetch the dependencies:

lein deps

Start emacs and start the repl:

M-x clojure-jack-in

Now some reality

Now i can do light up and down a led on my arduino uno from the repl!

Open the repl:

user> (use 'clodiuno.core 'clodiuno.firmata)
nil

Open the board:

user> (def board (arduino :firmata "/dev/ttyS42"))
#'user/board

Now you can see something along the line:

user> board
#<Ref@9652e4: {:digital-in {2 (0 0 0 0 0 0 0 0), 1 (0 0 0 0 0 0 0 0), 0 (0 0 0 0 0 0 0 0)}, :version [2 2], :port #<RXTXPort /dev/ttyS42>, :interface :firmata}>

We must first set the built-in 13 pin (the led) to OUTPUT mode.

user> (pin-mode board 13 OUTPUT)
nil

Then play with the board, may the light be

user> (digital-write board 13 HIGH)
nil

… or not:

user> (digital-write board 13 LOW)
nil

Result