Search

Essential Guide to Arduino’s Watchdog Timer

The Arduino UNO board is powered by the ATmega328P chip, which serves as its controlling unit.

Embedded within the ATmega328P is a Watchdog Timer, a valuable feature designed to aid system recovery in instances where the system becomes unresponsive or freezes due to coding errors or hardware malfunctions.

Parts Required

Component NameBuy Now
Arduino Uno REV3Amazon
Nano V3.0, Nano Board ATmega328P 5V 16M MicroAmazon

How does the Watchdog timer work?

The Watchdog timer necessitates configuration based on the specific requirements of the application.

Utilizing an internal 128kHz clock source, the Watchdog timer initiates counting from zero up to a user-defined value. If the timer isn’t reset before reaching this predetermined value, it automatically resets the microcontroller.

The ATmega328P’s Watchdog timer offers 10 distinct time settings, determining the interval before the timer overflows and triggers a reset. These intervals range from 16 milliseconds to 8 seconds.

Example:

To illustrate the configuration of the Watchdog timer for an Arduino UNO board, let’s consider a straightforward example involving LED blinking.

The LEDs blink for a designated duration before entering a continuous loop (while(1)). This loop simulates a system in a hung state.

As the Watchdog timer isn’t reset within the while(1) loop, it initiates a system reset, causing the LEDs to resume blinking before the system hangs and restarts the cycle anew.

For this example, we’ll utilize the onboard LED connected to pin 13 of the Arduino UNO board. The only prerequisite for this sketch is the Arduino UNO board itself.

Important Note:

The Watchdog timer is initially deactivated in the code. A 3-second delay is implemented before activating the Watchdog.

This delay serves a crucial purpose: allowing the Arduino bootloader to check for any new code uploads and granting it time to program the flash memory.

This precaution is vital because of potential issues arising from faulty code or insufficient considerations. In such scenarios, the code may inadvertently cause the microcontroller to reset at extremely short intervals continuously.

Such behavior can damage the Arduino board and prevent sketches from being successfully uploaded.

While this may not be applicable to newer versions equipped with the Optiboot loader, it remains a concern for older models.

If the Arduino is inadvertently damaged in this manner, restoring it will require burning the bootloader using another Arduino as an ISP (In-System Programmer).

Sketch

#include<avr/wdt.h> /* Header for watchdog timers in AVR */

void setup() {
  Serial.begin(9600); /* Define baud rate for serial communication */
  Serial.println("Watchdog Demo Starting");
  pinMode(13, OUTPUT);
  wdt_disable();  /* Disable the watchdog and wait for more than 2 seconds */
  delay(3000);  /* Done so that the Arduino doesn't keep resetting infinitely in case of wrong configuration */
  wdt_enable(WDTO_2S);  /* Enable the watchdog with a timeout of 2 seconds */
}

void loop() {
  for(int i = 0; i<20; i++) /* Blink LED for some time */ 
  {
    digitalWrite(13, HIGH);
    delay(100);
    digitalWrite(13, LOW);
    delay(100);
    wdt_reset();  /* Reset the watchdog */
  }
  while(1); /* Infinite loop. Will cause watchdog timeout and system reset. */
}

Related article

Leave a Comment