How To Enable Hardware WDT On ESP32 Using Arduino IDE - Code

Now let’s look at the code. First of all you need to include the esp_task_wdt.h header. This should be available if you have properly installed arduino-esp32.

In the setup block we need to call two functions:

esp_task_wdt_init(uint32_t timeoutSeconds, bool panic) and esp_task_wdt_add(TaskHandle_t handle).
  • esp_task_wdt_init is used to initialise WDT with a timeout of timeoutSeconds and with a panic mode set. If panic is set to true, when WDT times out, it will throw a hardware panic and reboot.
  • esp_task_wdt_add is used to add a task to WDT. If handle is NULL, the current task is used.

Now, the watchdog timer need to be reset BEFORE it times out! This is done with

esp_task_wdt_reset() executed in the current task.

You can see that in this example I am initialising the WDT with a timeout of 3 seconds and then inn the main loop I am resetting it every 2 seconds. After five resets, I stop the reset call and let it timeout. As expected, it reboots my ESP32 after 3 seconds.

Tag » Arduino Wdt.h Download