A watchdog is a timer that resets the MCU unless the firmware keeps telling it things are fine. On an unattended device it is the last defence against a hang. It is also easy to configure so it never fires, or so it fires but hides the actual fault.
Hardware watchdog versus task watchdog
A hardware watchdog runs from its own clock and keeps counting even if the CPU is stuck, interrupts are disabled or the clock tree has failed. STM32 parts have an independent watchdog (IWDG) on the low-speed internal oscillator, and a window watchdog (WWDG) tied to the main clock. ESP32 has an interrupt watchdog, a task watchdog and RTC watchdog timers.
A task watchdog is software, usually watching FreeRTOS tasks. It is good at finding which task is stuck. But it depends on the scheduler, so a fault that kills the scheduler also kills it. Use both: the task watchdog to point at the culprit, the hardware watchdog as the backstop.
Where to feed it
The classic mistake is feeding the watchdog from a timer interrupt or from a dedicated “kick” task. The interrupt keeps running while the application is dead, so the dog is fed forever and never bites.
Instead, feed it from one place that is reached only when the whole system has made progress. A common pattern is a supervisor task: each worker task sets a flag or event bit when it completes its loop, and the supervisor feeds the hardware watchdog only when all expected flags are set within the period. A task that blocks on a dead network or an I2C bus that never answers then stops the feed.
Timeout selection
Pick the timeout from the slowest legitimate operation, not from the fastest. A cellular modem attach or an OTA flash erase can take many seconds, so those paths need either a longer budget or a feed during the wait. Typical values are 5 to 30 seconds for the hardware watchdog on a connected device. Too short and normal delays trigger resets. Too long and a hang looks like an outage.
Use a window watchdog where you also want to catch code that runs too fast, such as a loop that has skipped its work.
Log the cause
A clean restart that tells you nothing is half a fix. At boot, read the reset reason flags, and if it was a watchdog, record it. Before the reset, if you can, save which task failed to check in, in a few bytes of retained memory. Send that with the first message after boot. A device that resets by watchdog twice a day has a firmware bug to find.
What it does not fix
A watchdog restarts the device. It does not fix a memory leak, a bad power supply or a sensor stuck in a bad state that returns on every boot. Limit restart loops: after a few consecutive watchdog resets, boot into a safe mode that reports the fault and waits.
If your devices hang in the field and you cannot say why, book a discovery call, and read about our embedded firmware work.