Sub topic 12
Turning Debug Output On and Off with Macros
Prerequisites
This chapter assumes that you can structure an Arduino sketch with setup() and loop(), upload a sketch, use Serial.begin(), and write basic if statements. You should also know that a macro is a preprocessor rule that changes source code before the compiler builds the sketch.
Debug output is information printed only to help you develop and test a sketch. It might show sensor values, state changes, or error messages. Debug messages are useful while a sketch is failing, but they consume program memory and can slow the sketch when many messages are sent.
Use a compile-time switch
A common pattern is to define DEBUG as either 1 or 0. Macros then expand debug calls into serial functions when debugging is enabled. When debugging is disabled, the calls expand into nothing.
#define DEBUG 1
#if DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
const int SENSOR_PIN = A0;
void setup() {
Serial.begin(9600);
DEBUG_PRINTLN(F("Debug output enabled"));
}
void loop() {
int reading = analogRead(SENSOR_PIN);
DEBUG_PRINT(F("Reading: "));
DEBUG_PRINTLN(reading);
delay(500);
}With DEBUG set to 1, the preprocessor changes DEBUG_PRINTLN(reading) into Serial.println(reading). With DEBUG set to 0, it removes the statement before compilation. The production sketch therefore does not spend time sending those messages.
Keep debug messages useful
Use labels that identify the value or event. A bare number can be difficult to interpret when several values are printed.
#define DEBUG 1
#if DEBUG
#define DEBUG_PRINT(x) Serial.print(x)
#define DEBUG_PRINTLN(x) Serial.println(x)
#else
#define DEBUG_PRINT(x)
#define DEBUG_PRINTLN(x)
#endif
void setup() {
Serial.begin(115200);
}
void loop() {
int sensor = analogRead(A0);
bool buttonPressed = digitalRead(2) == LOW;
DEBUG_PRINT(F("sensor="));
DEBUG_PRINT(sensor);
DEBUG_PRINT(F(" button="));
DEBUG_PRINTLN(buttonPressed ? F("pressed") : F("released"));
delay(250);
}The F() macro stores constant text in flash memory on boards such as the Uno instead of copying it into scarce SRAM. Use it for fixed debug labels and messages.
| Setting | Result |
|---|---|
| `#define DEBUG 1` | Debug serial calls are compiled in |
| `#define DEBUG 0` | Debug serial calls are removed |
Important macro limitations
Macros perform text substitution. They do not have the type checking or scope rules of ordinary functions. Keep macro arguments simple, and do not pass expressions with side effects, such as DEBUG_PRINTLN(counter++), because behavior can become confusing if a macro is later changed.
For more complex logging, wrap the behavior in a regular function. For simple Arduino messages, DEBUG_PRINT() and DEBUG_PRINTLN() are usually sufficient.
Exercise
What does changing #define DEBUG 1 to #define DEBUG 0 do to the debug calls in the sketch?
Show answer
The preprocessor replaces the debug macros with empty statements, so their serial output is not compiled into the sketch.