Most of the time I want to post analyze the sensor data read from the Arduino within a spreadsheet. By default, I open up a serial monitor and copy/paste the displayed results into a spreadsheet. I have also used an SD card (link). Both methods have limitations. In the case in which sensor readings are printed to the serial monitor, the data that I can post process is limited to what is being displayed. In the case of the SD card, there is added complexity in adding the SD HW, card, and implementing the additional code that is different from the simple Serial.print() and Serial.println() functions.
A solution to using the simplicity of the Serial.print() functions within an Arduino sketch and then saving the results to a file is to add the Processing IDE to the toolchain.
The goal of this post is to walk through how data written to a serial port within an Arduino Sketch can be written to a text file by using the Processing IDE…on a Mac running OSX10.9. Mileage will vary on different OS versions or types (Windows, Linux..).
The steps that create a workflow where Serial.print(ln)s from an Arduino Sketch are redirected to a text file via the Processing IDE include:
- An Arduino sketch that is sending data to the serial port
- A processing sketch that opens a text file, reads the data on the serial port, and then write the data to the text file
I will skip how these IDEs get onto your workstation. There are many examples and explanations on installing the Arduino and Processing IDEs.
The key for me to get both IDEs to “share” a serial port was to start the Arduino sketch first. Before running the Processing sketch, I could open the serial monitor. Then Processing will start. If this order is not followed, a message similar to this one:
will appear. Don’t take in input from Arduino’s serial monitor. This will cause the printing of results to be unpredictable. Sometime the results will print to the serial monitor. Sometimes the results will get to Processing. I ended up “hard coding” the continuous reading command when I wanted results to go to a file.
Arduino Sketch: Write Values to Serial Port
The example I’ll use is the one I used to send pH values read by the Atlas-Scientific pH stamp circuit. Instead of copy/pasting, Iposted the sketch at this GitHub location as well as Atlas-Scientific’s original sketch. It is basically the same sketch as the one Atlas-Scientific posts on its site except no input comes from the serial monitor. The other change was converting the string pH reading into a float. I was getting noise as well as the string formatting was different between the two environments. Given I see these as “throw away” sketches – leaving how to whip them up as needed – I decided to just convert to a float and deal with floats instead of strings.
The only command passed in is the continuous read command. The continuous read command tells the Atlas-Scientific pH stamp to continuously send back pH readings. The readings continuously being sent to the Arduino from the pH stamp are then printed to a serial port just like any other Arduino sketch.
Processing Sketch: Read Values from Serial Port and Save to a File
The Processing Sketch can be found at this GitHub location. I followed the processing sketch that I found from Daniel Christopher’s Instructable (link). I modified the sketch to output to a file. I’ve spent little more than 1/2 an hour working with Processing code. A Processing sketch is very similar to an Arduino sketch. The challenges I came across included:
- listening on the right port. Most of the Processing sketches I saw used the line- port = new Serial(this, Serial.list(), 9600); -to associate a variable to a port. This isn’t a great idea because as is my case, the serial port used by the Arduino IDE is not the first one…I don’t know the //Processing code well enough to generalize. I did something equally bad – hardcoding the name of the port- String portName = “/dev/tty.usbmodem1451”; port = new Serial(this, portName, 9600);
It works. So I’m using it (until I stumble across the more elegant solution).
- How to write data coming in from a serial port out to a file. It turns out to be easy!
- hard coded the path and filename of the file that will hold the sensor readings. E.g.: output = createWriter(“/Users/margaret/Documents/Lettuce Buddy HW/HealthypH/DataFiles/ASContinuousRead/ASBakngSoda.csv”);
I look at both the Arduino and Processing Sketch’s code as the UI. So hard coding inputs works ok for me since I can easily change a variable and do another run.
I was ecstatic to get sensor readings from the Arduino into a file without copy/pasting the readings that are printed on the serial monitor or by including an SD card. Processing made this so much simpler. The code I threw together is just that – thrown together code. This post is like a recipe. In this case a recipe for sending readings printed to a serial port within an Arduino sketch to a file. This is handy when finding statistics on a batch of readings.
Thanks for reading this far. Please find many (many) things to smile about.