# Graphing Values in Arduino, the EASY Way!

10,860

30

1

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.

Although I usually use this trick with the Serial Port, Sometimes I also use it with an LCD screen

### Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

## Step 1: Psuedocode!

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;

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;

## Step 2: Basic Example for Arduino

Here's an example to graph an analog sensor, such as a potentiometer. The analog pin is A0 for these examples.

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

void loop() {
// Analog Sensor Connected to analog pin 0

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

Try it, you know you want to!

When you do try it, you'll notice that the graph is sideways, and the bottom of the Serial Monitor shows the most recent reading of the Sensor.

You'll also notice that the graph can get quite long, and may be wrapped around to the next line if the windows isn't wide enough. To fix this we can use a function called map();

this will take in a minimum and maximum value for your sensor, and then scale it to a desirable output.

Here's how the code would look like for this example. Note, 1023 is the maximum output value for the analogRead() function. also note, we are scaling the analog read function to a value from 0 to 100. You will need to place this line of code before the for loop in the earlier example.

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

Here is the complete code for a scaled graph of an Analog sensor on analog pin 0.

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

void loop() {
// Analog Sensor Connected to analog pin 0

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

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

## Step 3: Other Places I've Used This Technique.

I've use this trick quite often while programming. One example was when I was graphing values on an LCD screen. Note, when graphing on an LCD screen using the LiquidCrystal library on Arduino, use lcd.print("                "); to print out blank spaces to clear the rest of the graph.

Participated in the
Pocket-Sized Contest

## Recommendations

• ### Internet of Things Class

22,824 Enrolled

## Discussions

It would probably be better to build a string of the length to send and only call the serial.println once. Pad out the end of the string with spaces to make it always 16 chars long. Either that or build an char array and a "dump array to serial" function. Make the dump array function run regularly in the code and just update the chars in the array when they change.

There are several ways of doing that without disturbing the main code. You could have a flag that says array updated, and the dump program dumps the array out and clears the flag. You could run a comparision dumping program with two arrays, one that is a copy of what is on the display currently and the other being the one you update. If the dump array function then only calls serial.println when the two arrays don't match, it copies one array over the other and sends out the whole copy array.

You could also just dump individual chars out via an interrupt call to the display and just rotate through them all 100 times a second.

The trick to a lot of projects is to have the supporting code not impinge much on the main program loop or function. Something that operates in the background and does the job without the main program halting for it.