Introduction: Graphing on an Arduino, the EASY WAY!

Sometimes when you're testing a sensor or debugging a value in an Arduino project, you want to see something other than numbers flying by in the Serial Monitor. However, you want to get the sensor working quickly, and you don't want to take the time to write code to graph it or display it in an intricate way.

Well, we can fix that :)

The trick is very simple. However it graphs it sideways, but hey, it's quick and easy.

The basic idea to graphing really quickly is this
print a basic letter, which can be a unit for the graph such as a lowercase L, l;

however, we want to print this letter repeatedly to emulate a graph.

Pseudocode:

//Gather Sensor Data;

// scale values down to a reasonable size

for(int i = 0; i < (The value you want to graph); i++) {
//print the letter you chose, such as "l";
// but print it all on the same line;
}
// now go to the next line in the Serial Monitor;



Here is some sample code to read and graph the input from AnalogPin A0;

void setup() {
  Serial.begin(9600);
}

void loop() {
  // Analog Sensor Connected to analog pin 0
  int SensorVal = analogRead(A0);

  SensorVal = map(SensorVal, 0, 1023, 0, 100);

  for(int i = 0; i < SensorVal; i++) {
    Serial.print("l");
  }
  Serial.println();
  delay(50);
}

Comments

author
BilalA20 (author)2015-09-19

it will make the loop duration varying, will have extremely bad results.

author
Phil_S (author)2015-01-19

Hello,

Not a very nice graph, russ? What do you expect on Serial Monitor? Not Excel. Just be grateful.

Actually, very nice, just what I was looking for for looking at temporary temperature changes - mapped 0 to 50 degrees C to 0 to 100 or 0 to 200.

Well done chowmix

author
alexis Water Features (author)2014-08-19

Thanks a lot! Such a simple idea and very effective for what I wanted which was to roughly represent an audio output into the arduino.

author
davidsmith99 (author)2012-12-07

I've used similar methods for serial port diagnostics for years.
Here's an enhancement your graph above to print the number to be graphed as well... and to get the format of the number consistent.
Uses Arduino syntax

#include <stdlib.h>
float z = 0;
float y = 0;
float x = 0;
static char dtostrfbuffer[15];    // needed by graph:  print float

void loop() {
    .....
    z = graph(y)     // the number you want to graph at a suitable rate such as 1 - 10 Hz}
    ...
}


int graph (float z)  // draw a text-based vertical graph to monitor
{
  int i = 0;
  int k = 0;
  int maxWidth = 80;
  k = int(z);
  if (k > maxWidth) k = maxWidth;  // width of the graph "column"
        // dtostrf(FLOAT,WIDTH,PRECSISION,BUFFER);
        // gist.github.com/2343665  
        // www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#g6c140bdd3b9bd740a1490137317caa44
  dtostrf(z,8, 2, dtostrfbuffer);  // -nnnnnn.nn Use this for a consistent float format
  Serial.print(dtostrfbuffer);

  Serial.print(" |");
  for (i=0; i<maxWidth; i++) {
    if (i<=k) Serial.print("-");
    else Serial.print(" ");
  }
  Serial.print("|\n");
  return k;
}

OUTPUT looks like:  (needs monospaced font)


   46.51 |-----------------------------------------------                                 |
   47.32 |------------------------------------------------                                |
   45.81 |----------------------------------------------                                  |
   42.00 |-------------------------------------------                                     |
   41.92 |------------------------------------------                                      |
   39.60 |----------------------------------------                                        |
   35.07 |------------------------------------                                            |
   32.36 |---------------------------------                                               |
   29.51 |------------------------------                                                  |
   28.58 |-----------------------------                                                   |
   23.59 |------------------------                                                        |
   22.59 |-----------------------                                                         |
   19.63 |--------------------                                                            |
   14.75 |---------------                                                                 |
   13.98 |--------------                                                                  |
    9.38 |----------                                                                      |
    6.96 |-------                                                                         |
    4.78 |-----                                                                           |
    2.87 |---                                                                             |
    3.24 |----                                                                            |
   -0.08 |-                                                                               |
    0.94 |-                                                                               |
   -1.70 |                                                                                |
    0.01 |-                                                                               |
    0.09 |-                                                                               |
   -1.47 |                                                                                |
    1.31 |--                                                                              |
    2.44 |---                                                                             |
    1.89 |--                                                                              |
    3.65 |----                                                                            |
    5.68 |------                                                                          |
    9.96 |----------                                                                      |
   12.46 |-------------                                                                   |
   15.14 |----------------                                                                |
   15.96 |----------------                                                                |
   20.88 |---------------------                                                           |

author
russ_hensel (author)2012-08-28

Not a very good graph, but so simple it gets my vote ( do I have one? ).

author
ahz (author)2012-07-02

very cool trick
very handy and easy to use

big thanks ;)

About This Instructable

14,415views

80favorites

License:

Bio: I am a young engineer who likes to build things. My motto is Why buy, when you can Make! in 2010 I got an Arduino ... More »
More by Chowmix12:Graphing on an Arduino, the EASY WAY!Graphing values in Arduino, the EASY way!Never lose an RC BodyClip again!
Add instructable to: