The code uploaded, and nothing works. The LED won't light, and the sensor won't read. You can't see inside the Arduino. The Serial Monitor is the window you look through: you make the board tell you where it is and what it's reading.
Getting started
void setup() {
Serial.begin(9600);
Serial.println("setup done");
}
void loop() {
int v = analogRead(A0);
Serial.print("A0 = ");
Serial.println(v);
delay(500);
}
Open Tools → Serial Monitor (or the icon at the top right), and choose the same speed as in the code (9600).
print: prints without a new line.println: prints and moves to a new line.
All of this runs over a protocol called UART, covered in detail in the UART, I2C and SPI article. The board has a chip that converts it to USB so the computer understands it.
Strange characters?
If you see things like ⸮⸮⸮, then the speed (baud) in the monitor doesn't match the one in Serial.begin. Make them the same. With the ESP32 the usual rate is 115200, and as the board starts it prints some garbage characters, which is normal (boot messages at a different speed).
Which speed is best? 9600 is enough for a few messages. If you print a lot or quickly, use 115200: printing finishes faster and the program waits less.
Printing in different formats
int x = 202;
float t = 23.456;
Serial.println(x, BIN); // 11001010
Serial.println(x, HEX); // CA
Serial.println(t, 1); // 23.5 (one digit after the point)
Serial.println(t, 3); // 23.456
- BIN is useful when you read a register from a sensor and want to see each bit.
- HEX is useful for I2C addresses and remote-control codes.
- A
floatprints two digits after the point unless you say otherwise.
If you want to print a line with several values at once, the ESP32 has Serial.printf:
Serial.printf("t=%.1f h=%d%%\n", t, h);
On the Uno, use snprintf into a buffer and then print it. Note that snprintf on the Uno doesn't print a float, so print it separately with Serial.print(t, 1).
Timestamps
To know when things happen and how long they take:
Serial.print(millis());
Serial.print(" ms: ");
Serial.println("sensor read");
If there are 1000 ms between two messages and you expected 100, something is stalling the program: a forgotten delay, or a library waiting for a sensor. See millis instead of delay.
The debugging method
Put messages in the important places:
Serial.println("before sensor read");
float t = readTemp();
Serial.print("t = "); Serial.println(t);
if (t > 30) {
Serial.println("fan ON");
digitalWrite(FAN, HIGH);
}
- The message didn't appear? The code never got here. Something before it is stuck (for example a library waiting for a sensor that isn't connected).
- The value is strange? The problem is in the reading or the wiring, not the logic.
- "fan ON" appeared but the fan isn't running? The problem is in the hardware: a transistor, the supply, or an open connection.
It's the same tracing logic as with a multimeter, but for code: split the path in half, and see which half the fault is in.
The board keeps resetting?
If you see setup done repeating every so often without you pressing anything, the board is restarting. Common causes: the supply drops when a motor or relay switches on, or memory runs out on the Uno. On the ESP32, the message itself tells you the cause: Brownout detector was triggered means the supply is weak, and Guru Meditation Error means the code did something wrong, such as reading from a location that doesn't exist.
The Serial Plotter
Tools → Serial Plotter draws the numbers as a graph. Excellent for sensors:
Serial.print(a); Serial.print(" "); Serial.println(b);
Each number on the line becomes a trace. You'll see noise and jitter with your own eyes. You can also name the traces:
Serial.print("raw:"); Serial.print(raw);
Serial.print(",filtered:"); Serial.println(filtered);
The graph then shows each trace's name. That's exactly what you need when tuning a filter or a switching threshold for a sensor such as a soil moisture sensor: the raw and filtered readings side by side.
Sending commands to the Arduino
if (Serial.available()) {
char c = Serial.read();
if (c == '1') digitalWrite(13, HIGH);
if (c == '0') digitalWrite(13, LOW);
}
Type in the box at the top of the Serial Monitor and press Enter.
Longer commands
If you want to send a command such as pwm 128:
if (Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd.startsWith("pwm ")) {
int v = cmd.substring(4).toInt();
analogWrite(9, v);
Serial.print("pwm set to "); Serial.println(v);
}
}
Set the line ending in the Serial Monitor to Newline (the menu at the bottom next to the speed). This is very useful while experimenting: you change a motor speed or a sensor threshold without uploading the code each time.
Switch off debug messages with one line
Once the project is finished, the messages cost time and memory. Instead of deleting them one by one:
#define DEBUG 1
#if DEBUG
#define DBG(x) Serial.print(x)
#define DBGLN(x) Serial.println(x)
#else
#define DBG(x)
#define DBGLN(x)
#endif
// in the code:
DBG("t = "); DBGLN(t);
Change DEBUG to 0, and all the messages disappear from the uploaded code.
Watch out
- On the Uno, Serial uses pins 0 and 1. If something is connected to them, uploading and printing may break.
- Printing takes time. Lots of printing inside a fast loop or inside an interrupt slows the program.
- Constant strings eat RAM on the Uno. Use
Serial.println(F("text"))so they stay in flash. - Boards such as the Leonardo need
while (!Serial);so you don't miss the first messages. - The Serial Monitor and uploading don't mix in some setups: if another program has the port open (such as the Plotter open in another window), the upload will fail.
Recurring problems
| Problem | Usual cause |
|---|---|
| Nothing appears at all | You forgot Serial.begin, or chose the wrong port |
| Strange characters | The speed differs between the code and the monitor |
| The upload says the port is busy | The Serial Monitor or another program has it open |
| The messages keep starting over | The board is resetting: supply or memory |
| The command you sent isn't executed | The line-ending setting doesn't match what the code expects |
FAQ
Why does the board reset when I open the Serial Monitor? That's normal on the Uno: opening the port triggers a reset so you see the messages from the beginning.
Can I see the messages without the computer? Yes, on an LCD or OLED display, or over Bluetooth from the ESP32 to a phone.