Seeing Sensors: How to visualize and save Arduino-sensed data

Sometimes when you’re prototyping a project it’s nice to see what’s going on with your sensors or variables in your code. Sure you can watch analogRead() values scroll at warp speed on your Arduino Serial Monitor, but what if you want to save that data? Or look at a graph you make in real time instead of numbers? Maybe you want to do some self tracking and visualize your Fitbit stats, or compare two different light sensors or force sensing resistors? In this class I’ll demonstrate two workflows that help us do this…

The Basics
Check out the Analog In lab. Using this code:

int sensorPin = A0;    // analog input pin to hook the sensor to
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() { 
  Serial.begin(9600); // initialize serial communications 
}

void loop() {
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.println(sensorValue); // print value to Serial Monitor
  delay(50); // short delay so we can actually see the numbers
}

With a potentiometer (or other sensor) hooked up to analog pin 0, you should be able to click the magnifying glass icon on the top right of the Arduino screen and see something like this:

Graphing Serial Data in Processing
Okay so now you have data streaming in from your Arduino through your USB cable, and can see the values on the Serial Monitor. But unless you have Matrix-like visual skillz, these streaming numbers might be hard to keep track of. The next step is to use another program called Processing to visualize this data.

Check out step 4 of the Serial Out Lab. Load this code onto the Arduino:

int sensorPin = A0;    // analog input pin to hook the sensor to
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() { 
  Serial.begin(9600); // initialize serial communications 
}

void loop() {
  sensorValue = analogRead(sensorPin)/4; // read the value from the sensor
  Serial.write(sensorValue); // print bytes to serial
  delay(10);
}

Then use this code in Processing:

import processing.serial.*;
Serial myPort;        // The serial port
float xPos = 0;             // horizontal position of the graph

void setup () {
  size(800, 600);        // window size

  // List all the available serial ports
  println(Serial.list());

  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);

  background(#EDA430);
}

void draw () {
  // nothing happens in draw.  It all happens in SerialEvent()
}

void serialEvent (Serial myPort) {
  // get the byte:
  int inByte = myPort.read();
  // print it:
  println(inByte);

  float yPos = height - inByte;
  // draw the line in a pretty color:
  stroke(#A8D9A7);
  line(xPos, height, xPos, height - inByte);

  // at the edge of the screen, go back to the beginning:
  if (xPos >= width) {
    xPos = 0;
    // clear the screen by resetting the background:
    background(#081640);
  }
  else {
    // increment the horizontal position for the next reading:
    xPos++;
  }
}

If everything goes well, you should get a quickly scrolling graph that looks something like this:

Saving Data with CoolTerm and Graphing with Excel (or OpenOffice/Google Docs equivalent)
Sweet! Now you can “see” the numbers coming through the serial port in two different ways. Suppose you want to save that data to visualize, use as an outline for a laser cut data guru trophy, compare sensors, etc. One of the easiest ways to do this is through CoolTerm.

Check out step 3 of the Serial Out lab. Now upload this code to the Arduino:

int sensorPin = A0;    // analog input pin to hook the sensor to
int sensorValue = 0;  // variable to store the value coming from the sensor

void setup() { 
  Serial.begin(9600); // initialize serial communications 
}

void loop() {
  sensorValue = analogRead(sensorPin); // read the value from the sensor
  Serial.print(millis()); // print the time in milliseconds since the program started
  Serial.print(',');
  Serial.println(sensorValue); // print to serial
}

Notice we’re printing two values separated by a comma. This will come in handy in a minute. Now open CoolTerm, and you should see something like this:

Big deal, right? Same thing as on the Arduino Serial Monitor pretty much, right? Well as far as the viewer goes, you’re pretty much right. However, CoolTerm has a built in function to allow you to save data to a text file that we can open up later in a dozen different programs to have a look. While your data is scrolling past, navigate to Connections–>Capture to Textfile–>Start (like this):

Note the short cut keys. After you start, a screen will pop up asking you where and how to save the file. Just choose the defaults for now – the timestamp in the file name is super helpful. After you start, do something interesting to your sensor (twist the potentiometer, cover the light sensor, etc.).

Okay! Now, open Excel or your spreadsheet program of choice (OpenOffice Calc, Google Spreadsheet, etc.) – they will have similar workflows. I’m on Excel 2007 on a Windows 7 machine, fyi. Go through the motions to open the txt file you just created (note you may have to make sure All Files is selected in the drop down menu shown on the bottom right of the screenshot below, or you’ll only see .xls files and not .txt files).

Excel will be a little confused at first as to why you’re opening a txt file, so it will make you go through a few steps. The first one looks like this:

Just leave the defaults and hit next. On this screen, check the box next to Comma as shown below:

Then click on Finish. If everything worked out, you should now be looking at two columns of data neatly separated where the comma used to be. Score! Almost done. Now, let’s graph this data. Highlight columns A and B, then choose Insert–>Scatter–>Scatter with Smooth Lines:

This will insert a graph into your spreadsheet and look something like this:

DONE! Now you can save it, add a title, label your axes, whatever you want. And you have a digital diary of what was going on at a particular moment in time with your sensor.