Serial Monitor and debugging with Serial
The Serial Monitor shows text the Arduino sends over USB. Lab 2a uses it to display values; it is also your best debugging tool, and it is the same channel the Raspberry Pi reads in Lab 5c.
What "serial" and "baud" mean
The Uno sends text one bit at a time over the USB cable. Both ends must agree on the speed, the baud rate (bits per second). Serial.begin(9600) in the sketch and 9600 in the Serial Monitor's dropdown must match, or you get garbage like ⸮⸮⸮x⸮.
print vs println
| Code | Output |
|---|---|
Serial.print("Temp="); then Serial.println(25.3); | Temp=25.30 on one line |
Serial.println("Temp="); then Serial.println(25.3); | Temp= and 25.30 on two lines |
Serial.println(25.3456, 1); | 25.3 (1 decimal; the default is 2) |
Serial.println(x, BIN); | The number in binary |
The format matters when another program reads it: the Pi's Lab 5c code expects exactly Dist=... and Temp=..., one per line.
Debugging with Serial
When something does not work, print what the Arduino actually sees:
Serial.print("raw=");
Serial.print(analogRead(A0));
Serial.print(" button=");
Serial.println(digitalRead(2));
- Value never changes → wrong pin, or the part is not connected.
- Value jumps randomly → floating input or loose wire.
- Nothing printed after a certain line → the code is stuck before it (an endless
while, or a sensor library waiting).
Print labels, not just numbers, or you cannot tell which number is which.
Serial Plotter
Tools → Serial Plotter draws numbers as a live graph. Print numbers separated by spaces or commas, one set per line:
Serial.print(temperature);
Serial.print(",");
Serial.println(distance);
Great for seeing noise, trends and whether a sensor reacts at all.
Sending to the Arduino
The input box at the top sends text to the Arduino:
if (Serial.available() > 0) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();
if (cmd == "on") digitalWrite(13, HIGH);
}
Set the line-ending dropdown to Newline so readStringUntil('\n') sees the end.
Things nobody tells you
- Opening the Serial Monitor resets the Uno. The sketch starts again from
setup(). That is why the first line is sometimes cut off, and why counters restart. - Only one program can use the port. While the Serial Monitor is open, uploads fail and the Pi's Python cannot open the port.
- Printing too much slows the sketch down. At 9600 baud, about 960 characters per second fit. Printing a long line every loop without a delay makes the whole sketch lag.
- Pins 0 and 1 are the serial line. Anything wired to them interferes with both uploading and the Serial Monitor.
Problems
| Problem | Fix |
|---|---|
| Garbage characters | Baud rate in the monitor does not match Serial.begin. |
| Nothing appears | Serial.begin missing; wrong port; sketch not uploaded; the code never reaches the print. |
| Everything on one long line | print used where println was needed. |
| Upload fails while the monitor is open (IDE 1.x or a second window) | Close the monitor, upload, reopen. |
Pi says Device or resource busy | Close the Serial Monitor (or unplug from the PC) before the Pi reads it. |