The Serial Monitor and Debugging
Send text and sensor values from the board to your computer with Serial, read the Serial Monitor and Plotter, and track down bugs methodically.
Welcome back!
Step 1 of 11Learn
Your board can talk back
An Arduino has no screen, so how do you know what your code is thinking? You make it send text to your computer over the USB cable using serial communication.
void setup() {
Serial.begin(9600); // start serial at 9600 baud
Serial.println("Ready");
}
void loop() {
int raw = analogRead(A0);
Serial.print("Light: ");
Serial.println(raw);
delay(200);
}Open Tools > Serial Monitor and lines like "Light: 512" scroll past five times a second.
Serial.begin(9600)sets the speed in baud (bits per second). The Serial Monitor must be set to the same speed, using the dropdown in its corner. Common values are 9600 and 115200.Serial.print()sends text without a line break;Serial.println()adds one.- Text in double quotes is printed literally; a variable name prints its value.
If the monitor shows garbage like "ÿ⸮⸮x", the baud rates don't match. That one problem accounts for a large share of "my Arduino is broken" posts. Fix the dropdown, and the text appears.
Step 2 of 11Practice · Fill in the blanks
Fill in the blanks
If Serial.begin(115200) is in the sketch but the Serial Monitor is set to 9600, the monitor shows because the baud rates do not match.
Step 3 of 11Learn
Debugging with print statements
Serial printing is the main debugging tool for most Arduino developers. The method:
- Decide what you expect. "When I press the button, pressCount should go up by one."
- Print the evidence right where it matters: the raw reading, the variable, a message when a branch runs.
- Compare what you see with what you expected. The first place they differ is where the bug lives.
int raw = digitalRead(BUTTON_PIN);
Serial.print("raw=");
Serial.print(raw);
Serial.print(" count=");
Serial.println(pressCount);Printing a short label before every value (raw=, count=) keeps the output readable when you print several values on one line.
The Arduino IDE also has a Serial Plotter (Tools > Serial Plotter) that draws numbers as live graphs. Print one value per line, or several separated by spaces or commas on one line, and each becomes its own colored trace. It's excellent for seeing sensor noise, a slow temperature drift, or exactly when a button bounces.
Two cautions: printing takes time (at 9600 baud, each character takes about 1 ms), so heavy printing can slow a fast loop. And remove or reduce debug prints once a project works.
Step 4 of 11Try the calculator
How long does a print take?
Serial sends about 10 bits per character (8 data bits plus start and stop bits).
- Time per message
- 20.83 ms
- Maximum messages per second
- 48
Step 5 of 11Practice · Work it out
At 9600 baud with about 10 bits per character, roughly how many milliseconds does it take to send a 24-character line?
Step 6 of 11Tap to reveal
Decoding common compiler errors
"expected ';' before …"
A missing semicolon, usually at the end of the line above the one reported.
"… was not declared in this scope"
A misspelled name (pinmode instead of pinMode), a variable used outside the block where it was declared, or a library that was not included.
"expected '}' at end of input"
An opening brace was never closed. Use Auto Format and look for where the indentation stops making sense.
"redefinition of …"
Two variables or functions share the same name, often from pasting code twice.
"Low memory available, stability problems may occur"
Your variables are close to filling the 2 KB of SRAM. Shrink arrays, use smaller types, and wrap constant text in F(), as in Serial.println(F("Ready")), which keeps it in flash.
Step 7 of 11Tap to reveal
A methodical debugging checklist
1. Power and connections
Is the board powered (ON LED lit)? Is GND shared between the Arduino and every module? Are jumpers fully seated and in the rows you think?
2. The right pin
Does the pin number in the code match the physical wire? Is pinMode set? Is the part on a PWM pin if you use analogWrite?
3. Print the input
Print the raw sensor or button value. If it never changes, the problem is wiring or the sensor, not your logic.
4. Print the decision
Print a message inside each if branch. If the expected branch never runs, check the condition, the comparison operator and the threshold.
5. Test the output alone
Write a three-line sketch that only turns the output on. If that fails, the issue is the output circuit.
6. Change one thing at a time
Make one change, upload, observe. Changing three things at once hides which one mattered.
Step 8 of 11Practice · What would you do?
What would you do?
Kofi's temperature fan never switches on. He prints the temperature and sees sensible values like 31.2 to 32.0, above his 30-degree threshold. What is the most useful next step?
Step 9 of 11Practice · Put in order
Put these debugging steps in a sensible order for a sensor project that "does nothing".
Drag the items, or use the arrow buttons, to put them in order.
- Fix the single cause you found and retest
- Print the raw sensor reading
- Print which branch of the logic runs
- Check power, ground and wiring
- Test the output device with a minimal sketch
Step 10 of 11Note
Step 11 of 11Practice · Choose one
Which line prints "Temp: 23.5" followed by a new line, given float t = 23.5?
Lesson complete!