Arduino Watchdog

Arduino has a feature that helps to handle unexpected errors in the Arduino program when the program stops responding, goes into an infinite loop, freezes. This is the watchdog timer (WDT).

UNO R3 board ATmega 328 with usb cable
UNO R3 board ATmega 328 with usb cable from amazon(Ad)

The Arduino Watchdog timer also uses an interrupt. If the Arduino program does not respond until the set timer of the Watchdog overflows, the Watchdog will restart the microcontroller.

To use Watchdog, we need the AVR Watchdog library, call it at the beginning of the Arduino sketch.

#include <avr/wdt.h>

To use watchdog, we need three functions. Use the wdt_enable(time) function to enable the use of Watchdog.

wdt_enable(time)

In the parameter, we specify the time after which the watchdog restarts the processor. This time must be defined in such a way that it is enough to run the program in the loop().

The Watchdog timer can be enabled with different time settings. The Watchdog library contains the constant values ​​that can be used. The value of these constants is between 16ms and 8s according to the table below.

TimeConstant name
16 msWDTO_15MS
32 msWDTO_30MS
64 msWDTO_60MS
0,125 sWDTO_120MS
0,25 sWDTO_250MS
0,5 sWDTO_500MS
1.0sWDTO_1S
2.0sWDTO_2S
4.0sWDTO_4S
8.0sWDTO_8S

With the help of the wdt_disable() function, we can stop the monitoring of the Watchdog.

wdt_disable();

The wdt_reset() function should normally be called at the beginning of loop(). Normally, every loop() runs and resets the watchdog timer counter. If our Arduino program crashes for any reason, the wdt_reset() function cannot reset the Watchdog timer, the Arduino will restart.

#include <avr/wdt.h>

void setup()
{
  wdt_enable(WDTO_8S); // Turn on the 8 second watchdog timer
}

void loop()
{
  wdt_reset(); // Watchdog reset

  // main program...
}

In the case of a longer, more time-consuming program, the wdt_reset() function must be placed again in the critical program section.

The following example code shows the control of an irrigation system with a soil moisture sensor. This code is not necessarily practical, but it illustrates the use of watchdog.

#include <avr/wdt.h>

const char motorValve = 13;
const char sensorPin = A0;

int sensorValue = 0;


void setup()
{
  // Enable watchdog with a 4-second timeout
  wdt_enable(WDTO_4S);
  
  Serial.begin(9600);
  
  pinMode(motorValve, OUTPUT);
  digitalWrite(motorValve, LOW);
}

void loop()
{
  // Watchdog reset
  wdt_reset();   
  
  // Let's read the value of the sensor
  sensorValue = analogRead(sensorPin);
  
  // If the sensor value decreases
  if(sensorValue < 50)
  {
    // We start watering by opening the motorized valve
    digitalWrite(motorValve, HIGH);
  
    // We wait until the soil is sufficiently moist.
    // The cycle runs until the sensor value is at least 100.
    while(sensorValue >= 100)
    {
      // We're restarting Watchdog's timer because we don't know
      // when the sensor value reaches 100      
      wdt_reset();
	
      delay(1000);
	
      // read the value of the sensor again
      sensorValue = analogRead(sensorPin);
    }
    // If the value of the sensor reaches 100, the cycle is exited
    // and shut off the water
    digitalWrite(motorValve, LOW);
  }
  
  // We write the value of the sensor on the serial port.
  Serial.println(sensorValue);
  
  delay(1000);
}

The Watchdog timer is not only used to handle freezes caused by unexpected errors in the Arduino program. The Watchdog timer runs continuously even when the Arduino is asleep, so it is suitable for occasionally waking up the Arduino.

In battery-powered projects, we must strive to save energy. Watchdog helps with this as well.

#include <avr/wdt.h>
#include <avr/sleep.h>

const char ledPin = 13;

void setup()
{
  // We set the watchdog timer to 8 seconds
  wdt_enable(WDTO_8S);

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
}


void loop()
{
  // We reset the Watchdog timer
  wdt_reset();
  
  // We turn on the onboard LED for two seconds
  digitalWrite(ledPin, HIGH);
  delay(2000);
  digitalWrite(ledPin, LOW);


  // We can use it, for example, to process sensor data
  // Then we send it to sleep mode to save energy.
  
  // We select the power down mode
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);

  // Then we send the processor to sleep
  sleep_mode();
}

advertising – Amazon.com