Raspberry Pi plus Arduino

raspberry_pi_plus_arduinoThere are many projects for which Raspberry Pi is a good choice, and there are many projects for which Arduino is a good choice, but you don’t have to pick one or the other; sometimes, the best solution is to use both. One simple way to get started is to load Firmata onto the Arduino and control it from Python scripts running on the Raspberry Pi. I followed the instructions for the Firmata tutorial in The MagPi, Issue 7, but made a couple of minor changes including how to get some of the required software (the article’s instructions are no longer the preferred method) and what pins to use (since the article has you breadboard your own hardware and I had suitable pre-assembled hardware already on hand).

To get the Arduino environment (including GCC, AVR Tools, and Java for the Arduino IDE) onto your Raspberry Pi, it’s as easy as:

$ sudo apt-get install arduino

Use the Arduino IDE to pick your board type from Tools->Board and install the sketch under File->Examples->Firmata->StandardFirmata. Once that is done, you can exit the Arduino IDE because there’s no more Arduino programming to do. The Firmata sketch knows how to listen for and respond to requests over the USB serial interface and does not require programming changes as you develop your application on the Raspberry Pi host.

The glue between the Arduino running Firmata and the Python scripts you’ll write is pyFirmata, which takes a couple of steps to set up:

$ sudo apt-get install python-serial
$ sudo pip install pyfirmata

For my example, rather than build my own hardware from scratch on a breadboard, I used a DangerShield, a fun Arduino shield Zach ‘Hoeken’ Smith (of MakerBot International fame) sold a few years back. It provides three resistive linear sliders with LEDs in the handles, three push-buttons, a 7-segment LED display, two plain LEDs, a piezo buzzer, a “knock sensor”, an LM35 temperature sensor, a power LED, and reset button all on one board.  The sample code with The MagPi tutorial only needed the LM35 and a single LED and a breadboard out of all of that, but the extra hardware on the DangerShield will be great for exploring bigger and better Python scripts without having to add more parts.

Danger Shield on Arduino running Firmata Sketch
Tk window from PyFirmataTk demo

The Python script example in the article uses Tk to open a simple window containing an input slider and space to display some output text. The script reads the slider and sends the current position as part of a Firmata-formatted command to write that value to a pin (D3 in this case, set by board.get_pin('d:3:p') ). This has the same effect as a native Arduino sketch calling AnalogWrite() on the same pin. Setting the on-screen slider to 11, as shown in the screen shot, ultimately results in Firmata setting the Pulse Width Modulation (PWM) value to 11%, lighting LED1 to 11% of its maximum brightness. Additionally, twice a second (determined by the Python script which has code to wait 500ms between checks), the script requests Firmata to return the value of analog pin A0 (set by board.get_pin('a:0:i')), which gets scaled and displayed in the Tk window as degrees Celsius (the LM34 is the part you want if you want to measure degrees Fahrenheit).

Based on my experiments so far, I look forward to testing how quickly I can send commands from the Raspberry Pi to the Arduino in the hopes that it’s fast enough to send motor forward/backward events from Python code to try to implement some the algorithms from my homework from the Udacity class “CS373: Build a Self-Driving Car” I took last year (now apparently titled “Artificial Intelligence for Robotics”).

Advertisements

2 responses to “Raspberry Pi plus Arduino

  1. Pingback: Raspberry Jam #2 | Ethan's Makerblog·

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s