Connecting the real world to R with an Arduino

If connecting data to the real world is the next sexy job, then how do I do this? And how do I connect the real world to R?

It can be done as Matt Shottwell showed with his home made ECG and a patched version of R at useR! 2011. However, there are other options as well and here I will use an Arduino. The Arduino is an open-source electronics prototyping platform. It has been around for a number of years and is very popular with hardware hackers. So, I had to have a go at the Arduino as well.

My Arduino starter kit from oomlout

The example I will present here is silly - it doesn't do anything meaningful and yet I believe it shows the core building blocks for future projects: Read an analog signal into the computer via the Arduino, transform it with R through Rserve and display it graphically in real time. The video below demonstrates the final result. As I turn the potentiometer random points are displayed on the screen, with the standard deviation set by the analog output (A0) of the Arduino and fed into the rnorm function in R, while at the same time the LED brightness changes.

I don't claim to be an expert in any of this (far from it), but at the end my experiment worked. I am happy to share my experience, as I have benefited from reading various blog posts as well. I can't guarantee that any of this will not harm your hard- or software. Hence, I used an old computer. Of course feedback will be much appreciated if gently delivered.

Here is a list of hardware and software I used and 10 steps to recreate the experiment:


  • Arduino (Uno)
  • Computer (2003 iBook G4, MacOSX 10.4.11)
  • USB cable
  • Potentiometer (10k Ohm)
  • Resistor (560 Ohm)
  • LED
  • Wire
  • Bread-board


Step 1

Install Arduino, Processing and R on your computer. I have used my old iBook G4, but the software is available for most platforms, including Windows and Linux.

Step 2

Assemble the hardware with the potentiometer, resistor and LED on a bread-board next to the Arduino. This setup is basically example 8 of my oomlout starter kit, with the difference that I connected the LED to Pin 9 instead of Pin 13. You find a detailed description, including video and code on their site.

Image of my setup created with Fritzing

Step 3

Connect the Arduino with your computer via USB and start the Arduino software. You can reset the Arduino with a little sketch, if you want to:
void setup(){}
void loop(){}
Copy and paste the code below into a new Sketch and hit the upload button. The LED should light up and as you turn the potentiometer the brightness should change. While you upload the file to the Arduino notice the connection path of your device in the bottom of the console window, mine said: Arduino Uno on /dev/cu.usbmodem3B11.

Open the Serial Monitor in the Arduino software (Tools > Serial Monitor) and observe how the displayed numbers change from 0 to 1023 as you turn the potentiometer.

Step 4

Close the Arduino software and start Processing. You should get a similar window and IDE as with the Arduino software. Open the preference settings and find out where Processing stores the Sketches by default; mine are saved here: /Users/Markus/Documents/Processing.
To make Processing talk to the Arduino it needs the Arduino library for Processing. Download the zip-file and extract it. Expanding the archive should result in a folder called arduino. If it doesn't exist already, create the folder libraries in the default Processing Sketch folder, and place in there the arduino folder. Restart Processing, click on File > Examples ... and a little window should list the new arduino library at the bottom.

Step 5

Check the port number of the Arduino in Processing: The following little Processing sketch will list the serial ports in the console. Match the device paths listed against the one you noted in Step 3 and keep a record of the port number of the Arduino device path.

Step 6

Test that you can read and write to the Arduino from Processing with the Sektch below. Change mySerialPort to the device number you identified in the previous step. Run the Processing code and you should see the potentiometer values displayed in the console window below your code. If you get any error messages regarding the RXTX library (most likely on a Mac) then check out the post by Ellen Sundh.

Step 7

Open R and install the Rserve package, e.g. via the command install.packages("Rserve"). Start R, load the Rserve package and start the daemon, e.g.
or better via the command line:
R CMD Rserve

Step 9

Download the files REngine.jar and RServeEngine.jar from the Rserve page into a temporary folder. Those files are needed to connect Processing to R. More information around how to call R from Processing has the following post on the GuruBlog.

Step 10

Back to Processing. Create a new sketch in Processing, insert the code below and save the file. Add the two jar-files you downloaded in the previous step to your new sketch in Processing via Sketch > Add to File ...

Fingers crossed. Hit the run bottom and a little window should come up, plotting random points from left to right and as you turn the potentiometer the vertical range of the plotted points should change as well.

Update 2012-11-14

My friend Jean-Robert wrote a great blog post about how to use the Arduino with R via rJava.


BioStatMatt said...

Nice work! And thanks for the mention.

Tom Hiatt said...

This is awesome. Someday I want to try something like this. How much time did this whole project take for you?

Frank Sit said...

Yeah It's good I
appreciate this article. I recommand it to my friends,I,ll be back here again
and again . Visit our
for more .

Post a Comment