Hey! This article is really old! The code might still work, but I’ve updated the installation instructions for Processing 2.1 and Sun Oracle Java here: Processing 2.1 + Oracle Java + Raspberry Pi + Serial + Arduino = ☺.
This might not look like much, but it was a lot of work to get here. It’s the display from a small Processing sketch, running on a Raspberry Pi, talking to an Arduino controlling the brightness of an LED with the slider, and reading from an LM35 temperature sensor.
I wanted to see if I could get graphical control of an Arduino on the Raspberry Pi. I wrote about the simplest sketch in Processing that combined output (to control a small green LED through a resistor) and input (from an LM35, that simplest of sensors). This is how it looks running on a slightly faster machine than the Raspberry Pi:
LED at half brightness, LM35 showing 25°C
LED off, sensor at 26°C
I had the same results on the Raspberry Pi; just much, much slower. The sketch is below the fold.
Running Processing on Raspberry Pi
Processing is both written in and generates Java, so there’s some hope that it can run on most platforms. Up-to-date installation instructions. These instructions are modified from Processing sur Raspberry Pi, for which thanks are given to the original author:
Install the JDK and Java serial library:sudo apt-get install librxtx-java openjdk-6-jdk
Download the Linux version of Processing, and unpack it to somewhere permanent in your home directoryDelete the java folder in the Processing directory; for me, that wasprocessing-1.5.1/java
Replace that java folder with a link to your system’s installation:ln -s /usr/lib/jvm/java-6-openjdk-armhf java
In the Processing folder, remove or renamemodes/java/libraries/serial/library/linux32/librxtxSerial.so
; it’s an x86 binary, and will failIn the Processing folder, also removemodes/java/libraries/serial/library/RXTXcomm.jar
, and replace it with a copy of/usr/share/java/RXTXcomm.jar
(If you don’t do this, you’ll get a warning: “WARNING:Â RXTX Version mismatch”, and any serial comms will fail.)- Download and install the controlP5 library for Processing
- Download and install the Arduino library for Processing
- Program your Arduino with Firmata; the version that comes with the Arduino software is
finea bit old. - Connect your Arduino to the Raspberry Pi with a USB cable; it may require external power.
Now fire up Processing. It will used to take a while to start up, and will throw the following warning:
Despite this, it should eventually should start up fine:
Now, this is slow. It takes tens of seconds to start up. It might not be the most practical development tool, but Processing sketches are very portable, so you can develop on one machine, and then run on the Raspberry Pi.
The code at the end of this article expects:
- an Arduino running the Firmata DAQ sketch attached to a USB port;
- a small LED connected from digital pin 3, through a 1kΩ resistor to ground;
- an LM35 with the following connections: +Vs → +5V, Vout → analogue pin 0, and GND → GND.
If you run this, after about half a minute, the blank sketch window appears, and about half a minute later, the slider and temperature reading appears. If it doesn’t, there’s a good chance that the serial libraries are wrong. Try this sketch:
import processing.serial.*; import cc.arduino.*; Arduino arduino; println(Arduino.list());
This should return a number and a serial port where the Arduino was found; something like ‘[0] /dev/ttyACM0’.
What I really want to do now is get this same hardware running with Python and tkinter. It’s not that Python’s my favourite language; it’s just that the Raspberry Pi Foundation chose Python as the official language for the board. I’d rather work to further the aims of this educational foundation rather than work against it. Processing’s pretty much unworkably slow on the Raspberry Pi — but it does work!
Here’s the example code:
// arduino_firmata_io_example // control an LED's brightness on pin 3 // read an LM35 on analogue 0 // scruss - 2012-08-12 import processing.serial.*; import cc.arduino.*; import controlP5.*; ControlP5 controlP5; Arduino arduino; int ledPin = 3; // digital 3; PWM capable int lm35pin = 0; // analogue 0 int brightness=128; // LED brightness, 0-255 int lastbrightness=0; // store brightness to stop flicker void setup() { size(250, 50); controlP5 = new ControlP5(this); // horizontal controlP5 slider bound to brightness variable controlP5.addSlider("brightness",0,255,128,90,12,128,16); // set up first Arduino we find arduino = new Arduino(this, Arduino.list()[0]); arduino.pinMode(ledPin, Arduino.OUTPUT); } void draw() { background(240); // blank screen fill(0,0,0); // black text text("Brightness:", 15, 25); // slider label // read LM35 and print text("Temperature: " + str(int(read_lm35(lm35pin))) + " °C", 15, 48); if (brightness != lastbrightness) { // LED flickers if we keep doing analogWrite() with same value // so only write if slider brightness value has changed arduino.analogWrite(ledPin, brightness); } lastbrightness=brightness; } float read_lm35(int pin) { // read lm35 on pin, and return float °C // assumes 5V supply, and 0-100°C range return (5.0 * arduino.analogRead(pin) * 100.0)/1024.0; }
Leave a Reply