Exercises — Watchdog timers — purpose, feeding, types
This page is a graded workout for the watchdog timer topic. Every problem has a full solution hidden inside a collapsible callout so you can test yourself first. Levels run L1 Recognition → L2 Application → L3 Analysis → L4 Synthesis → L5 Mastery.
Before we start, one promise: every symbol used below is one you already earned in the parent note. The only formula we lean on is the timeout equation, so let us re-anchor it in plain words — and pin down our symbols once so nothing surprises you later.

Prerequisite links if any step feels shaky: Hardware Timer Peripherals, Interrupt Service Routines, System Reset Sources, Real-Time Operating Systems, Fault Tolerance, Brown-Out Detection, Safe State Design, Bootloader Design.
L1 — Recognition
Problem 1 (L1)
A watchdog timer is best described as which one of these?
(a) A stopwatch the CPU reads to schedule tasks. (b) A hardware down-counter that resets or interrupts the system if software fails to periodically "feed" it. (c) A voltage sensor that detects a low battery. (d) A memory region storing the last error code.
Recall Solution
Answer: (b). The defining behaviour is the dead-man's-switch idea from the parent note: software must keep proving it is alive by feeding the timer. If feeding stops, the counter reaches and forces a reset/interrupt.
- (a) describes a plain hardware timer — no automatic reset on neglect.
- (c) is Brown-Out Detection, a different reset source.
- (d) is just logging; it takes no action.
Problem 2 (L1)
Match each term to its meaning: feed, timeout, prescaler.
Recall Solution
- Feed ::: The act of software reloading the counter back to so it never reaches (on AVR this is the
wdrinstruction, i.e.wdt_reset()). - Timeout ::: The time it takes the counter to fall from to with no feeding — the deadline software must beat.
- Prescaler ::: A clock divider that slows the counter so each decrement takes longer, letting a small counter measure a long time.
L2 — Application
Problem 3 (L2)
An 8-bit watchdog counter () is clocked at with . Find the timeout period in milliseconds.
Recall Solution
Plug straight into the formula. So this configuration times out in about 996 ms — just under one second. Look at figure s01: same staircase idea, just steps instead of .
Problem 4 (L2)
You need a timeout of 500 ms on a WDT with and (1 MHz). What prescaler value do you need? Is it a "typical" prescaler (1, 8, 64, 256, 1024)?
Recall Solution
Rearrange the formula to solve for the unknown — why rearrange? Because the timeout is the given here and the prescaler is what we must pick. Here is the timeout we want. The nearest standard prescaler is . With the real timeout becomes A hair over 500 ms — safe, since a slightly longer window never causes a false reset; it only relaxes the deadline.
Problem 5 (L2)
A main loop runs three tasks costing ms, ms, and ms, then feeds the watchdog once per iteration. The watchdog timeout is ms. What is the margin (spare time) before a hang would trip the reset, and what fraction of the timeout does the loop use?
Recall Solution
One full loop takes . That is the longest gap between two feeds in healthy operation, so . A healthy utilisation leaves comfortable headroom. Figure s02 shows the feed happening at ms against the ms deadline bar.

L3 — Analysis
Problem 6 (L3)
Two developers configure the same 16-bit WDT (, ).
- Dev A picks .
- Dev B picks .
A legitimate firmware-update routine takes 40 seconds during which the watchdog cannot be fed. Which developer's configuration causes a false reset, and why?
Recall Solution
Compute both timeouts (, are just each config's ). The update needs the watchdog to stay quiet for .
- Dev A (): the watchdog fires mid-update → false reset, possibly corrupting the flash write (a job for careful Bootloader Design).
- Dev B (): survives the update comfortably.
The analysis takeaway: timeout must exceed the longest legitimate uninterrupted operation, or you get false positives. Short timeouts recover faster from real hangs but are less tolerant of long valid work — a genuine trade-off.
Problem 7 (L3)
A window watchdog on an STM32 uses a 6-bit down-counter fed at an effective decrement rate of decrement per millisecond, starting from . The chip's rule (stated precisely so the boundaries are unambiguous):
- Feeding is rejected as too early while the counter is still strictly greater than the upper threshold . A feed is legal only once the counter has fallen to or below.
- The chip issues a reset as soon as the counter reaches the lower threshold (i.e. is already "too late").
Find (earliest legal feed time) and (latest safe feed time), measured in whole milliseconds from the last refresh. Watch the counter is discrete — it only takes integer values at integer milliseconds.
Recall Solution
Because the counter drops one unit per millisecond from , its value at whole millisecond is Earliest legal feed (). We need . Solve . At the counter is exactly , which the rule declares legal (" or below"). So the earliest legal feed is (If the chip instead required strictly below , the first legal instant would be the next step, ms, because at the counter still reads . We adopt the "" rule stated above.)
Latest safe feed (). Reset fires the instant , i.e. at . So ms is already a reset — too late. The last safe whole-millisecond feed is therefore one step earlier, when the counter still reads : Legal window: feed at any whole millisecond with ms (counter from down to ) — a -step-wide slot. Figure s03 draws this band and labels the two boundary counter values.

L4 — Synthesis
Problem 8 (L4)
Design goal: choose a WDT configuration for a control loop.
- The main loop, worst case, takes per iteration (measured).
- You want the timeout to be roughly 2× the worst-case loop time so a single slow iteration never false-resets, but a true hang recovers within a fraction of a second. Call this wish .
- Hardware: , , standard prescalers .
Pick the prescaler, report the actual timeout , and confirm the safety margin over one worst-case loop.
Recall Solution
Step 1 — target. Desired .
Step 2 — what prescaler lands near there? Solve the formula for the prescaler at the target: The smallest available prescaler is , which gives the shortest possible timeout on this hardware. Name that best-case value (it is simply when ): Step 3 — read the reality. Even the fastest configuration times out at , far above our wish. That is a feature of the hardware: with a slow kHz clock and a 16-bit counter, you cannot get a sub-second timeout. So we accept , .
Step 4 — margin check. The loop uses only of the window — extremely safe, no false resets, and a true hang still recovers in . Synthesis lesson: sometimes the "ideal" timeout is unreachable and the design job is choosing the closest feasible configuration, then verifying the margin is comfortable — which it is.
Problem 9 (L4)
Refactor the following buggy feed placement into a correct heartbeat, and explain in one sentence what class of bug the fix now catches.
void loop() {
wdt_reset(); // fed first
read_sensor();
if (compute() < 0) {
while (1) { /* error spin */ } // intended "safe halt"
}
actuate();
}Recall Solution
Corrected version — feed after a full successful iteration:
void loop() {
read_sensor();
if (compute() < 0) {
enter_safe_state(); // deliberate, then let WDT reset us
while (1) { } // no feed here -> WDT recovers
}
actuate();
wdt_reset(); // heartbeat: only if we reached the end
}What it now catches: any path that fails to complete the entire loop body — including the deliberate error-spin — because the feed only happens once read_sensor → compute → actuate all succeed. In the buggy original, the early feed gave the error-spin a full fresh timeout before recovery, and an accidental infinite loop after the feed would still be masked for one whole window. See Safe State Design for why we drive outputs to a known-safe value before letting the reset happen, and Fault Tolerance for the recovery philosophy.
L5 — Mastery
Problem 10 (L5)
A window watchdog protects an ISR-driven loop. The window is (as in the parent note; feeds strictly inside the open interval are legal). A timing bug makes an interrupt fire every , and each firing runs the loop and attempts a feed.
(a) In an 8-firing span ( after the last successful refresh), how many of the attempted feeds land inside the legal window? (b) Explain why a plain (non-window) watchdog would report this system as perfectly healthy, and what the window watchdog gains.
Recall Solution
(a) The ISR fires at after the last successful refresh. A feed is legal only when (strictly inside).
- : too early () → each is rejected / triggers a reset on a strict window watchdog.
- : on the boundary, and is false → too early, not legal.
- : → legal ✅ — the only one.
- : boundary, is false → too late.
Final count: exactly 1 legal feed (at ) out of the 8 attempts. Every other attempt is a window violation, so the window watchdog refuses the early feeds and forces you to confront the timing bug.
(b) A plain watchdog only asks "were you fed before ?" Feeding every satisfies that easily — 8 healthy-looking heartbeats inside every span — so the timing bug is completely invisible to it. The window watchdog adds the lower bound , converting the question into "were you fed at the RIGHT rate?" (not too fast, not too slow). That is exactly what catches interrupt storms and skipped-delay bugs that a plain watchdog sails past. Cross-reference Interrupt Service Routines for why keeping ISRs short and rate-correct matters, and Real-Time Operating Systems for how an RTOS task-based watchdog generalises this to per-task heartbeats.
Problem 11 (L5)
Multi-task heartbeat. An RTOS runs three tasks that must each stay alive. A common pattern: each task sets its own bit in a shared alive_flags word when it completes a cycle; a supervisor feeds the hardware watchdog only when all bits are set, then clears them. Task A cycles every , Task B every , Task C every . What is the minimum hardware-watchdog timeout that will not false-reset in healthy operation, and why?
Recall Solution
The supervisor can only feed once all three bits are set, then it clears them. After clearing, the next feed cannot happen until every task has completed at least one more cycle — and the slowest task, C, gates that, because A and B set their bits far more often and are already set well before C finishes.
- Worst-case gap between two supervisor feeds Task C's period (plus a little scheduling jitter).
- Therefore the hardware timeout must satisfy Choosing gives a margin over the slowest heartbeat while still recovering a genuine total hang within . Mastery point: in a multi-task system the watchdog is only as fast as your slowest mandatory heartbeat — set the timeout from that task, not from the fast ones, or a stalled Task C will look healthy whenever A and B keep the shared feed alive. That AND-gate on the flags is what prevents a fast task from masking a dead slow task.
Recall Quick self-audit checklist
- Timeout formula: multiply by prescaler ::: — bigger prescaler means longer timeout.
- Solve for prescaler ::: , then round to a standard value.
- Where to feed ::: once, at the end of a complete iteration (heartbeat), never first-thing, never per-function.
- Window watchdog ::: legal only in ; "reset when counter " means last safe step is .
- Multi-task ::: gate the feed on the AND of all mandatory heartbeats; timeout set by the slowest task.