Arduino Based Graph

Arduino Based Graph © LGPL

How to make a graph from an analog input with Processing and Arduino.

  • 1,626 views
  • 3 comments
  • 6 respects

Components and supplies

About this project

This shows you how to send a byte of data from the Arduino or Genuino to a personal computer and graph the result. This is called serial communication because the connection appears to both the board and the computer as a serial port, even though it may actually use a USB cable, a serial to USB and a USB to serial converter.

You can use the serial monitor of the Arduino Software (IDE) to view the sent data, or it can be read by Processing (see code below), Flash, PD, Max/MSP, etc.

Circuit

Connect a potentiometer or other analog sensor to analog input 0.

Schematic

Code

/*  Graph
 A simple example of communication from the Arduino board to the computer: The  value of analog input 0 is sent out the serial port. We call this "serial"  communication because the connection appears to both the Arduino and the  computer as a serial port, even though it may actually use a USB cable. Bytes  are sent one after another (serially) from the Arduino to the computer.
 You can use the Arduino Serial Monitor to view the sent data, or it can be  read by Processing, PD, Max/MSP, or any other program capable of reading data  from a serial port. The Processing code below graphs the data received so you  can see the value of the analog input changing over time.
 The circuit:  - any analog input sensor attached to analog in pin 0*/
