I wired a temperature sensor to my Arduino as part of the third project in the Arduino Projects Book. The project has the Arduino send the data to the serial port where the Arduino program (IDE) can show it. This data would be most useful to me, however, if it could be logged in a plain text file for data analysis. However, it’s a little annoying that there isn’t an easy way to save the data output by the Arduino.
So, I needed to use the terminal program “cat” to look at what was coming across the serial port. First I had to look up which port the Arduino was connected to in the Tools menu of the Arduino IDE: since I’m on a Mac, running OSX, this turned out to be something like /dev/cu.usbmodem411. (I could have looked this up by listing the contents of the /dev/ directory and then looking for anything connected to the usbmodem). To see the data I used:
> cat /dev/cu.usbmodem411
To actually save the data I directed the output of the cat command to the file data-log.txt:
> cat /dev/cu.usbmodem411 > data-log.txt
The contents of the file looked like this:
T25.68 6003,25.68 7504,25.68 Time (milliseconds), Temperature (deg. C) 0,25.20 1501,25.68 3002,25.68 4502,25.68 6003,26.66 7504,28.12 9006,30.08 10335,32.03 11663,33.98 12992,35.45 14321,36.91 15650,38.38 16979,39.84 18309,41.31 19559,42.77 20810,43.75 22062,42.77 23313,41.31 24563,40.82 25815,39.36 27144,38.87 28473,37.89 29802,37.40 31131,36.91 32459,35.94 33788,35.45 35118,35.45 36447,34.96 37776,34.47 39105,33.98 40434,33.98 41763,33.50 43091,33.01 44421,33.01 45750,33.01 47079,32.52 48408,32.52 49737,32.52 51066,32.03 52396,31.54 53724,31.54
I’m not sure what’s the deal with the first three lines, but if you ignore them you see the column headers (Time and Temperature) and then the time (in milliseconds) and temperature data) in a nice comma delimited format.
The Arduino program (sketch) that produced this output was:
const int T_sensor_Pin = A0; const float baselineTemp = 20.0; const int outPin = 4; void setup(){ Serial.begin(9600); pinMode(outPin, OUTPUT); Serial.println("Time (milliseconds), Temperature (deg. C) "); } void loop(){ int T_sensor_Val = analogRead(T_sensor_Pin); float T_sensor_Voltage = (T_sensor_Val * 5.0)/1024.0; float T = (T_sensor_Voltage - 0.5) * 100; float T_F = (T * 1.8) +32.0; Serial.print(millis()); Serial.print(","); Serial.println(T); delay(1500); }
Alternative Methods of Saving Data
Python
Python
Based on the same Arduino sketch, you can write a Python program to read the data. This method enables you to use the data directly with VPython for visualization.
First you need to install the pySerial library (pySerial). Then you can read the serial data using:
data-log.py
import serial ser = serial.Serial('/dev/cu.usbmodem411', 9600) while True: print ser.readline()
CoolTerm
You can also use CoolTerm to save the serial data (see directions on how).
An SD Card
Another alternative, is to get a shield that can hold an SD card (SparkFun and Adafruit have ones for less than $20) and write to that.