Sub topic 3
Opening and using the Serial Monitor
This chapter assumes you can write a basic sketch with setup() and loop(), upload it from the Arduino IDE, and use Serial.begin() with a baud rate. Here you learn what the Serial Monitor does, how to open it, and what each control in it changes.
What the Serial Monitor is
The Serial Monitor is a small terminal built into the Arduino IDE. It opens the same serial port the IDE uses to upload sketches. It shows every byte the board sends and lets you send bytes back.
The Monitor does not know what your sketch means by its output. It only decodes the incoming bytes as text and displays them. Two things have to agree for that text to look right: the baud rate on both ends, and your own expectations about line endings.
Opening the Monitor
Before you open it, select your board and port under Tools. The Monitor connects to whichever port is selected there.
- In IDE 2.x, click the magnifier icon at the top right, or press
Ctrl+Shift+M(Cmd+Shift+Mon macOS). A Serial Monitor tab opens in the bottom panel next to the Output tab. - In IDE 1.x, use the magnifier icon, the same shortcut, or Tools > Serial Monitor. It opens in a separate window.
Upload this sketch first, then open the Monitor:
void setup() {
Serial.begin(9600);
Serial.println(F("Setup complete"));
}
void loop() {
Serial.println(millis());
delay(1000);
}Set the baud rate dropdown to 9600. You should see Setup complete, followed by a number that grows by about 1000 each line.
Why the board restarts when you open the Monitor
On an Uno, Nano, or Mega, opening the Monitor restarts the sketch. The IDE toggles a control line (DTR) when it opens the port. A capacitor on the board turns that toggle into a reset pulse. This is why Setup complete appears right after you open the Monitor, even if the sketch had been running for minutes.
This reset has two practical effects:
- Variables return to their starting values every time you open the Monitor.
- Output printed in
setup()appears reliably, because the Monitor is already listening by the time the restarted sketch gets there. Output printed before you opened the Monitor is lost.
Boards with native USB, such as the Leonardo, Micro, and Zero, usually do not reset when the Monitor opens. On those boards, text printed early in setup() can be lost because the computer has not connected yet. A common fix is to wait for the connection:
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for the USB serial connection (native USB boards only)
}
Serial.println(F("Connected"));
}
void loop() {
}On an Uno, Serial is always true, so the loop exits at once. Be careful, though. On a native USB board, this loop blocks forever if no Monitor is ever opened. Do not use it in a sketch that must run on its own, without a computer attached.
The controls
The controls differ slightly between IDE versions, but they cover the same functions.
| Control | What it does | Common mistake |
|---|---|---|
| Baud rate dropdown | Sets the speed the Monitor uses to read and send | Not matching the value in `Serial.begin()` |
| Line ending dropdown | Chooses what the Monitor appends to text you send | Leaving it on the wrong option for your parsing code |
| Autoscroll | Keeps the newest line visible | Turning it off and thinking the output stopped |
| Timestamp | Adds the computer's clock time to each received line | Mistaking it for the board's `millis()` time |
| Clear output | Erases the display only | Expecting it to affect the board |
Baud rate
The dropdown only affects the computer side. Changing it does not change the sketch. If the sketch calls Serial.begin(9600) and the dropdown says 115200, you see garbage characters or nothing at all. The fix is always to make both sides match. The next chapters cover choosing a rate, and the troubleshooting chapter covers garbled output in more depth.
Line ending
The line ending dropdown controls what the Monitor adds after the text you type when you press Enter or Send.
| Option | Bytes appended | Typical use |
|---|---|---|
| No line ending | None | Single-character commands |
| Newline | `\n` (LF) | Sketches that read until `\n` |
| Carriage return | `\r` (CR) | Some devices that expect CR |
| Both NL & CR | `\r\n` | Modules that expect CRLF, such as many AT-command devices |
The setting matters because the board receives exactly the bytes you send, including any extra ones. If you type 1 with Newline selected, the sketch receives two bytes: '1' and '\n'. A sketch that treats every unknown character as an error will complain about the newline. The input example below ignores \n and \r for this reason.
Timestamps and autoscroll
The timestamp option is added by the Monitor when a line arrives. It is useful when you want to see roughly when something happened, but it has jitter from USB and the operating system. For precise timing, print millis() from the sketch instead.
If you scroll up to read earlier output, autoscroll may pause or you may turn it off. New text then keeps arriving below the visible area. Turn autoscroll back on if the display looks frozen but the TX LED is still blinking.
Sending data to the board
The text field at the top of the Monitor sends data to the board. Press Enter or click Send. The Monitor does not echo what you typed unless your sketch prints it back.
This sketch reads one character at a time and reacts to 1 and 0:
const int LED_PIN = 13;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println(F("Send 1 to turn the LED on, 0 to turn it off"));
}
void loop() {
if (Serial.available() > 0) {
char c = Serial.read();
if (c == '1') {
digitalWrite(LED_PIN, HIGH);
Serial.println(F("LED on"));
} else if (c == '0') {
digitalWrite(LED_PIN, LOW);
Serial.println(F("LED off"));
} else if (c != '\n' && c != '\r') {
Serial.print(F("Unknown command: "));
Serial.println(c);
}
}
}Try it with No line ending and then with Both NL & CR. The behavior stays the same, because the sketch skips the extra bytes. If you remove the c != '\n' && c != '\r' test and choose Newline, every command is followed by Unknown command: and a blank line.
What happens between the sketch and the screen
Text takes several steps to get from Serial.println() to your screen. Knowing the path helps when output goes missing.
Each stage can fail on its own. A wrong baud rate corrupts the bytes between the UART and the interface. A closed or busy port stops them at the driver. A hidden line ending can make the Monitor look blank or stacked on one line.
Sharing the port
Only one program can use a serial port at a time. This has consequences:
- If another program holds the port, such as a terminal program or a second IDE window, the IDE reports that the port is busy and the Monitor cannot connect.
- Uploading a new sketch requires the port. IDE 2.x closes and reopens the Monitor around an upload. In IDE 1.x, close the Monitor window if the upload fails with a port error.
- If you unplug the board while the Monitor is open, the port disappears. After you plug the board back in, the port may come back under a different name, and you need to reselect it under Tools > Port.
A short checklist for a new Monitor session
- Select the correct board and port under Tools.
- Upload the sketch and wait for the upload to finish.
- Open the Monitor and set the baud rate to the value in
Serial.begin(). - Set the line ending to match what your sketch expects.
- Confirm that autoscroll is on.
- If you see nothing, close and reopen the Monitor, which also resets the board.
Exercise
Your sketch calls Serial.begin(115200) and prints a line every second. The Monitor shows unreadable symbols. You also notice the sketch restarts each time you open the Monitor. What do you change so the text is readable, and why does the restart happen on an Uno?
Show answer
Set the Monitor's baud rate dropdown to 115200 so it matches Serial.begin(). The sketch itself needs no change. The restart happens because opening the port toggles the DTR line, and the board's auto-reset circuit turns that toggle into a reset pulse.
Exercise
You type 1 into the Monitor's send field with "Newline" selected, and the LED sketch prints "LED on" and then "Unknown command:" followed by a blank line. Which part of the sketch normally prevents the second message, and why did it fail here?
Show answer
The test c != '\n' && c != '\r' normally ignores the line-ending bytes. If that test is missing or wrong, the '\n' byte that the Monitor appends after the 1 is read as a separate character and reported as an unknown command. The Monitor sends two bytes, '1' and '\n', so the sketch reads two characters.