A step-by-step method for finding a bug on an ESP32 microcontroller: describe, check the hardware, trace, isolate, read a crash.
Debugging an embedded program
A step-by-step method for finding a bug on an ESP32 microcontroller: describe, check the hardware, trace, isolate, read a crash.
Prerequisites:
Software:
Machines/Tools:
This guide applies when the program compiles and uploads, but the board does not do what you expect: nothing happens, the behaviour is wrong, or the board restarts in a loop. On a microcontroller, there is no error screen and no debugger at hand: the method matters more than the tool. Work from the simplest to the finest, changing only one thing at a time.
Before touching the code, write in one sentence what you expect and what you observe: “when I press the stick, the game should start; the screen stays on the home screen”. Also note when it happens: always, at start-up, after a few minutes, after a specific action.
A problem you can reproduce is already half solved. A problem that “happens sometimes” first requires finding what triggers it.
One bug in two is not in the code. Check in this order:
D5 and not 5, which means GPIO5)?The test programs of Lab 2 (The kit modules) check each module separately: if they work, the hardware is not to blame. For a board that no longer shows up or a display that stays black, see the guides Recovering an ESP32-S3 board that no longer shows up and Troubleshooting an SPI TFT display that shows nothing.
The serial monitor is your first debugging tool. Print what the program is doing, and above all the values it is handling:
void setup() {
Serial.begin(115200);
delay(1500); // lets the serial monitor reopen
Serial.println("Starting");
}
void loop() {
// ...
Serial.printf("[%lu ms] state=%d x=%.2f score=%d\n",
millis(), (int)game.state, joystickX, game.score);
}
A few rules for useful traces:
millis(): you see what happens when, and how often;loop() (otherwise the monitor scrolls too fast to read): for example only when the state changes;Serial.print() in an interrupt function: raise a flag there, and let loop() print;Reduce the problem to the smallest part that still shows it:
git diff shows what has changed since the last commit, and git stash sets your changes aside while you check that the old version works.loop()). If the bug disappears, it is in that half; otherwise, in the other. Start again on the right half.main.cpp that only does the thing causing trouble. If it works, the fault comes from an interaction with the rest of the program.State a hypothesis (“the button is read the wrong way round”), think of the test that would confirm or refute it (“print digitalRead() with the button released, then pressed”), run that test, then make only one change. Changing three things at once means never knowing which one solved the problem, or which one created another.
Write down your hypotheses and results in your logbook: it is the best way not to go round in circles, and it is exactly what is expected in the project documentation.
When the ESP32 crashes, it writes the cause to the serial monitor before restarting. Do not close the monitor: read the first line.
| Message | What it means | Common causes |
|---|---|---|
Guru Meditation Error: Core 1 panic'ed (LoadProhibited) or (StoreProhibited) |
Read or write at a forbidden address | Null pointer, array index out of bounds |
Stack canary watchpoint triggered (loopTask) |
The stack of loop() overflowed |
Large array declared in a function, recursion |
Task watchdog got triggered |
A task did not yield in time | while loop that never ends, busy waiting |
| Restarts in a loop with no readable message | Crash right at start-up | Error in setup(), insufficient power supply |
The crash comes with a Backtrace: line full of addresses. PlatformIO can translate it into function names and line numbers: add this filter to platformio.ini, then restart the monitor.
monitor_filters = esp32_exception_decoder
When you are stuck, a good question saves everyone time. It contains:
| Symptom | Likely cause | Solution |
|---|---|---|
| Nothing appears in the serial monitor | Speed different from Serial.begin(), messages sent before the port opened |
Same speed on both sides (115,200); delay(1500) after Serial.begin() |
| Unreadable characters in the monitor | Monitor speed different from the program’s | Set monitor_speed = 115200 in platformio.ini |
| The program works, then freezes after a while | Counter overflow, heap fragmentation, endless wait | Trace with millis() to date the freeze, then isolate (step 4) |
| A button counts several presses | Contact bounce | Debounce with millis() |
| A value changes “by itself” | Variable modified by an interrupt without volatile, or overwritten by an array that is too small |
volatile on shared variables; check array sizes |
| The behaviour changes when you add a trace | Timing problem (a trace slows the loop down) | Suspect a delay(), a wait or a time-dependent order of operations |