Last Updated : Nov ,2026
Hardware vs. Software Watchdogs: Designing Self-Healing IoT Devices
How commercial products recover from hard faults, brownouts and infinite loops in the field, with nobody around to press reset.
A customer of ours ran about four hundred soil sensors across farmland in three states. One of them stopped reporting. Not crashed, not offline. It kept sending a perfect heartbeat every fifteen minutes. It just never sent any sensor data again. The firmware had wedged itself into a state where the network task was alive and the sensor task was dead, and nothing noticed.
Somebody had to drive two hours to pull the battery.
That sensor had a watchdog. It was enabled. It did nothing, because it was being fed by the one part of the system that still worked. That is the story of almost every watchdog failure I have seen: the watchdog was there, and it was useless.
The core idea
A watchdog is a countdown timer that resets the device unless your code keeps proving it is alive. Your firmware "kicks" the timer before it expires. Miss the deadline and the watchdog pulls the reset line. That's it.
The hard part is not turning it on. The hard part is deciding what counts as proof of life. Get that wrong and you get a device that reports itself healthy while doing nothing useful.
Two kinds, and you need both
A hardware watchdog is a timer outside your program. On STM32 it is the IWDG, on ESP32 the RTC watchdog, on AVR the WDT. It runs from its own low-speed clock, so it keeps counting even if the main clock dies or the core halts. On many chips, once you enable it, software cannot turn it off again until the next power cycle. That is the point. A watchdog that a bug can disable is not a watchdog.
It catches hard faults, infinite loops, deadlocks and clock failures. It does not catch a task that is running but doing the wrong thing, or one dead thread when another thread keeps kicking.
A software watchdog is your own code watching the rest of your code. It has no special powers, but it has context. Every important task gets an "alive" flag that it sets only when it finishes a full cycle of real work. A supervisor task checks all the flags, clears them, and only then kicks the hardware watchdog. If any task missed its deadline, the supervisor refuses to kick, the hardware timer expires, and the device resets.
The supervisor decides whether the system is healthy. The hardware timer enforces that decision whether or not the CPU cooperates.
Linux devices use the same split at a bigger scale: /dev/watchdog is backed by a hardware timer in the SoC, systemd supervises individual services with WatchdogSec=, and systemd itself kicks the hardware via RuntimeWatchdogSec=. Service, init, silicon.
The mistake that turns a watchdog into decoration
Kicking the watchdog from a timer interrupt. It feels tidy, and it is the single most common way to break the whole mechanism, because now the watchdog only proves the timer peripheral works. The main loop can spin forever on a corrupted pointer while the kicks keep flowing.
// WRONG: only proves the timer works
void TIM2_IRQHandler(void) {
WDT_Kick(); // runs even if main() hung
}
// RIGHT: the kick is earned, not free
void supervisor_task(void) {
for (;;) {
sleep_ms(500);
if (alive[SENSOR] && alive[RADIO] &&
alive[STORAGE] && alive[CTRL]) {
memset(alive, 0, sizeof alive);
WDT_Kick();
}
// else: say nothing. Let it expire.
}
}
Some chips also offer a windowed watchdog (WWDG on STM32). It resets the device if you kick too early as well as too late. That catches the runaway case where a corrupted program counter lands in a tight loop that happens to include the kick instruction.
Brownouts: the fault nobody plans for
Infinite loops are dramatic. Brownouts are sneaky, and on battery-powered devices they are far more common. The radio transmits, current spikes, the battery voltage sags for a few milliseconds. The core keeps running, but flash and RAM are now out of spec. A flash write gets corrupted, or a register read returns garbage. Then the voltage recovers and the chip carries on from a corrupted state with no idea anything happened.
Simulated rail on a cold, half-empty cell during three BLE-style transmit slots. Without the capacitor the rail dips below the flash-write minimum but stays above the factory-default brownout level, so nothing resets. The thresholds are illustrative; take the real numbers from your MCU and flash datasheets.
Three things fix this:
- Set the brownout detector above the flash-write minimum. Almost every MCU has one. Most ship with it at the lowest level or off.
- Never write critical data without a second copy. Two config slots, each with a CRC and a sequence number. A brownout mid-write kills one; the other survives.
- Put bulk capacitance next to the radio and measure the battery before you transmit, not after.
External supervisor on a battery node. The WDI pin is driven by a GPIO the supervisor task toggles on purpose, RESET is latched and active-low, and the bulk cap sits on the rail the radio pulls from.
Reset is not recovery
Resetting the chip is step one. If the bug is deterministic, you reset straight back into it and boot-loop until the battery dies. If a bad firmware update caused it, every reset lands in the same bad firmware. And if you never record why you reset, you have learned nothing.
Every box on the right is something the device does differently from last time. Repeating the same boot and hoping is not a strategy.
Products that survive wrap the watchdog in four habits:
- Read the reset-cause register on every boot. Log it. Send it home. One byte in every heartbeat tells you more about field reliability than a month of lab testing.
- Keep a boot counter in non-volatile memory. Clear it only after the device has run healthy for a meaningful period. When it hits three without clearing, do something different: safe mode, config wipe, rollback, or deep sleep with back-off.
- A/B firmware with a confirmation window. New firmware boots into the inactive slot and has a few boots to call
confirm(). If it hangs, the watchdog resets, the bootloader counts the failure, and flips back. ESP-IDF, MCUboot, SWUpdate, RAUC and Mender all ship this. Turn it on and test the failure path. - Leave a crash breadcrumb in no-init RAM. Program counter, link register, task ID. Read it on the next boot. Now a watchdog reset comes with an address attached.
What it looks like in shipped products
Smart meters sit on a wall for twenty years. External supervisor, windowed watchdog, boot counter in EEPROM, double-buffered metrology journal. A few resets a year is normal. A boot loop means a truck roll that costs more than the meter, so the safe-mode logic is taken seriously.
Mars Pathfinder, 1997. A priority inversion between three tasks left the system hung, and the watchdog correctly reset it, repeatedly. JPL reproduced the fault on a ground replica from the trace logs the spacecraft sent back and uploaded a patch that turned on priority inheritance for the mutex. That is what good watchdog design buys you: the system fails safe, keeps talking, and gives you enough to fix it remotely.
Cellular trackers. The modem hangs more often than the host. If an AT command times out, toggle the modem's power key. If that fails three times, cut its power rail with a load switch. If that fails, the host resets itself. Layers, each one dumber and more forceful than the last.
Try it
Hang a task in the simulator, then switch the kick source between "timer ISR" and "supervisor". One of them never resets. The other resets within two seconds and drops into safe mode after three failed boots.
The uncomfortable truth
Most watchdog setups I review are configured to make the engineer feel safe, not to make the device recover. They are enabled, they are kicked, and they would never fire under any real failure, because the kick is wired to the one thing that never fails.
A watchdog is not a feature you add. It is a question you ask about every task in the system: if this stopped working, how would the device know? Answer it for each one, write the answer into the supervisor, and let the hardware do the rest.
The soil sensor got new firmware a week later. The supervisor now checks for a fresh reading before it kicks the dog. Nobody has driven out to pull a battery since.







