ABC of Electronics cosycom.com
ARDUINO 09 · Debugging

The Serial Monitor: Debugging with Serial.print

The single most useful debugging tool for a beginner: printing what your sketch is actually doing, in plain text, while it runs.

You'll need: just your Uno and USB cable — this lesson is entirely about the software side.

When a sketch doesn't behave as expected, guessing at the cause wastes time that printing the actual values almost always saves. The Serial Monitor is a simple text window built into the IDE that displays whatever your sketch sends back over the same USB cable used to upload it.

void setup() {
  Serial.begin(9600);
}

void loop() {
  int reading = analogRead(A0);
  Serial.print("A0 reading: ");
  Serial.println(reading);
  delay(200);
}

Serial.begin(9600), called once in setup(), opens the serial connection at a chosen speed (9600 bits per second — the same baud-rate concept from Lesson 32's UART theory; this number just needs to match what you select in the Serial Monitor window's dropdown, or you'll see garbled text). Serial.print() sends text without a line break, and Serial.println() sends text followed by one — useful for labeling a value, as shown above.

Open it via Tools → Serial Monitor (or the magnifying-glass icon, top right of the IDE) any time your sketch is uploaded and running. Sprinkling Serial.println() calls at key points in a sketch — right after reading a sensor, right before an important decision — turns an invisible, silently-running program into one you can actually watch think.

abcofelectronics
Serial Monitor window Each Serial.println() call adds one more line here, live, while your sketch runs — a direct window into what the board is actually doing.
TRY THIS
Before assuming a bug is complicated, add a Serial.println() right where you're unsure what's happening — seeing the actual value usually points straight at the cause.