From Arduino to R (matlab,mathematica etc)

Some days ago I bought my Arduino!!!! I have never had any contact with electronics before, so it was a bit amazing when I opened the box (I felt like a child opens Christmas present)!! The platform is  Arduino UNO R3! Writing and uploading the firmware to Arduino is very simple. What I had in mind when buying the microcontroller was not making robotics and things like these but completing some projects that had to do with data collection and R usage to analyze them.

Until now I have bought some resistors, some LEDs and an LDR photoresistor to become familiar to the platform.

In order to start doing some work with the Arduino I connected the LDR to the board using the Voltage Divider circuit.

The code I used was from this guy http://bildr.org/2012/11/photoresistor-arduino/ modified a little bit in order to get two columns of data. The first column is milliseconds and the second is luminosity as it is expressed from the analog port of arduino*.

[code language=”C”]
int LDR_Pin = A0; //analog pin 0

void setup(){

Serial.begin(9600);

}

void loop(){

int LDRReading = analogRead(LDR_Pin);

Serial.print(millis());

Serial.print(",");

Serial.println(LDRReading);

delay(1000); //1sec to slow down the output for easier reading

}
[/code]

*Arduino analog port gets values from 0 to 1023. 0 = no voltage, 1023 is 5V. So, approximately 512=2.5V etc. If you have the manual of the sensor you can then use it in order to get the values you want, because knowing the output voltage then you can know how the resistor of the sensor is changed. The sensor I am using didn’t come with a manual or a URL I could find one. If I would like to calibrate it, I should have a photometer, but here is not my point to show how calibration could be done.

How to log the data from Arduino’s monitor

After some research I found some sites that explain how to log data to your pc from the microcontroller but they require some lines of coding and at this point I am not so much interested in making all the things automated.

I found though a program that does the job I want. It is called CoolTerm and it is also written from a guy called Roger Meier, who is not a professional programmer, but he does a really good job. This program lets you record all the data that are coming out from your controller, while Arduino is connected to the USB.

First step: Open CoolTerm program while ARDUINO is connected to the USB and running

Second step: Press options and press select the port where the microcontroller is connected. 0

Third step: Press connect

2

Fourth step: Press Connections->Capture to text file->Start to start logging the data from Arduino.

1

Importing data to R

For a person who is familiar to R, this is the easy part. You import the data in R and then plotting them. I wrote a little script which does the job a bit automated, but it works only for a two column data file (it also need some modification if there is no comma separating your input data, but it shows the way how to do the job). The script is the following:

[code language=”r”]
fa2r<-function(){

indata<-readLines(file.choose())

indatanew<-sub(","," ",indata) ##change comma to gap (if theres a comma)

data_final<-read.table(textConnection(indatanew))

##textConnection to split the table where where the gap is

col1<-readline("Name the first column (x axis): ")

col2<-readline("Name the second column (y axis): ")

names(data_final)[1]<-col1

names(data_final)[2]<-col2

ploted_data<-ggplot(data=data_final,aes_string(x=data_final[1], y=data_final[2]))+

xlab(col1)+ylab(col2)+

geom_point()+geom_line()

plotname<-readline("Name to export the plot: ")

ggsave(ploted_data,filename=paste(plotname,".png",sep=""),scale=2.5)

}
[/code]

The output plot is the following:
dat
PS:This is just an easy way. There is a huge amount of information how to use other methods too, like Processing in combination with Rserve.

Comments

3 responses to “From Arduino to R (matlab,mathematica etc)”

  1. Antonio S. Chinchón

    Wow! You opened Pandora’s box! Congratulation! What’s next?

  2. I want to make a prototype air qyality station with arduino, a thermometer, a humidity sensor and a dust sensor (and maybe a CO detector). I am waiting for the sensor to be delivered to start the project. And who knows what’s next. (at the same time I am also searching for work 😉 )

  3. […] my Arduino to see if PLX-DAQ works. I used the same circuit as described in my previous article From Arduino to R (matlab,mathematica etc). The code is the following. […]

Leave a Reply

Your email address will not be published. Required fields are marked *