Home / Learn Chip Programming / Debugging with printf, Logs and LED Codes
Debugging with printf, Logs and LED Codes
The simplest, always-available debugging tools and how to use them well.
Theory in plain words
What it is
Log messages with levels (ERROR, WARN, INFO, DEBUG), timestamps and module tags. Use LED blink codes when no console exists. Compile logs out in release builds.
Why we use it
Most bugs are found by watching the program's behaviour. Good logging also helps with field support.
How it works
Define LOG macros that print with level and time, buffer logs and send them from a low priority task, use a ring buffer for crash logs that survive reset.
Where it is used
- Daily debugging
- Field diagnostics
- RTOS timing problems
Log filter playground
Timestamps show ordering and delays. Levels keep the output readable.
#ifdef DEBUG
#define LOG_D(tag, fmt, ...) printf("[%lu] D %s: " fmt "\r\n", millis(), tag, ##__VA_ARGS__)
#else
#define LOG_D(tag, fmt, ...) ((void)0) // compiled out: zero cost
#endif
#define LOG_E(tag, fmt, ...) printf("[%lu] E %s: " fmt "\r\n", millis(), tag, ##__VA_ARGS__)
// LED error codes when no console: blink N times = error N
void blink_error(uint8_t n) { for (uint8_t i = 0; i < n; i++) { led(1); delay_ms(150); led(0); delay_ms(150); } delay_ms(1000); }- Do not print in ISRs; set a flag or write to a ring buffer.
- Add counters (retries, errors, overruns): numbers reveal intermittent problems.
- Keep one pin free for a scope or logic analyzer trigger.
- Store the last error code and reset reason in RTC or no-init RAM to read after a crash.
Quick quiz
1. Where not to printf?
It blocks and is slow.
2. Why timestamps in logs?
Needed to understand timing bugs.
Common questions
Where not to printf?
Inside ISRs. It blocks and is slow.
Why timestamps in logs?
Show ordering and delays. Needed to understand timing bugs.