Advertisement

Visualizing Data with Arduino

I like to visualize things, so I decided to prepare this simple tutorial to help you better understand how to visualize data using Arduino. This tutorial assumes that you have some basic electronics experience, can experiment with Arduino, and have some basic programming skills.

 

Arduino Serial Plotter

Serial Plotter is an amazing tool available in Arduino IDE (from v1.6.6) to visualize the “returned” data beyond just seeing numbers spit out onto the traditional serial monitor window. The Arduino Serial Plotter is, in fact, a software utility within the IDE that takes incoming serial values and graphs them against an X/Y axis.

 

To create a graph based on the received serial data from Arduino Serial Plotter, you need to program Arduino to read a sensor and direct the data to the Serial Port. Here, a standard light-dependent resistor (LDR) is used as an analog input sensor and directs its output to the Serial Plotter. You can start off by setting up the hardware and copying the example sketch shown below. After the upload, open the serial plotter by pressing “CTRL+SHIFT+L” (on Windows).

const int AnalogIn  = A0;



int sensorIn = 0;



void setup() {

Serial.begin(9600);

}



void loop() {

sensorIn = analogRead(AnalogIn);

Serial.println(sensorIn);

delay(100);

}

 

Here’s a random snapshot of the end results as displayed in Arduino’s Serial Plotter window:

 

As you can see when doing this experiment, one bothersome issue is that as soon as the plot hits the right side of the plotter window, it starts scrolling from right to left. There’s no way to stop/freeze it to buffer the values outside the display!

 

Arduino and Processing

This is, in fact, a “refresher” on the communication between the Arduino and Processing! Processing is a great source for creating graphics, and structurally, it is very similar to the Arduino IDE. The Processing IDE (https://processing.org/download/) communicates with the Arduino IDE through serial communication.

 

Here, I will show you how to make a simple galvanic skin response (GSR) device with the help of Arduino and Processing to measure the conductance of your skin. Hardware setup for this experiment is very easy and very similar to the earlier one. First of all, remove the LDR used in the setup, and extend +5-V and A0 connections as depicted in the hardware layout shown below. Also, replace the 10K resistor with a 47K to 100K one — this isn’t crucial, though.

 

After uploading the Arduino code (as usual), just copy/paste the Processing code into the processing application and run the processing program. Now put on the finger electrodes to see a graph. The “finger electrodes” are nothing but small foil sheets attached to exposed ends of the two wires coming from the +5-V and A0 points. The height of the graph represents the conductivity of your skin (the higher the graph, the higher the conductivity). The same setup can also be used as a “dirty” soil moisture level tester or a fabric wetness/dryness tester.

 

Arduino Code

const int AnalogIn  = A0;



int electrodeIn = 0;



void setup() {

  Serial.begin(9600);

}



void loop() {

  electrodeIn = analogRead(AnalogIn);

  Serial.println(electrodeIn);

delay(100);

}

 

Processing Code

import processing.serial.*;

Serial myPort;       

int xPos = 1;        

float inByte = 0;



void setup () {

size(400, 300);

println(Serial.list());

myPort = new Serial(this, “COM5”, 9600); // select this COMx same as to arduino serial monitor port number

myPort.bufferUntil(‘\n’);

background(0);

}



void draw () {

stroke(127, 34, 255);

line(xPos, height, xPos, height - inByte);

if (xPos >= width) {

    xPos = 0;

    background(0);

} else {

   

    xPos++;

  }

}



void serialEvent (Serial myPort) {

String inString = myPort.readStringUntil(‘\n’);

 if (inString != null) {

   

    inString = trim(inString);

    inByte = float(inString);

    println(inByte);

    inByte = map(inByte, 0, 1023, 0, height);

  }

}

So far, I described some basic Arduino data visualization tips and created simple graphs from serial data cooked by Arduino through the Serial Plotter and Processing program (not neck-deep, admittedly). Well, get ready to tackle another funny project. This displays a yellow circle against a blue background that changes its size according to the output (temperature level) given by an LM35 temperature sensor connected to Arduino Uno (LM35’s VCC to +5 V, GND to Gnd, and OUT to A0 of Arduino). See the Arduino & Processing sketch given below:

 

Arduino Code

float tempC;

int sensorPin = 0;



void setup()

{

Serial.begin(9600);

}



void loop()

{

tempC = analogRead(sensorPin);

tempC = (5.0 * tempC * 100.0)/1024.0;

Serial.println();

Serial.print(tempC);

delay(1000);

}

Processing Code

import processing.serial.*;

                   

int lf = 10;

String myString = null;

Serial commPort; // My Serial Port

float num;



void setup() {

 background(0,0,0);

 size(320,240);

 frameRate(120);

 commPort = new Serial(this, “COM5”, 9600); // Replace this COMx with your COM Port

 commPort.clear();

}



void draw() {

 while (commPort.available() > 0) {

 myString = commPort.readStringUntil(lf);

 if (myString != null) {

 print(myString);

 num=float(myString);

 println(num);

 println(frameCount);

 background(24,58,242);

 fill(#FFE02E);

 ellipse(160,120,num*4,num*4);



 }

 }

 

I’ve been a big fan of Processing since its inception and have rigged up so many data visualization projects. This little guide serves as a starter for novices to make the art go. Remember, if you have the time and inclination, there are many ways to make your Processing projects uniquely your own. Working on new projects always gives you the opportunity to learn and absorb new things!

 

 

 

2 Comments

Join the conversation!

Error! Please fill all fields.
Looking for the latest from TI?