Raspberry Pi, Arduino and Firmata

From Middlesex Racket
Jump to: navigation, search

Our aim is to talk to an Arduino board from a Raspberry Pi using Firmata. First of all you need to upload the Firmata protocol to your board. This is very easy, follow the instructions available here for the Arduino Uno board:http://firmata.org/wiki/Main_Page#Arduino_Uno.

If you want, you can check that everything is OK by trying a simple Python program to connect to the Arduino board. Install the following packages on your Raspberry Pi (if they are not already there):

 # apt-get install arduino
 # apt-get install python-serial
 # apt-get install python-pip
 # pip install pyfirmata

Connect a led to pin 9 and create a file test.py with the following code:

 import pyfirmata
 PORT = '/dev/ttyACM0'
 board = pyfirmata.Arduino(PORT)
 while 1:

You need to make sure that the user running this code can write to /dev/ttyACM0 (add the user to the tty group if this is not the case, or run the code as root). Run the code with:

 $ python test.py

If everything is OK, your led should blink (1 second delay) and you are now ready to move to the next step: use Firmata from Racket

Firmata and Racket

Assuming you have a working copy of Racket on the Raspberry Pi (this is easy to install), download the following modified version of firmata: firmata.rkt modified code This version works under Linux and is slightly different from Bob's version (I had to change stty -f to stty -F). Place this file in some directory, and in the same directory create a file test.rkt with the following content:

#lang racket

;; (require (planet xtofs/firmata:1:0/firmata))
(require "firmata.rkt")
(open-firmata "/dev/ttyACM0")

(define (GoTo)
  (set-pin-mode! 9 OUTPUT_MODE)
  (set-arduino-pin! 9)
  (sleep 1)
  (clear-arduino-pin! 9) 
  (sleep 1)


Run it with $ racket test.rkt and you should see your led blinking every second.

Useful links

Personal tools