Sub topic 7
Tracing Your Sketch with Debug Prints
Assume you know the Arduino setup() and loop() functions, can upload a sketch, and can use variables, if statements, analogRead(), and digitalWrite(). This chapter uses the Serial Monitor to observe what your sketch is doing while it runs.
A debug print is a message sent from the Arduino to your computer to show the sketch's internal state. The internal state is the current set of variable values and conditions. Debug prints help you find where execution differs from what you expected.
Add a Useful Trace
Start serial communication in setup() with Serial.begin(). Then print a label beside each important value. A label tells you what the number means.
const int SENSOR_PIN = A0;
const int LED_PIN = 13;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
Serial.println(F("Sketch started"));
}
void loop() {
int sensorValue = analogRead(SENSOR_PIN);
bool sensorHigh = sensorValue > 600;
Serial.print(F("sensor="));
Serial.print(sensorValue);
Serial.print(F(" high="));
Serial.println(sensorHigh ? F("true") : F("false"));
digitalWrite(LED_PIN, sensorHigh ? HIGH : LOW);
delay(500);
}The conditional operator, written as condition ? value1 : value2, selects one of two values. Here it prints readable Boolean values instead of 1 or 0.
Serial.print() leaves the cursor on the same line. Serial.println() prints a value and then starts a new line. Several print() calls can build one complete record before println() ends it.
Open the Serial Monitor after uploading. Select the same baud rate used by Serial.begin(), which is 9600 in this example. You should see one record every half second.
Trace Decisions and Timing
Print a message immediately before or after a decision when you need to know which branch ran.
const int BUTTON_PIN = 2;
const int LED_PIN = 13;
void setup() {
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
}
void loop() {
bool pressed = digitalRead(BUTTON_PIN) == LOW;
Serial.print(F("time="));
Serial.print(millis());
Serial.print(F(" pressed="));
Serial.println(pressed ? F("yes") : F("no"));
if (pressed) {
digitalWrite(LED_PIN, HIGH);
Serial.println(F("action: LED on"));
} else {
digitalWrite(LED_PIN, LOW);
Serial.println(F("action: LED off"));
}
delay(100);
}millis() returns the number of milliseconds since the board started running the sketch. Adding it to a trace shows when an event occurred. This can reveal unexpected delays or a loop that runs more often than intended.
A good trace answers three questions:
- What event or loop iteration produced this message?
- Which values influenced the decision?
- What action did the sketch take?
Avoid Trace-Related Problems
Serial output takes time. Printing inside a fast loop can slow the sketch and change the behavior you are trying to observe. Print at a controlled interval, or print only when a value or state changes.
Long constant strings also use memory. On small AVR-based boards, wrapping constant text in F() keeps it in program memory instead of copying it into RAM.
If the output is unreadable, check the baud rate first. If the output stops, confirm that the sketch reaches the print statement and that the correct board port is selected.
Exercise
A trace repeatedly prints sensor=0, even though the sensor changes. What should you check first?
Show answer
Check the sensor wiring and pin number, then verify that the sketch reads the expected analog pin and that the Serial Monitor baud rate matches Serial.begin().