Tutorial On Arduino Watchdog Timer Setup

Today in this tutorial we will learn importance of watchdog timer and configuring watchdog timer in Arduino.

What is Watchdog Timer ?

A watchdog timer (WDT) is a hardware timer that automatically generates a system reset if the main program neglects to periodically service(reset) it. The Watchdog Timer is clocked from a separate On-chip Oscillator which runs at 1 MHz. This is the typical value at V CC = 5V. It is often used to automatically reset an Arduino that hangs because of a software or hardware fault. Some systems may also refer to it as a computer operating properly (COP) timer. All Arduino boards have watchdog timer hardware.

How Watchdog Timer Works?

arduino watchdog timer code

The main program typically has a loop that it constantly goes through performing various functions. The watchdog timer is loaded with an initial value greater than the worst case time delay through the main program loop. Each time it goes through the main loop the code resets the watchdog timer (sometimes called “kicking” or “feeding” the dog). If a fault occurs and the main program does not get back to reset the timer before it counts down to zero, an interrupt is generated to reset the processor. Used in this way, the watchdog timer can detect a fault on an unattended arduino program and attempt corrective action with a reset. Typically after reset, a register can also be read to determine if the watchdog timer generated the reset or if it was a normal reset. On the arduino this register is called the Watchdog Reset Flag Register (WDRF).

How to program watchdog timer ?

Step 1: Library required

#include <avr/wdt.h>

this library is required to use watchdog timer in arduino.

Step 2: Enabling/Disabling Watchdog timer with reset interval

If the Watchdog Timer is not needed in the application, this module should be turned off. If the Watchdog Timer is enabled, it will be enabled in all sleep modes, and hence, always consume power. In the deeper sleep modes, this will contribute significantly to the total current consumption.

Enabling Watchdog timer

Syntax: wdt_enable(WDT Reset Timer);

Example:

wdt_enable(WDTO_4S);

Watchdog timer can be enabled with different time settings. Time setting is time between the watchdog reset and feed. Time must be greater than the time required for program loop takes to come back again. Maximum of 8 Seconds and minimum of 15mSec can be set.

Watchdog Time Setting Table for different Arduino controllers

Threshold value Constant name Supported on
15 ms WDTO_15MS ATMega 8, 168, 328, 1280, 2560
30 ms WDTO_30MS ATMega 8, 168, 328, 1280, 2560
60 ms WDTO_60MS ATMega 8, 168, 328, 1280, 2560
120 ms WDTO_120MS ATMega 8, 168, 328, 1280, 2560
250 ms WDTO_250MS ATMega 8, 168, 328, 1280, 2560
500 ms WDTO_500MS ATMega 8, 168, 328, 1280, 2560
1 s WDTO_1S ATMega 8, 168, 328, 1280, 2560
2 s WDTO_2S ATMega 8, 168, 328, 1280, 2560
4 s WDTO_4S ATMega 168, 328, 1280, 2560
8 s WDTO_8S ATMega 168, 328, 1280, 2560

Disabling Watchdog timer

wdt_disable();
Step 3: Resetting Watchdog timer

This function must be called in the beginning of loop(). It resets the watchdog timer count. If program loop hangs and unable to reset it then watchdog timer will reset the arduino and prevent controller hang issue due to noise or logic failure.

wdt_reset();
Step 4: Basic Arduino Code for Watchdog Timer

wdt_reset() is necessary to include it to every time-consuming operation. For instance, if you have data transmission cycle in your main loop, and this cycle lasts for a couple of seconds, you should put wdt_reset(); there as well, or your board may reset during transmission.

When choosing a threshold, take all time- and timing-related values into account: delays, function thresholds, bus timeouts and speed. Threshold value should always be greater than the largest value – at least 1.5 times greater in most cases. However, this depend on your application.

Another thing to take in mind is that it will reset the MCU, but not another hardware in your project, so you have to do it manually during Arduino initialization – if you have such hardware, sure.

You will get same RAM contents after Watchdog timer reset.

#include <avr/wdt.h>

void setup(){
   //watchdog timer with 2 Seconds time out
   wdt_enable(WDTO_2S);
}

void loop(){
   //do stuff here

   wdt_reset();
}

What are the other uses of Watchdog Timer?

Watchdog timer is not only used for handling hang issues of controller but it can be used as Power saving tool. Watchdog timer keeps running in power down modes of controller.

To periodically wakeup the controller and go back to sleep is possible using Watchdog reset. In remote sensing applications.

Example: Watchdog Timer for Power Saving in Remote Data Sensing Application

Lets take a case of Temperature monitoring using wireless Module nRF24L01 and LM35 Temperature Sensor. In this we want temperature updates at every 8 Seconds and operate controller using batteries.

Program Flow:

  1. Set Watchdog Timer to 8 Seconds
  2. Read Temperature Data
  3. Send using nRF24L01
  4. Power Down
  5. Watchdog Timer will automatically resets cpu after 8 Seconds. As we are not resetting WDT time in program using WDT_reset();

Arduino Code for Remote Sensing with Power Saving using Watchdog Timer

#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "LowPower.h"

RF24 radio(9,10);

const uint64_t pipe = 0xF0F0F0F0E1LL;

void setup() {
  //Setup Watchdog Timer with 8 Seconds Reset
  wdt_enable(WDTO_8S);
  
  //Setup Wireless nRF24L01 Radio
  radio.begin();
  radio.setRetries(15,15);
  radio.setPayloadSize(30);
  radio.setPALevel(RF24_PA_MAX); 
  radio.setDataRate(RF24_250KBPS);

  radio.openWritingPipe(pipe);
  radio.stopListening();
}

void loop() {
    double Temperature = ((5.0/1024.0) * analogRead(A0)) * 100;
    radio.powerUp();
    delay(5);
    radio.write(Temperature,sizeof(double));

  //Turn off Radio
    radio.powerDown();

  // Set sleep to full power down.  Only external interrupts or
  // the watchdog timer wake the CPU after 8 Seconds
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
}

This way we can utilize Watchdog timer in Arduino. Lets Summarize What we have Learned

Summary:

Watchdog timer
  • A timing device such that it is set for a preset time interval and an event must occur during that interval else the device will generate the timeout signal on failure to get that event in the watched time interval.
  • Timeout may result in processor start a service routine or start from beginning

Functional Example

  • Assume that we anticipate that a set of tasks must finish in 100 ms interval.
  • The watchdog timer is reseted by the program instruction in case the tasks finish within 100 ms interval.
  • In case task does not finish (WDT_reset() is not called by the program instruction), watchdog timer generates program reset after 100 ms because there is failure of finishing the task in anticipated interval.
Watchdog timer application
  • Watchdog Timer can be used for Power Saving in Battery operated Remote sensing applications.
  • It used to prevent controller hang issues.

Leave a Reply