Reading and decoding Temperature Data from Arduino¶
(This is Part 2 of the Measuring Temperature Lab ).¶
** For this lab you will need pyserial. This can be installed thru Anaconda the same way we did Basemap. Try either of the following options for installation. **
# Use an R kernel from jupyterlab
install.packages("serial")
Some Cliff Notes on Dataframes¶
This exercise uses dataframes. Base R generates data frames by default. At the time of creation, it is possible to specify the name and data type of each column in the dataframe.
# time is stored as a string, while resitance and temperature are numeric
dd <- data.frame(time = character(), resistance = numeric(), Temp = numeric())
# Load the libraries
# install.packages("serial")
library("serial")
library("lubridate")
Open the serial connection using the serialConnection() command.
You will need to find the exact port name on your computer. Hint: This can be found in the Arduino Tools menu under "port".
# Assign the serial object that you will write to and read from
#S <- serialConnection(port = "cu.usbmodem1101",mode = "9600,n,8,1")
#open(S)
Warning message in open.serialConnection(S): “cu.usbmodem1101 is already open and will be reconfigured!”
Port cu.usbmodem1101 ( 9600,n,8,1 ) is open. Buffer usage (in/out): 9/0
Warning message in print.serialConnection(S): “Try summary() for more information!”
Initialize the three variables you will be using to store (time, resistance, temperature).
# Initialize dataframe to capture time, resistence, and Temp, as shown in datetime notes.
Set the coefficients for the Steinhart-Hart equation
##1/T = A + B*Ln(R) + C(Ln(R))^3
A = 0.001125308852122
B = 0.000234711863267
C = 0.000000085663516
# Set a while loop or for loop. You can make it go indefinitely until you kill the loop with Kernel -> Interrupt.
#lp = 0
#stop <- 'go'
# Use a for or while loop. You can make it go indefinitely until you kill the loop with Kernel -> Interrupt.
#while (stop == 'go'){
# Use the write.serialConnection() command to send a data request to the Arduino.
# Sys.sleep(2) # Add an N-second pause to allow the buffer to be filled.
# Read a line from the Serial data object stored in 'S'. Note, readline() line returns a byte array.
# You can convert to a string using decode('utf-8'). You may also want to investigate the .strip() function
# that will remove unwanted characters.
a <- read.serialConnection(S)
# Parse the string. In R, it will be easiest to just send the numeric value of resistance without any
# other text characters.
# Use an 'if statement' if necessary and append the new resistance value to the array 'R' that was initialized
# above.
# Compute Temperature using A, B, C coefficients and the resistance value.
# Convert Temp in deg Kelvin to deg C.
# Record the date and time using the datetime package.
# Append the datetime, resistance, and temperature as a new row in your Pandas Dataframe.
if (a != "") {
tryCatch(
{
# Get the time stamp for now
# Convert the resistance reading from the serial connnection into a numeric value
# Compute temperature from resistance and the Steinhar-Hart coefficients.
# Append a new row with current time and iterator value with rbind
},
error = function(e) {
print("incomplete string")
}
)
}
# Save the data to a csv using the pandas function df.to_csv() where df is the name of your dataframe.
# You can use a conditional statement to do this every N'th time, e.g. every 100th time.
write.csv(dd, "my_data.csv")
}
close(S)
Completing your data collection¶
- Close the connection to the Serial data object. (THIS IS REALLY IMPORTANT)!!!
- Make a plot of Temperature vs. time.
**Make a plot of Temperature vs. datetime. Save that plot using plt.savefig() **
What to turn in:¶
See instructions in Thermistor Lab Part 2.