Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created July 14, 2016 17:28
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save jalapic/2193571b319d6f452fa7faef696c46be to your computer and use it in GitHub Desktop.
Save jalapic/2193571b319d6f452fa7faef696c46be to your computer and use it in GitHub Desktop.
Arduino data to R
library(serial)
rm(list = ls()) # clear environment
graphics.off() # close all graphic windows
### establish a serial connection
con <- serialConnection(name = "get_temps",
port = "COM5",
mode = "115200,n,8,1",
buffering = "none",
newline = 1,
translation = "cr")
#close(con)
isOpen(con)
open(con)
isOpen(con)
### grab data from serial port
read.serialConnection(con)
#Grab last five characters and turn into number
substrRight <- function(x, n){ substr(x, nchar(x)-n+1, nchar(x)) }
tmp <- read.serialConnection(con)
tmp
substrRight(tmp,5)
as.numeric(substrRight(tmp,5))
as.numeric(substrRight(read.serialConnection(con),5))
#### Single Non-Moving Plot
x11()
N <- 1000
x <- rep(NA,N)
for(i in seq(N)) {
Sys.sleep(0.101)
tmp <- read.serialConnection(con)
x[i] <- as.numeric(substrRight(tmp,5))
plot(1:N, x, type="l", ylim=c(22, 28),lwd=2,
main="Temperature from Arduino", xlab="Seconds*10",
ylab="Temperature - Celsius")
}
#### Scrolling Plot
x11()
N <- 1000
x <- rep(NA,N)
for(i in seq(N)) {
Sys.sleep(0.101)
tmp <- read.serialConnection(con)
x[i] <- as.numeric(substrRight(tmp,5))
if(i<=200){
plot(1:200, x[1:200], type="l", ylim=c(22, 28),lwd=2,
main="Temperature from Arduino", xlab="Seconds*10",
ylab="Temperature - Celsius")
} else
if(i>200){
plot((i-200):i, x[(i-200):i], type="l", ylim=c(22, 28),lwd=2,
main="Temperature from Arduino", xlab="Seconds*10",
ylab="Temperature - Celsius")
}
}
/* ---------------------------------------------------------
* | Arduino Experimentation Kit Example Code |
* | CIRC-10 .: Temperature :. (TMP36 Temperature Sensor) |
* ---------------------------------------------------------
*
* A simple program to output the current temperature to the IDE's debug window
*
* For more details on this circuit: http://tinyurl.com/c89tvd
*/
//TMP36 Pin Variables
int temperaturePin = 0; //the analog pin the TMP36's Vout (sense) pin is connected to
//the resolution is 10 mV / degree centigrade
//(500 mV offset) to make negative temperatures an option
/*
* setup() - this function runs once when you turn your Arduino on
* We initialize the serial connection with the computer
*/
void setup()
{
Serial.begin(115200); //Start the serial connection with the copmuter
//to view the result open the serial monitor
//last button beneath the file bar (looks like a box with an antenae)
}
void loop() // run over and over again
{
float temperature = getVoltage(temperaturePin); //getting the voltage reading from the temperature sensor
temperature = (temperature - .5) * 100; //converting from 10 mv per degree wit 500 mV offset
//to degrees ((volatge - 500mV) times 100)
Serial.println(temperature); //printing the result
delay(100); //waiting a tenth of a second
}
/*
* getVoltage() - returns the voltage on the analog input defined by
* pin
*/
float getVoltage(int pin){
return (analogRead(pin) * .004882814); //converting from a 0 to 1023 digital range
// to 0 to 5 volts (each 1 reading equals ~ 5 millivolts
}
@cguilherme00
Copy link

Great code, works great for acquisition of the data. But my plot is just empty!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment