Solving Arduino Bootloop from Watchdog Reset
What causes this
When an Arduino experiences a bootloop resulting from a watchdog reset, it likely stems from a task taking longer than expected, causing the watchdog timer to expire. The watchdog timer is a safety mechanism designed to reset the microcontroller if the software hangs or runs into an infinite loop. On the ESP32, for instance, the watchdog timer is controlled by the WDT_WPROTECT_REG and WDTCONFIG0_REG registers. If the watchdog is not reset in time by the application, it triggers a reset, resulting in a continuous bootloop. This is often accompanied by error codes such as 0x03, which indicate a core panic caused by a watchdog timeout.
Minimal reproduction
To reproduce the issue, consider the following code snippet that deliberately stalls the CPU:
```cpp
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Starting bootloop test");
delay(1000); // Simulate a normal delay
}
void loop() {
// Infinite loop causing a watchdog reset
while (true) {
// Simulate a task that never completes
}
}
```
Expected serial output before the reset:
```
Starting bootloop test
Guru Meditation Error: Core 1 panic'ed (Interrupt wdt timeout on CPU1)
Core 1 register dump:
PC : 0x400d1234 PS : 0x00060034 A0 : 0x800d5678 A1 : 0x3ffb1f00
...
```
The fix
To fix this bootloop, ensure that your code periodically resets the watchdog timer within the loop. Modify the code as follows:
Before:
```cpp
void loop() {
// Infinite loop causing a watchdog reset
while (true) {
// Simulate a task that never completes
}
}
```
After:
```cpp
#include
void loop() {
// Reset the watchdog timer periodically
while (true) {
esp_task_wdt_reset();
delay(10); // Allow some breathing space for the watchdog
}
}
```
Additionally, you can increase the watchdog timeout period in your configuration if necessary, but this should be done with caution as it reduces the safety net.
How SerialDoctor catches this
SerialDoctor leverages the Web Serial API to analyze the serial output from your Arduino board. It instantly identifies the root cause of bootloops by parsing error messages and decoding register dumps, like those from the watchdog timer. In under three seconds, SerialDoctor pinpoints issues such as watchdog timeouts and suggests precise fixes. Visit [serialdoctor.com](https://serialdoctor.com) for more information.
Quick checklist
- [ ] Verify that all loops have a termination condition or a watchdog reset.
- [ ] Ensure that any blocking operations have a timeout or periodic exit.
- [ ] Use
esp_task_wdt_reset()in any long-running tasks. - [ ] Consider increasing the watchdog timeout in configuration if justified.
- [ ] Regularly test your application with SerialDoctor to catch potential issues early.
Seeing this crash on your board right now?
Connect it to SerialDoctor and get a root cause + code fix in under 3 seconds.
Try SerialDoctor free →