Sub topic 3
Opening and Using the Serial Monitor
This chapter assumes you can upload an Arduino sketch, use setup() and loop(), and understand variables, if statements, and Serial.begin(). You also need a board connected to your computer through USB.
The Serial Monitor is a window in the Arduino IDE that displays text sent by the board. It can also send text back to the board. This makes it useful for checking variable values, confirming that code reaches a particular branch, and entering simple commands.
Open the Serial Monitor
First, select the correct board and port in the Arduino IDE. Upload a sketch that starts serial communication:
void setup() {
Serial.begin(9600);
Serial.println("Board started");
}
void loop() {
Serial.println("Loop is running");
delay(1000);
}Open the Serial Monitor from the IDE's Tools menu, or use its toolbar button. The monitor normally has these controls:
- A text area showing received data.
- A field where you can enter data to send.
- A line-ending selector.
- A baud-rate selector.
- An option to keep the view scrolled to the newest line.
Set the baud rate to 9600, matching the argument passed to Serial.begin(9600). A baud rate is the number of symbols sent per second. Both sides must use the same rate.
Read output and send input
Serial.println() sends text followed by a newline. The newline moves the next message to a new row. Serial.print() sends text without moving to a new row.
The monitor's line-ending setting controls what is appended when you send text. Common choices are:
- No line ending
- Newline
- Carriage return
- Both newline and carriage return
Choose the setting expected by your sketch. For example, code that reads one character at a time should ignore '\n' and '\r' when the monitor sends both.
The board may reset when you open the monitor, especially on boards that use USB-to-serial control signals. Therefore, the first startup message may appear before you open the window. Press the board's reset button or upload the sketch again if you need to observe startup output.
> callout note > If the text is readable but appears too quickly, enable automatic scrolling only when needed. For long logs, turning it off can make it easier to inspect earlier messages.
Match the settings
| Sketch setting | Serial Monitor setting | Result |
|---|---|---|
| `Serial.begin(9600)` | `9600` baud | Readable text |
| `Serial.begin(115200)` | `9600` baud | Garbled text |
| Reads a line ending | Matching line-ending option | Complete command received |
If the monitor is blank, check that the sketch actually calls Serial.println(), the selected port is correct, and the board is running the uploaded sketch. Close other programs that may already have the port open. If no port appears, reconnect the USB cable and check whether it supports data rather than charging only.
Exercise
Exercise
A sketch uses Serial.begin(115200), but the Serial Monitor shows unreadable symbols. What should you change?
Show answer
Set the Serial Monitor baud rate to 115200, then reset the board if necessary so that startup output is visible.