## Simple Plots

Arduino’s Serial Plotter plots 500 (x,y) values.

The `y` value being plotted is an argument supplied to the `Serial.print()` command.
The `x` value is just a count of the number of data points plotted.

So if you have a loop which executes:

``````void loop() {
int v = analogRead(0);   // read from A0, a.k.a analog port 0
Serial.println(v);       // plots a value that will be between 0 and 512
}``````

On the first pass through the loop, an internal index kept by the Serial Plotter
program assigns the coordinate `x = 1` to the value being sent to the Serial plotter,
so it’s `(x,y)` coordinates are `(1,v)`. On the next pass, the analog port is
read again, and the Serial Plotter plots the new point at `(2,v)`. The bottom line
is that every time a point is plotted, the internal counter is incremented by 1.
The maximum number of plots which can be displayed at one time is 500.

After the Serial Plotter has displayed 500 values, it start moving its plot window,
one value at a time, to the right. So for example, at the time it plots `(500,v)`,
the x-axis ranges from 1 to 500. But when it plots `(501,v)`, then the x-axis
ranges from 2 to 501. At this point, the Serial Plotter becomes a “moving window”
on the data. At all times, that window is 500 values wide.

It is possible to see time varying input from an analog port even when no sensor
is attached to it. (This is because the output of such a port is “floating” between
0 and 5V.) So the following simple sketch might be the simplest plot
you can generate:

``````void setup() {
Serial.begin(9600);
while (!Serial) {};
}

void loop() {
Serial.println(v);       // Send the value read to the Console or Plotter, whichever is open.
delay(100);              // use a delay, so the 500-point buffer doesn't fill up instantly
}``````

A critical feature to note in the sketch above is the user of the command `Serial.println()`.
Until a `println` function is executed, the data point is not sent to the Serial Plotter.
However, you can execute the `println()` function in a separate command, so this works:

``````  Serial.print(v);        // nothing will be displayed yet
Serial.println();       // now the value v is available to the Serial Plotter``````

In any case, the end result will be a plot like that shown in the Introducion.

## Plotting 2 or More Values Simultaneously

When you try the preceding sketch, you will notice that the vertical scale does not remain
constant. As the data window moves from values that range from (say) 250 to 500 to a
later set of values that might range from (say) 250 to 350, the Serial Plotter adjusts the
vertical scale so that the data in the plot window is displayed with the maximum “zoom”.

If this drives you crazy, then there’s a way out: 3 values simultaneously, one which is
lower than the lowest value you expect to see displayed, one which is higher, and the
actual value. Now in order to plot multiple values, you must send some kind of
separator character between the values you send. The choices are a plain space (`" "`),
a comma (`,`), or a `TAB` (`\t`). The following `loop()` reads from analog port 0,
but writes both a maximum value and a minimum value at the same time, so the scale of
the graph never changes:

``````void loop() {
Serial.print(500);       // The maximum value to be displayed
Serial.print(" ");       // A spacer
Serial.print(v);         // Send the value read to the Console or Plotter, whichever is open.
Serial.print(" ");       // A spacer
Serial.print(0);         // The minimum value to be displayed
Serial.println();        // Send all 3 data values now!
delay(100);              // use a delay, so the 500-point buffer doesn't fill up instantly
}``````

As a result of `print`ing 2 constant data values as well as the time-varying voltage from
analog port 0 time `Serial.println()` is called, the plot window now contains 2 straight lines,
one above and one below the voltage.

When 2, 3, or more values are being plotted, one begins to wonder which value corresponds to
which reading. The color key near the top right corner of the plot display hold the answer.
Consider the following slightly more complicated `loop()` function, which involves not only
reading from analog port 0 but also analog port 5:

``````void loop() {

Serial.print(300);       // Plot a constant which is the max value to be displayed (#1)
Serial.print(" ");
Serial.print(v0);        // Plot the value read from A0 (#2)
Serial.print(" ");
Serial.print(v5);        // Plot the value read from A5 (#3)
Serial.print(" ");
Serial.print(200);       // Plot a constant which is the min value to be displayed (#4)
Serial.println();        // Send all 3 data values now!
delay(100);
}``````

Now there are 4 values being plotted, the max and min constants and the two analog voltages.
In the image below, notice that the color key shows: Blue -> Red -> Green -> Yellow.
Those correspond to the order in which the data are plotted in the sketch.

So:

1. The blue line corresponds to the first datum being plotted (`Serial.print(300)`.
2. The red line corresponds to the second datum being plotted (`Serial.print(v0)`.
3. The green line corresponds to the third datum being plotted (`Serial.print(v5)`.
4. The yellow line corresponds to the fourth datum being plotted (`Serial.print(200)`.

We see that the voltage being read from A0 is consistently higher than the voltage being read from A5.