The Serial Monitor and Plotter
The Serial Monitor is a text window on the other end of the USB cable, and the Serial Plotter is the same stream drawn as a graph. Both only make sense when their baud menu says the same number as Serial.begin().
The two ends of one wire
Serial in a sketch is the ATmega328P's UART, wired from D0 and D1
straight to the CH340C, the chip beside the USB-C socket that turns it into a serial port on
your computer. The Serial Monitor (the magnifying-glass button at the top
right of the IDE, or Tools, Serial Monitor) opens that port and shows what
arrives as text.
Serial.begin(9600) sets the chip's side to 9600 bits a second. The monitor
has its own baud menu, and nothing on the cable tells one end what the other
chose. The line carries highs and lows, and each end times them with its own
clock.
At the same speed every sample lands in the middle of a bit and the letters come out. At any other speed the monitor builds bytes the sketch never sent, and you see symbols. The fix is always the menu, never the sketch: every sketch in this book uses 9600, and the menu stays there once it is set.
Each character costs 10 bits on the line (a start bit, eight data bits and a
stop bit), so 9600 baud is 960 characters a second. A sketch that prints more
than that fills the 64-byte transmit buffer, and from then on every print()
waits for room. If printing makes a sketch slow, that is why.
print and println
Serial.print(x) sends x and nothing else. Serial.println(x) sends x
and then two characters, a carriage return and a newline, so the next output
starts on a new line. print for the parts of a line, println for the last.
Numbers go out as the digits you would write: Serial.print(42) sends the two
characters 4 and 2. Serial.write(42) sends one byte with the value 42,
which the monitor shows as *. The rest of this book uses print.
Typing back
The box at the top of the monitor sends what you type when you press Enter. The menu beside the baud menu decides what is added at the end: nothing, a newline, a carriage return, or both. Watch the board as you send: the RX lamp flickers, because it lights whenever D0 is pulled LOW by the CH340C. The TX lamp does the same for D1 while the sketch prints.
Opening the monitor resets the board
When the monitor opens the port, the CH340C pulls its DTR line low, and on this board DTR reaches the chip's reset pin through a 100 nF capacitor (C6). The edge becomes a short pulse on RESET and the sketch starts again from setup(). The upload uses the same pulse to wake the bootloader.
It is useful: the first lines in the window are always the start of the run, not the middle. It also means a sketch cannot keep running undisturbed while you connect to it; anything it was counting starts over.
The Serial Plotter
Tools, Serial Plotter reads the same port and draws numbers instead of printing them. Arduino's documentation for IDE 2 sets out the format:
- one line per sample, ended by
println(); - values separated by a comma, a space or a tab;
- an optional label in front of each value,
name:value, which becomes the legend.
The sketch above prints lines like level:55,half:27 about twenty times a second, and the
plotter draws two lines that rise and fall together. Its baud menu has the
same rule as the monitor's. The variables page
uses the monitor to watch a number go wrong.
The code
Upload it and open Tools, Serial Plotter at 9600 baud: two lines, one rising and falling between 0 and 100, the other half its height. Open the Serial Monitor instead and the same data is text.
// Print_And_Plot.ino — two numbers the Serial Plotter draws as lines
// Tools > Board: Arduino Uno, or Arduino Nano with
// Processor ATmega328P (Old Bootloader if the upload says
// "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor or Plotter: 9600 baud
// Wiring: none
int level = 0;
int step = 5;
void setup() {
Serial.begin(9600);
}
void loop() {
level = level + step;
if (level >= 100 || level <= 0) {
step = -step; // turn round at 0 and 100
}
Serial.print("level:");
Serial.print(level);
Serial.print(",half:");
Serial.println(level / 2); // println ends the line: one sample
delay(50);
}Each println() is one sample. The names before the colons become the plotter's legend; leave them out and the plotter makes up its own.
When it does not work
The monitor's baud menu does not match Serial.begin() in the sketch. Set the menu to the same number, 9600 for every sketch in this book. The board does not need re-uploading; the next line it prints comes out clean.
Check that the sketch calls Serial.begin() in setup(), that the right port is chosen in Tools, Port, and that the board is not still uploading. If the TX lamp flickers while the window stays blank, the bytes are leaving the board and the problem is the menu or the port.
Only one program can hold a serial port at a time. The Arduino IDE knows to let go of its own monitor during an upload, but another program with the port open, such as a second IDE window or a terminal, blocks it. Close it and upload again.
Every sample has to end with a line ending, so the last print of each sample must be println(). Values must be separated by a comma, a space or a tab, and any line of plain words in the stream gets in the way. Print only numbers and their labels while the plotter is open.
The types a sketch stores numbers in, how big each is on this chip, and what happens one past the top.
Variables, and why int stops at 32767 →Edit this page — content/books/atmega328p/the-serial-monitor.mdx
Discuss this article
Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.