void setup() {  // initialize the serial communication:  Serial.begin(9600);}
void loop() {  // send the value of analog input 0:  Serial.println(analogRead(A0));  // wait a bit for the analog-to-digital converter to stabilize after the last  // reading:  delay(2);}
/* Processing code for this example
 // Graphing sketch
 // This program takes ASCII-encoded strings from the serial port at 9600 baud  // and graphs them. It expects values in the range 0 to 1023, followed by a  // newline, or newline and carriage return
 import processing.serial.*;
 Serial myPort;        // The serial port  int xPos = 1;         // horizontal position of the graph  float inByte = 0;
 void setup () {    // set the window size:    size(400, 300);
   // List all the available serial ports    // if using Processing 2.1 or later, use Serial.printArray()    println(Serial.list());
   // I know that the first port in the serial list on my Mac is always my    // Arduino, so I open Serial.list()[0].    // Open whatever port is the one you're using.    myPort = new Serial(this, Serial.list()[0], 9600);
   // don't generate a serialEvent() unless you get a newline character:    myPort.bufferUntil('\n');
   // set initial background:    background(0);  }
 void draw () {    // draw the line:    stroke(127, 34, 255);    line(xPos, height, xPos, height - inByte);
   // at the edge of the screen, go back to the beginning:    if (xPos >= width) {      xPos = 0;      background(0);    } else {      // increment the horizontal position:      xPos++;    }  }
 void serialEvent (Serial myPort) {    // get the ASCII string:    String inString = myPort.readStringUntil('\n');
   if (inString != null) {      // trim off any whitespace:      inString = trim(inString);      // convert to an int and map to the screen height:      inByte = float(inString);      println(inByte);      inByte = map(inByte, 0, 1023, 0, height);    }  }
*/
/* Max/MSP v5 patch for this example
----------begin_max5_patcher----------1591.3oc0YszbaaCD9r7uBL5RalQUAO3CvdyS5zVenWZxs5NcfHgjPCIfJITRTxj+6AOHkoTDooroUs0AQPR73a+1cwtK3WtZxzEpOwqlB9YveAlL4KWMYh6Q1GLo99ISKXeJMmU451zTUQAWpmNy+NM+SZ2y+sR1l02JuU9t0hJvFlNcMPydOuBv.U5Rgb0LPpRpYBooM3529latArTUVvzZdFPtsXAuDrrTU.f.sBffXxLvGE50lIHkUVJXq3fRtdaoDvjYfbgjujaFJSCzq4.tLaN.bi1tJefWpqbO0uz1IjIABoluxrJ1guxh2JfPO2B5zRNyBCLDFcqbwNvuv9fHCb8bvevyyEU2JKTYhkBSWPAfq2TZ6YhqmuMUo0feUn+rYpY4YtY+cFw3lUJdCMYAapZqzwUHX8ScrjAd+SIOU6UBAwIygy.Q1+HAA1KH6EveWOFQlitUK92ehfal9kFhUxJ3tWcsgpxadigWExbt1o7Ps5dk3yttivyg20W0VcSmg1G90qtx92rAZbH4ez.ruy1nhmaDPidE07J+5n2sg6E6oKXxUSmc20o6E3SPRDbrkXnPGUYE.i5nCNB9TxQjG.G0kCTZtH88f07Rt0ZMMWUw8VvbKVAaTk6GyoraPdZff7rQTejBN54lgyvHE0Ft7AvIvvgvIwO23jBdUkYOuSvIFSiNcjFhiSsUBwsUCh1AgfNSBAeNDBZDIDqY.f8.YjfjV1HAn9XDTxyNFYatVTkKx3kcK9GraZpI5jv7GOx+Z37Xh82LSKHIDmDXaESoXRngIZQDKVkpxUkMCyXCQhcCK1z.G457gi3TzMz4RFD515FG3bIQQwcP3SOF0zlkGh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7mCVGCLce7GFcQwDdH5Ta3nyAS0pQbHxegr+tGIZORgMRnMj5vGl1Fs16drnk7Tf1XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd.HJ5hhnng3h9HPj4lud02.1bxGw.-----------end_max5_patcher-----------
*/ 

Processing Sketch

Using the Processing sketch in the code sample above, you'll get a graph of the sensor's value. As you change the value of the analog sensor, you'll get a graph something like this:

Code

codeC/C++
 /* Graph

  A simple example of communication from the Arduino board to the computer: The
  value of analog input 0 is sent out the serial port. We call this "serial"
  communication because the connection appears to both the Arduino and the
  computer as a serial port, even though it may actually use a USB cable. Bytes
  are sent one after another (serially) from the Arduino to the computer.

  You can use the Arduino Serial Monitor to view the sent data, or it can be
  read by Processing, PD, Max/MSP, or any other program capable of reading data
  from a serial port. The Processing code below graphs the data received so you
  can see the value of the analog input changing over time.

  The circuit:
  - any analog input sensor attached to analog in pin 0
*/

void setup() {
  // initialize the serial communication:
  Serial.begin(9600);
}

void loop() {
  // send the value of analog input 0:
  Serial.println(analogRead(A0));
  // wait a bit for the analog-to-digital converter to stabilize after the last
  // reading:
  delay(2);
}

/* Processing code for this example

  // Graphing sketch

  // This program takes ASCII-encoded strings from the serial port at 9600 baud
  // and graphs them. It expects values in the range 0 to 1023, followed by a
  // newline, or newline and carriage return

  import processing.serial.*;

  Serial myPort;        // The serial port
  int xPos = 1;         // horizontal position of the graph
  float inByte = 0;

  void setup () {
    // set the window size:
    size(400, 300);

    // List all the available serial ports
    // if using Processing 2.1 or later, use Serial.printArray()
    println(Serial.list());

    // I know that the first port in the serial list on my Mac is always my
    // Arduino, so I open Serial.list()[0].
    // Open whatever port is the one you're using.
    myPort = new Serial(this, Serial.list()[0], 9600);

    // don't generate a serialEvent() unless you get a newline character:
    myPort.bufferUntil('\n');

    // set initial background:
    background(0);
  }

  void draw () {
    // draw the line:
    stroke(127, 34, 255);
    line(xPos, height, xPos, height - inByte);

    // at the edge of the screen, go back to the beginning:
    if (xPos >= width) {
      xPos = 0;
      background(0);
    } else {
      // increment the horizontal position:
      xPos++;
    }
  }

  void serialEvent (Serial myPort) {
    // get the ASCII string:
    String inString = myPort.readStringUntil('\n');

    if (inString != null) {
      // trim off any whitespace:
      inString = trim(inString);
      // convert to an int and map to the screen height:
      inByte = float(inString);
      println(inByte);
      inByte = map(inByte, 0, 1023, 0, height);
    }
  }

*/

/* Max/MSP v5 patch for this example

 ----------begin_max5_patcher----------
1591.3oc0YszbaaCD9r7uBL5RalQUAO3CvdyS5zVenWZxs5NcfHgjPCIfJIT
RTxj+6AOHkoTDooroUs0AQPR73a+1cwtK3WtZxzEpOwqlB9YveAlL4KWMYh6
Q1GLo99ISKXeJMmU451zTUQAWpmNy+NM+SZ2y+sR1l02JuU9t0hJvFlNcMPy
dOuBv.U5Rgb0LPpRpYBooM3529latArTUVvzZdFPtsXAuDrrTU.f.sBffXxL
vGE50lIHkUVJXq3fRtdaoDvjYfbgjujaFJSCzq4.tLaN.bi1tJefWpqbO0uz
1IjIABoluxrJ1guxh2JfPO2B5zRNyBCLDFcqbwNvuv9fHCb8bvevyyEU2JKT
YhkBSWPAfq2TZ6YhqmuMUo0feUn+rYpY4YtY+cFw3lUJdCMYAapZqzwUHX8S
crjAd+SIOU6UBAwIygy.Q1+HAA1KH6EveWOFQlitUK92ehfal9kFhUxJ3tWc
sgpxadigWExbt1o7Ps5dk3yttivyg20W0VcSmg1G90qtx92rAZbH4ez.ruy1
nhmaDPidE07J+5n2sg6E6oKXxUSmc20o6E3SPRDbrkXnPGUYE.i5nCNB9TxQ
jG.G0kCTZtH88f07Rt0ZMMWUw8VvbKVAaTk6GyoraPdZff7rQTejBN54lgyv
HE0Ft7AvIvvgvIwO23jBdUkYOuSvIFSiNcjFhiSsUBwsUCh1AgfNSBAeNDBZ
DIDqY.f8.YjfjV1HAn9XDTxyNFYatVTkKx3kcK9GraZpI5jv7GOx+Z37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3.2p6jgi9hsNIv97CoyJ01xzl
c3ZhUCtSHx3UZgjoEJYqNY+hYs5zZQVFW19L3JDYaTlMLqAAt1G2yXlnFg9a
53L1FJVcv.cOX0dh7mCVGCLce7GFcQwDdH5Ta3nyAS0pQbHxegr+tGIZORgM
RnMj5vGl1Fs16drnk7Tf1XOLgv1n0d2iEsCxR.eQsNOZ4FGF7whofgfI3kES
1kCeOX5L2rifbdu0A9ae2X.V33B1Z+.Bj1FrP5iFrCYCG5EUWSG.hhunHJd.
HJ5hhnng3h9HPj4lud02.1bxGw.
-----------end_max5_patcher-----------

*/

Schematics

CONNECTION
Graph circuit3 gn55ygpitw
SCHEMATICS
Analogreadserial sch vozq95b70c

Comments

Similar projects you might like

MQ-4 Natural Gas Sensor Experiment Based on Arduino

Project tutorial by Andrew85

  • 1,237 views
  • 1 comment
  • 7 respects

Bar Graph

Project tutorial by Tech with Matthew

  • 9,501 views
  • 16 comments
  • 42 respects

RF BASED SMART HOME AUTOMATION SYSTEM

by Dennis Mwanza

  • 31,765 views
  • 4 comments
  • 48 respects

Arduino Based Digital Temperature Sensor

Project showcase by Abishek Bhalaaji

  • 6,876 views
  • 0 comments
  • 7 respects

Digital Oscilloscope Experiment Based on Arduino

Project tutorial by Andrew85

  • 2,905 views
  • 3 comments
  • 18 respects
Add projectSign up / Login