Arduino and Processing
I’m getting started with collecting data using an Arduino board, but displaying it using Processing. Eventually, I want do collect the data from a Raspberry PI board, but I’m starting with an Arduino because I think it will be easier.
The first step is to hook up a temperature probe to the Arduino and start collecting data. I’m using a MAX31855 breakout board from Adafruit. I hooked it up as suggested in the tutorials:
MAX31855 DO to Arduino digital 3
MAX31855 CS to Arduino digital 4
MAX31855 CLK to Arduino digital 5
And then ran wires for ground and 3V.
Next I needed to download the Adafruit MAX31855 library. I unzipped the file and put it in my ~/Arduino/libraries directory. One thing I needed to do was rename their directory. It had underscores in it, which the Arduino IDE complained about. So I renamed the directory AdafruitMAX31855 and it worked fine.
Here is the code I loaded on the Arduino, which I basically copied from the Adafruit website, but took out all the comments to make it shorter.
#include "Adafruit_MAX31855.h" int thermoDO = 3; int thermoCS = 4; int thermoCLK = 5; Adafruit_MAX31855 thermocouple(thermoCLK, thermoCS, thermoDO); void setup() { Serial.begin(9600); delay(500); // wait for MAX chip to stabilize } void loop() { Serial.println(thermocouple.readFarenheit()); delay(1000); }
After I loaded this, I could open up the serial port monitor and watch the temperatures get printed. So that was done. Now on to Processing. Eventually, I’d like to generate a graph of the data. But first, I just want to see if I can simply read the data from the serial port and print it out in the Processing console.
Here is the code for Processing:
import processing.serial.*; Serial thePort; String theString = null; char endChar = '\n'; void setup() { thePort = new Serial(this, Serial.list()[2], 9600); theString = thePort.readStringUntil(endChar); println(Serial.list()); // only need this to get the list of ports so you know which one to use } void draw() { // everything happens in the serialEvent() } void serialEvent(Serial thePort) { theString = thePort.readStringUntil(endChar); if (theString != null) { theString = trim(theString); println(theString); } }
A few interesting things to note. You have to find out which port you’re using. On my mac, in the Arduino IDE, I have choices like this:
In Processing, this command will give a list of all the available ports.
println(Serial.list());
The output of that command will look like this:
/dev/cu.Bluetooth-Incoming-Port /dev/cu.Bluetooth-Modem /dev/cu.usbmodem1411 /dev/tty.Bluetooth-Incoming-Port /dev/tty.Bluetooth-Modem /dev/tty.usbmodem1411
I know from the Arduino IDE that I used /dev/cu.usbmodem1411 as the port. In the above array, that port is at position 2. So to open a connection to that port in Processing, I should use the command:
thePort = new Serial(this, Serial.list()[2], 9600);
This all works ok. I’m not that familiar with Processing, so I need to read up on it a bit more so I fully understand everything I did. And then the next step will be to move on to creating a graph.