Introduction: Data Logging Sensors/Inputs With Processing

Picture of Data Logging Sensors/Inputs With Processing

Simple guide to using processing software to log data from your Arduino through the Serial port. The data will be log and save as a csv table for easy of use with other software.

Processing is used for a variety of projects, from visual arts to research.

Here is the website for Processing: https://processing.org/

They have simple tutorials to get you started and a reference page to help with understanding the code.

Items you will need:

Arduino Software(including attached sensor of your choice) - We will be using the button project from my previous instructable to aid in this project, but a simple button input, like the StateChange example Arduino provides, will also work.

Processing Software

~We will skip over the Arduino hook-up part, and go straight to the the code and output.

Step 1: Setup the Arduino With Code

Picture of Setup the Arduino With Code

This is a simple code that output the number one to the serial port as a string.

The output will be what we collect with processing. Ideally you want to have it output as a string otherwise Processing will not record the number or letter that is output, but the DEC number of the value.

You can find out more about DEC number at: http://www.asciitable.com/

~You can program either Arduino to output string values or program Processing to convert values to string.

For this example we will have Arduino output as a string.

Step 2: Processing Code for Data Collection

Picture of Processing Code for Data Collection

This first bit of code will make Processing collect data and store it as a csv file .

Before starting Processing, make sure the Arduino sketch is uploaded to your Arduino, but do not open the serial monitor. If the serial monitor is open Processing will not be able to access the COM port and collect data.

//import the required libraries
import processing.serial.*;
Serial mySerial;
Table table;

void setup()
{
  //set mySerial to listen on COM port 10 at 9600 baud
  mySerial = new Serial(this, "COM10", 9600);
  
  table = new Table();
  //add a column header "Data" for the collected data
  table.addColumn("Data");
}

void draw()
{
  if(mySerial.available() > 0)
  {
    //set the value recieved as a String
    String value = mySerial.readString();
    //check to make sure there is a value
    if(value != null)
    {
      //add a new row for each value
      TableRow newRow = table.addRow();
      //place the new row and value under the "Data" column
      newRow.setString("Data", value);
    }
  }
}

void keyPressed()
{
  //save as a table in csv format(data/table - data folder name table)
  saveTable(table, "data/table.csv");
  exit();
}

*Make sure to adjust the code for the same COM port and and baud rate as your Arduino sketch*

This bit of code is great for one time data collection of your project, but it lacks a few things. In the next step, more code will be added to include date/time of each input and save the table with the date as the filename.

Step 3: Add Timestamp to Data and File

This part will add code to the previous step and allow for files to be saved without having to change the save file name. The code will put a timestamp on each received value from the Arduino. When the Processing program is closed it will name the file with the timestamp(yyyy-mm-dd-hh-mm).

//import the required libraries
import processing.serial.*;

Serial mySerial;
Table table;
String filename;

void setup()
{
  //set mySerial to listen on COM port 10 at 9600 baud
  mySerial = new Serial(this, "COM10", 9600);
  
  table = new Table();
  //add a column header "Data" for the collected data
  table.addColumn("Data");
  //add a column header "Time" and "Date" for a timestamp to each data entry
  table.addColumn("Time");
  table.addColumn("Date");
}

void draw()
{
  //variables called each time a new data entry is received
  int d = day();
  int m = month();
  int y = year();
  int h = hour();
  int min = minute();
  int s = second();
  
  if(mySerial.available() > 0)
  {
    //set the value recieved as a String
    String value = mySerial.readString();
    //check to make sure there is a value
    if(value != null)
    {
      //add a new row for each value
      TableRow newRow = table.addRow();
      //place the new row and value under the "Data" column
      newRow.setString("Data", value);
      //place the new row and time under the "Time" column
      newRow.setString("Time", str(h) + ":" + str(min) + ":" + str(s));
      //place the new row and date under the "Date" column
      newRow.setString("Date", str(d) + "/" + str(m) + "/" + str(y));
    }
  }
}

void keyPressed()
{
  //variables used for the filename timestamp
  int d = day();
  int m = month();
  int h = hour();
  int min = minute();
  int s = second();
  //variable as string under the data folder set as (mm-dd--hh-min-s.csv)
  filename = "data/" + str(m) + "-" + str(d) + "--" + str(h) + "-" + str(min) + "-" + str(s) + ".csv";
  //save as a table in csv format(data/table - data folder name table)
  saveTable(table, filename);
  exit();
}

Things to note about the code:

-two new columns for time and date

-in void draw() (similar to void loop()) the time and date is called each time to ensure each variable is recorded at that time

-you can rearrange the columns an any order in void setup(), void draw() has no affect on the order of the columns

-the program when closed will save the file as .csv with the variable filename as the title. The title is a combination of the month, day, hours, minutes, and seconds.(This is to ensure that new files don't overwrite each other.)

*Code can be add to the the filename string to include the year*

NOTE: If there is other inputs that need to be in its own separate column, code would be needed to differentiate from each value received by Processing. This could be additional code for both the Arduino and Processing sketch.

Comments

karlt1 (author)2016-12-02

good tuto

take a look at :

e-mole.cz/diy/molegraph

and this app too

may be it will help you to have something better

HenryC86 (author)karlt12016-12-03

Thanks! I'll take a look at it.

About This Instructable

1,034views

21favorites

License:

Bio: I am a engineering graduate with a degree in Engineering Management. I tinker with bits and pieces of electronics to make interesting items and learn ... More »
Add instructable to: