Skip to content
You Start with one site
Amounts in Start with one site
Microcontrollers · Concept

Watchdog timer

One idea Intermediate 5 min read
Found this useful?

A night guard must press a button on the wall every hour. If the button is not pressed, an alarm sounds, because the guard may have fallen asleep. A watchdog timer does this for a program: it is a countdown inside the chip that the program must start again and again, and if the program stops doing so, the countdown reaches zero and restarts the chip.

The countdownA separate timer with its own clock, running down to zero.For example 2 s
The clearOne line of the program that starts the countdown again.Run in every loop
The restartIf the countdown reaches zero, the chip starts the program again from the beginning.No person needed

See it

Predict first

A healthy program needs 3 s for one loop and clears the watchdog once in each loop. Someone sets the watchdog timeout to 2 s. What happens?

See it: a countdown that the program must keep starting again

Watchdog timeout
Time for one loop
Restarts of the chip in 12 s

The line is the time left on the watchdog. It falls all the time. At the end of each loop the program clears the watchdog and the line jumps back to the top. When the program hangs, nothing clears it, the line reaches zero and the chip restarts. The restart is drawn as taking no time.

The idea

A program can hang: it waits for a sensor that never answers, or a fault sends it round the same few lines for ever. At a remote site there is nobody to press the reset button.

The watchdog is a timer with its own clock, so it keeps running when the rest of the chip is stuck. You choose its timeout, from 16 ms to 8 s on the ATmega328P, and from 0.1 ms to about 26 s on the STM32F103, whose watchdog clock (nominally 40 kHz) may run anywhere from 30 to 60 kHz, so its timeout can be a third shorter than planned. The program must clear the watchdog, which starts the countdown again, before the timeout passes. If it fails to, the watchdog resets the chip, and the program starts again from the beginning.

timeout > longest healthy loop

the longest time a working program needs between two clears. Two times that is a common margin

A restart does not repair anything. If a sensor is broken, the program will hang again, so a good program counts its restarts and reports them.

One worked number

The question

A logger clears its watchdog at the end of every loop, and one loop takes 0.5 s. The timeout is 2 s. At 4.2 s the program hangs. When does the chip restart, and for how long was the logger stopped?

Show the worked solution, 3 steps
  1. Find the last clear before the hang

    Clears come at 0.5 s, 1.0 s, 1.5 s and so on. The last one before 4.2 s is at 4.0 s.

  2. Add the timeout

    The countdown started again at 4.0 s and nothing clears it after that. It reaches zero at 4.0 s + 2 s = 6.0 s, and the chip restarts.

  3. Find the time lost

    6.0 s − 4.2 s = 1.8 s stopped. Check it in the figure: press the button that makes the program hang.

The mistake to avoid

A watchdog does not know that the program works. It knows only that the clear still runs. A common error is to clear the watchdog from a timer interrupt, a piece of code that the chip runs by itself at fixed times. The main program can then hang for days while the interrupt keeps clearing the watchdog, and the chip never restarts. Put the clear in the main loop, after the real work is done.

A second trap: a timeout shorter than the longest healthy loop restarts a program that has no fault.

Check yourself

Question 1

A program clears its watchdog every 0.25 s. The timeout is 1 s. The program hangs 0.1 s after a clear. How long after the hang does the chip restart?

Question 2

A controller has a watchdog, and it still stopped sending data for two days until someone switched it off and on. Its watchdog is cleared inside a timer interrupt that runs every 10 ms. Why did the watchdog not help?

Question 3

You add a watchdog with a timeout of 2 s to a device that worked well. Now it restarts every 2 s. Its loop includes a wait of 3 s for a network modem to answer. What is wrong?

Where you meet it

  • Remote monitoringA logger at a borehole or a solar plant restarts itself after a hang, so nobody has to travel to press reset.
  • Machine controllersWhen the program of a controller stops, its watchdog switches the outputs to a safe state, so a motor is not left running.
  • Routers and modemsThe box that connects a site to the network restarts itself in the same way.
Key points
  • A watchdog is a countdown with its own clock. The program must clear it before it reaches zero.
  • If the program hangs, nothing clears it, and the watchdog restarts the chip.
  • The timeout must be longer than the longest healthy loop, with a margin.
  • Clear it from the main loop, never from a timer interrupt. A restart is a recovery, not a repair.
Found this useful?

Discussion

    Comments show your name. Be kind, stay on the topic.