Showing posts with label read serial. Show all posts
Showing posts with label read serial. Show all posts
Reading Arduino data directly into R
I have experimented with reading an Arduino signal into R in the past, using Rserve and Processing. Actually, it is much easier. I can read the output of my Arduino directly into R with the
Here is my temperature sensor example again:
And all it needs to read the signal into the R console with my computer is:
Note: This worked for me on my Mac and I am sure it will work in a very similar way on a Linux box as well, but I am not so sure about Windows. Crucially, I had to learn the difference between the tty* and cu* devices. I found the following statement in Mike's PBX Cookbook particular insightful:
With a little more R code I can create a 'live' data stream plot of my Arduino.
R code
Here is the original Arduino sketch as well:
scan
function. Here is my temperature sensor example again:
And all it needs to read the signal into the R console with my computer is:
> f <- file("/dev/cu.usbmodem3a21", open="r")
> scan(f, n=1)
Read 1 item
[1] 20.8
> close(f)
Super simple: Open the file connection. Scan n lines of data. Close the file connection. Job done.Note: This worked for me on my Mac and I am sure it will work in a very similar way on a Linux box as well, but I am not so sure about Windows. Crucially, I had to learn the difference between the tty* and cu* devices. I found the following statement in Mike's PBX Cookbook particular insightful:
You might notice that each serial device shows up twice in /dev, once as a tty.* and once as a cu.*. So, what's the difference? Well, TTY devices are for calling into UNIX systems, whereas CU (Call-Up) devices are for calling out from them (eg, modems). We want to call-out from our Mac, so /dev/cu.* is the correct device to use.You find the file address of your Arduino by opening the Arduino software and looking it up under the menu Tools > Port.
With a little more R code I can create a 'live' data stream plot of my Arduino.
![]() |
Reload this page to see the animated Gif again. |
R code
Here is the original Arduino sketch as well:
Session Info
R version 3.1.2 (2014-10-31)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods
[7] base
loaded via a namespace (and not attached):
[1] tools_3.1.
17 Feb 2015
07:27
Arduino
,
R
,
read serial
,
scan
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.
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
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.
2 Oct 2012
07:48
Arduino
,
Interface
,
Processing
,
R
,
read serial
,
Rserve
,
Tutorials