Watchdog Timer
This article details the implementation of a Task Watchdog Timer (TWDT) using an ESP32 IoT Programmable Controller, highlighting its significance in maintaining system stability by resetting at intervals and demonstrating the consequences of overflow.
Watchdog Timer
Example: Task Watchdog Timer (TWDT)
In this example, the watchdog timer is set with an overflow time of 3 seconds. For the first 10 seconds, the watchdog is reset every 2 seconds. After 10 seconds, the watchdog is no longer reset, causing the watchdog timer to overflow and the board to reset.
Hardware Required:
- ESP32 IoT Programmable Controller (SKU: DFR0886)×1
Example Code:
#include <esp_task_wdt.h>
// 3-second Watchdog Timer (WDT)
#define WDT_TIMEOUT 3
void setup() {
Serial.begin(115200);
Serial.println("Configuring WDT...");
esp_task_wdt_init(WDT_TIMEOUT, true); // Enable panic, ESP32 auto-restarts on WDT timeout
esp_task_wdt_add(NULL); // Add the current thread to WDT monitoring
}
int i = 0; // WDT reset counter
int last = millis(); // Record last reset time
void loop() {
// Reset WDT every 2 seconds, stop after 5 resets
if (millis() - last >= 2000 && i < 5) {
Serial.println("Resetting WDT...");
esp_task_wdt_reset(); // Reset watchdog timer
last = millis(); // Update reset time
i++; // Increment counter
// Stop resetting after the 5th reset, wait for WDT timeout and reboot
if (i == 5) {
Serial.println("Stopping WDT reset, CPU will reboot in 3 seconds");
}
}
}
Was this article helpful?
