Embedded

Watchdog timer in embedded systems

By CourseTron Team ·  8 min read ·  Updated Oct 2026 💬 𝕏 in f
Table of Contents
    🖨  Download / Print this guide (PDF)

    In short: a watchdog timer is a free-running hardware counter that resets the microcontroller unless firmware periodically "kicks" it before it expires. It is the last line of defence against a hung or runaway program, turning a silent lock-up into an automatic recovery.

    What Is a watchdog timer?

    A watchdog timer (WDT) is a dedicated hardware counter inside a microcontroller whose only job is to detect when software has stopped behaving normally. The counter counts down (or up) from a preset value toward a threshold. Healthy firmware must repeatedly reload the counter before it reaches that threshold — an action called "kicking", "feeding", "petting" or "refreshing" the watchdog. If the firmware ever fails to refresh in time, the counter expires and the watchdog forces a system reset, pulling the device out of a frozen state without any human intervention.

    The underlying assumption is simple but powerful: a correctly running program will always reach its refresh point on schedule, while a crashed, deadlocked, or runaway program will not. The watchdog therefore acts as a hardware proxy for "is the software still alive?".

    Why a watchdog timer matters

    Embedded systems frequently run unattended for months or years — in a car engine controller, a pacemaker, a water meter, or a satellite. When the main program hangs, there is usually no operator to press a reset button. Faults that a watchdog guards against include:

    • Infinite loops caused by a logic bug or an unexpected input combination.
    • Deadlocks where two tasks each wait on a resource the other holds.
    • Stack overflow or corruption that sends the program counter into invalid memory.
    • Peripheral stalls, for example a sensor bus that never returns and blocks a polling loop forever.
    • Transient upsets from electrical noise, brown-outs, or cosmic-ray bit flips that derail execution.

    Without a watchdog, any of these leaves the product dead until power is physically cycled. With one, the system reboots itself within milliseconds and, in most cases, resumes normal operation. This is why functional-safety standards such as ISO 26262 (automotive) and IEC 61508 (industrial) effectively mandate watchdog supervision for critical controllers.

    Key fact: the watchdog must be refreshed from the main application flow, not from a timer interrupt. If you kick it inside an ISR, the watchdog will keep petting itself even when the main loop has hung — which defeats the entire purpose.
    Where the watchdog is kicked Wrong: kick from an ISR Timer ISR Watchdog Main loop hung ISR keeps kicking: hang never detected Right: kick from the main flow Task flags Supervisor Watchdog Kick only when all flags are present A stalled task means no kick, so reset CourseTron · coursetron.com
    Kicking from a timer ISR hides a hung main loop; kicking from a supervisor gated on task flags exposes it.

    How a watchdog timer works

    At reset or during initialisation, firmware configures the watchdog with a timeout period (often a few milliseconds to a few seconds) and then enables it. From that point the counter runs continuously off a clock source — frequently an independent low-speed oscillator so the watchdog keeps ticking even if the main system clock fails.

    During normal operation the program periodically writes a specific value or key sequence to a watchdog register, reloading the counter. The common structure looks like this:

    1. Configure timeout and select the clock source.
    2. Enable the watchdog (on many MCUs this is a one-way action — it cannot be turned off until reset).
    3. In the main loop, perform real work, then refresh the watchdog only once all health checks pass.
    4. If a refresh is ever missed, the counter expires and asserts an internal reset.

    A well-designed refresh does more than blindly write the register. Good firmware gates the kick behind evidence that every critical task is actually running — for example, each task sets a flag, and the supervisor only feeds the watchdog when all flags are present, then clears them. This catches a single stalled task even while the rest of the system still loops.

    Counter value over time: kicks reload it, a missed kick resets threshold: counter expires kick kick no kick timeout: forced reset CourseTron · coursetron.com
    Animated: each kick reloads the counter; the third countdown gets no kick, reaches the threshold and forces a reset.
    Watchdog refresh and reset cycle Firmware main loop Watchdog counter kick / refresh (in time) counter reloaded, keeps running timeout expires (no kick) forces MCU reset CourseTron · coursetron.com
    Firmware kicks the watchdog counter in time; a missed kick expires the timeout and forces an MCU reset.

    Independent vs windowed watchdog timers

    Not all watchdogs are the same. Two variants dominate modern microcontrollers, and interviewers love the distinction.

    Independent watchdog (IWDG)

    An independent watchdog runs from its own dedicated low-speed oscillator, electrically separate from the main CPU clock tree. Because it does not depend on the system clock or PLL, it still fires even if the primary clock dies or the PLL fails to lock. This independence makes it the preferred choice for safety-critical designs. Its one weakness is that you can usually only refresh it "early" — it enforces a maximum interval but not a minimum.

    Windowed watchdog (WWDG)

    A windowed watchdog enforces both a lower and an upper bound. The refresh is only accepted inside a valid time window: if firmware kicks too early it is treated as a fault just as surely as kicking too late. This catches a subtle failure mode the independent watchdog misses — a program stuck in a tight, fast loop that happens to hit the refresh instruction far too often. The window therefore verifies that timing, not just liveness, is correct. Many MCUs let you use both an independent and a windowed watchdog together for layered protection.

    Refresh timing: independent versus windowed Too early fault (windowed) Valid window refresh accepted Too late reset minimum maximum Independent: enforces the maximum interval only Windowed: enforces both minimum and maximum CourseTron · coursetron.com
    An independent watchdog enforces only a maximum interval; a windowed watchdog also rejects a too-early refresh.

    How the industry uses and tunes watchdog timers

    Choosing a good timeout is an engineering judgement. Too short and legitimate long operations (a flash erase, a bootloader update) trip a false reset; too long and the device sits frozen for an uncomfortable interval before recovery. Teams typically size the timeout a little above the worst-case loop time with margin, and temporarily suspend or extend the watchdog around known long blocking operations.

    Production practice also includes:

    • Reset-cause logging: on boot, firmware reads a status register to learn whether the last reset came from the watchdog, so field failures can be counted and diagnosed rather than hidden.
    • External watchdog ICs: a separate supervisor chip (or a window watchdog on a companion device) guards against failures of the MCU itself, including clock and supply faults the internal watchdog shares.
    • RTOS integration: in an RTOS, a dedicated supervisor task collects check-ins from every critical thread and feeds the watchdog only when all of them have reported, so one starved task still triggers recovery.
    • Safe-state design: engineers ensure that a watchdog reset leaves actuators, motors and outputs in a defined safe condition during the reboot window.
    Learn this hands-on

    Real projects and industry tool flows, with certification.

    Explore CourseTron Courses →

    watchdog timer — Interview FAQ

    Why should the watchdog not be refreshed inside an interrupt service routine?

    Because a timer interrupt keeps firing on its own hardware schedule even if the main application has crashed or deadlocked. Kicking the watchdog from that ISR would keep the counter alive while the real program is dead, so the watchdog can never detect the hang. The refresh must sit on the main execution path, ideally gated by proof that all critical tasks ran.

    What is the difference between a windowed and an independent watchdog?

    An independent watchdog runs off its own clock and only enforces a maximum refresh interval, so it survives a main-clock failure. A windowed watchdog enforces both a minimum and maximum interval — refreshing too early is also a fault — which additionally detects code that is looping far too fast or running out of expected timing.

    How do you choose the watchdog timeout period?

    Measure the worst-case time of your main loop including the longest legitimate blocking operation, then add margin. The timeout should be long enough that normal operation never trips it, yet short enough that a genuine hang is recovered quickly. Long known operations such as flash writes are handled by refreshing within them or briefly reconfiguring the watchdog.

    How can you tell a reset was caused by the watchdog?

    Most MCUs latch the reset source in a status register that survives the reset. On boot, firmware reads this register, distinguishes a watchdog reset from a power-on or brown-out reset, and logs it. Counting watchdog resets in the field is a key reliability metric and a starting point for root-cause debugging.

    Related: interrupts in embedded systems · the VLSI Glossary · all courses.

    Advertisement