Arduino Watchdog: How It Works

Arduino watchdog

In this new article we are going to see what a watchdog, what it can be used for, and how to use it in your projects Arduino. Everything you need to know about this interesting yet unknown function. And yes, as its name suggests (watchdog), it can be used to track down some problems.

Here we will see All you need to know regarding…

What is a watchdog?

In computing, a watchdog It is a supervisory mechanism used to monitor the performance of a system or program. Its main function is to detect and respond to abnormal situations or failures in the system, such as crashes or freezes, and to take corrective measures to guarantee the continuity of the operation or the recovery of the system.

the watchdog operates on a timer which is configured for a specific time interval. If the system or program does not perform a specific action or power the watchdog (i.e., restart it) within that time interval, the watchdog assumes that the system is in an unwanted state or has stopped responding correctly and takes a predetermined action. This action may vary by implementation and may include rebooting the system, generating error logs, triggering alarms, or taking specific steps to correct the problem.

The watchdog is used on a variety of computer systems and devices, from operating systems and servers to devices embedded and critical real-time systems, including Arduino. Its primary objective is to improve system reliability and availability by automatically detecting and responding to problems, thereby reducing the need for manual intervention in failure situations.

What is the Arduino watchdog?

books about arduino

The Arduino watchdog timer must be set according to the needs of the application. He Watchdog Timer makes use of an internal 128 kHz clock source (may vary depending on the board and MCU used). When activated, it starts counting from zero up to a user predetermined value. If the Watchdog Timer does not reset when it reaches this value, it resets the microcontroller.

A microcontroller, or MCU, is a small electronic device that integrates a central processing unit (CPU), memory, input/output peripherals, and sometimes communication interfaces on a single chip. It is designed to control and execute specific tasks.

Watchdog Timer ATmega328P, which is implemented in Arduino UNO, offers 10 different time settings, each determining when the timer will overflow and therefore cause a reset. The different time intervals are the following: 16 ms, 32 ms, 64 ms, 0.125 seconds, 0.25 seconds, 0.5 seconds, 1 second, 2 seconds, 4 seconds and 8 seconds, as we will see later in the table that I include.

If it is still not clear to you what you can do with Watchdog Timer Arduino UNO, we'll see An example so that you understand it graphically. In this example, we will use a simple blink of the LEDs. The LEDs blink for a set period before entering the while() loop. This while() loop is used as an alternative to a locked system. Since the Watchdog Timer is not reset while in the while() loop, it will cause a system reboot, and the LEDs will start flashing again before the system crashes and reboots. This cycle will continue...

Considerations and features

Watchdog Timer is disabled at the beginning of the code. A delay of x seconds is incorporated before enabling the Watchdog. This delay is crucial to allow the Arduino bootloader to check whether new code is being loaded and to allow enough time to burn the code to flash memory. This aspect is relevant as a precaution. A situation may arise where, due to faulty coding or improper considerations, the written code resets the microcontroller at very short intervals infinitely. This can damage the Arduino board and prevent code from being properly uploaded to it. If this happens, it is necessary to burn the bootloader using another Arduino as ISP on the locked Arduino...

When we use the Arduino watchdog, it is necessary to use bit registers to define the behavior of the chip. The relevant registers and their meaning are detailed in the microcontroller datasheet that is present on the Arduino board. However, the Arduino integrated development environment (IDE) comes with some functions and macros designed to simplify this process, which can be imported by including the library #include to use the AVR chip watchdog.

In this way, we can configure the watchdog activating it using the wdt_enable() function. The argument to this function determines the time before the board resets in case the timer has not been reset. Regarding the values ​​that you can configure in the code, here I include them:

Time before watchdog is triggered wtd_enable() argument
15 ms WDTO_15MS
30 ms WDTO_30MS
60 ms WDTO_60MS
120 ms WDTO_120MS
250 ms WDTO_250MS
500 ms WDTO_500MS
1 s WDTO_1S
2 s WDTO_2S
4 s WDTO_4S
8 s WDTO_8S
For more information, you can consult the Arduino datasheet, where you can find more details about the integrated microcontroller itself, the registers that can be used with the watchdog, pinout, peripherals, technical specifications, voltages, power consumption, etc.

Example of using watchdog on Arduino

Arduino IDE, data types, programming

Finally, we are going to see how the watchdog is used in a practical way with an example in Arduino IDE. As we see, it is quite simple, you can find various source codes like this on the Internet, to be able to practice, modify, and create your own codes to use watchdog in your projects. Let's see our example:

#include <avr/wdt.h> // Incluir la biblioteca watchdog (wdt.h)
 
void setup()
{
  wdt_disable(); // Desactivar el watchdog mientras se configura, para que no se resetee
  wdt_enable(WDTO_2S); // Configurar watchdog a dos segundos
}
 
void loop()
{
  wdt_reset(); // Actualizar el watchdog para que no produzca un reinicio
  //Aquí iría el código de tu programa...
}

As can be seen in this example of a sketch for Arduino, there are three functions of the remarkable programming language for managing the watchdog, and these are:

  • wdt_disable() to disable the timer while configuring Arduino.
  • wdt_enable(tiempo) to assign an interval to the timer and start it, specifying the corresponding time as I have shown in the previous table.
  • wdt_reset() to renew the assigned interval and so that the program does not restart.

Add as preferred source in Google