Sub topic 11
Graphing Values with the Serial Plotter
The Serial Plotter is an Arduino IDE tool that draws changing numeric values over time. It is useful for observing sensor signals, checking whether a value is stable, and finding brief changes that are difficult to see in printed text.
This chapter assumes that you can upload a sketch, use setup() and loop(), read analog inputs with analogRead(), and use variables and if statements.
How the Serial Plotter reads data
The Arduino sends serial text one line at a time. The Serial Plotter treats each line as one sample in time. Each numeric field becomes a plotted series.
For labeled data, use this pattern:
temperature:23.50,light:742The label appears in the plotter legend. A comma separates series, and Serial.println() ends the sample with a newline.
Printing values for the plotter
Open the Serial Plotter from the Arduino IDE after uploading this sketch. Select the same baud rate in the plotter that the sketch passes to Serial.begin().
const int SENSOR_A = A0;
const int SENSOR_B = A1;
unsigned long lastSample = 0;
const unsigned long SAMPLE_INTERVAL = 50;
void setup() {
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
if (now - lastSample >= SAMPLE_INTERVAL) {
lastSample = now;
int light = analogRead(SENSOR_A);
int secondValue = analogRead(SENSOR_B);
Serial.print(F("light:"));
Serial.print(light);
Serial.print(F(",second:"));
Serial.println(secondValue);
}
}The interval is 50 milliseconds, so the sketch sends about 20 samples per second. The millis() check avoids stopping the rest of loop(). This matters when the sketch must also control outputs or read other sensors.
The Serial Plotter expects numbers. Avoid sending status messages such as Sensor ready while graphing. Text that does not follow the numeric format can create confusing points or disrupt the display. Send startup messages before opening the plotter, or disable them during plotting.
Choosing useful sample rates
A faster sample rate is not always better. A slow sensor may need only a few samples per second. A rapidly changing signal needs more samples, but each printed line takes time to transmit.
At 9600 baud, long lines are transmitted more slowly than short lines. If output becomes delayed, use a higher matching baud rate such as 115200, shorten labels, or increase the sample interval.
Common plotting errors
- Use the same baud rate in
Serial.begin()and the Serial Plotter. - End every sample with
Serial.println(). - Keep the same labels and number of fields on every line.
- Print only numeric values after each label.
- Increase the interval if the graph appears slow or the board has other time-sensitive work.
- Check the wiring if a line is stuck at
0or at the maximum analog value.
Exercise
Why does this output produce two plotted series: Serial.println(String("x:") + x + ",y:" + y);?
Show answer
The comma separates two fields, and each field has a label followed by a numeric value. The newline marks the end of one sample.