What is Watchdog Timer and How to Use it for Microcontrollers

If a microcontroller freezes during operation, it will not respond to any actions.  The only solution is to power off the device and restart it to resume normal operation. To solve the problem of microcontroller freezing, designers have developed a module called the watchdog timer. With the watchdog timer, microcontrollers are less likely to freeze. Let’s take a look at what a watchdog timer is and how to use it.

In a practical design, the schematic of a watchdog circuit would include the following parts:

  • Clock source: Provides a reference clock signal for timing and generating timer interrupt signals.
  • Timer: Generates timer interrupt signals based on the clock signal and is used to periodically detect the system operating state.
  • Status detection logic: Used to detect the system operation status, such as monitoring whether a specified signal is normal, checking whether the system is in the expected operating state, etc.
  • Reset circuit: Generates a reset signal to force the system to restart when an abnormality is detected.
  • Control logic: Used to control the working state of each part such as the timer and reset circuit.
SimpleWatchdogTimer

The watchdog timer is essentially a timer. For example, with an 8-bit timer, it can time up to 255 pulses. If the time of one pulse is one millisecond, the maximum time that the timer can set is 255 milliseconds. For instance, if we set the time to 128 milliseconds, it counts from 0 and increments by one every time a pulse is received until it reaches 128, which triggers an interrupt.

The microcontroller then knows that the timer has timed out. The working principle of the watchdog timer is similar to that of the timer, but instead of triggering a normal interrupt, it triggers a microcontroller reset, which is a restart. In short, we set a time in advance, and when the time is up, it triggers a microcontroller reset, which is the function of the watchdog timer.

Next, let’s talk about how to use the watchdog timer. First, let’s consider a simple LED program that lights up four LEDs in sequence with a time interval of 10 milliseconds. Because this program is inside a while¬†1¬†loop, it repeats the above process continuously.

Code:

import RPi.GPIO as GPIO
import time
# Setting the GPIO port
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set the LED GPIO port
led_pins = [18, 23, 24, 25]
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)
# Infinite loop
while True:
    # Light up LED lights
    for pin in led_pins:
        GPIO.output(pin, GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(pin, GPIO.LOW)
# Clear the GPIO port
GPIO.cleanup()

Then, we add a watchdog timer to the program. At the beginning of the program, we set the watchdog timer time to 50 milliseconds and start the watchdog timer. The watchdog timer starts timing, and once the time is up, the microcontroller will reset, and the program enters a loop.

import RPi.GPIO as GPIO
import time
# Setting the GPIO port
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# Set the LED GPIO port
led_pins = [18, 23, 24, 25]
for pin in led_pins:
    GPIO.setup(pin, GPIO.OUT)
# Setting watchdog GPIO port
watchdog_pin = 17
GPIO.setup(watchdog_pin, GPIO.OUT)
# Set the watchdog time to 50 milliseconds
watchdog_time = 0.05
# Start Watchdog Timer
GPIO.output(watchdog_pin, GPIO.HIGH)
watchdog_start = time.time()
# Infinite loop
while True:
    # Watchdog Timing
    if (time.time() - watchdog_start) > watchdog_time:
        # Trigger watchdog restart
        GPIO.cleanup()
        time.sleep(1)
        os.system('reboot')
    
    # Light up LED lights
    for pin in led_pins:
        GPIO.output(pin, GPIO.HIGH)
        time.sleep(0.01)
        GPIO.output(pin, GPIO.LOW)
# Clear the GPIO port
GPIO.cleanup()

The next step is to clear the watchdog timer, which starts counting from 0. Then the LED starts flashing, and when the four LEDs finish looping, it takes 40 milliseconds. At this time, there are still 10 milliseconds before the watchdog timer triggers a reset. If there is no freezing, the program will return to the first step after completing the loop, the watchdog timer will be cleared, and the next loop will begin.

Trigger microcontroller reset

As long as the program runs correctly, the watchdog timer will never trigger a microcontroller reset. However, if the program freezes, it will not return to the first step to clear the watchdog timer. After 50 milliseconds, the watchdog timer will trigger a microcontroller reset. Although it has frozen, the watchdog timer module is still running and can trigger a microcontroller reset.

If we do not clear the watchdog timer, the consequences will be terrible. In this case, the microcontroller will reset every 50 milliseconds, which is just as bad as freezing. The watchdog timer will become a mad dog. Therefore, when using the watchdog timer, we must feed the dog well.

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top