Arduino Watchdog Timer with Reset

Arduino Watchdog Timer with Reset

 



Description

As the title suggests, Watchdog timer reset is used to remove issues/anomaly ocurring during system functioning. The watchdog timer(WDT) in Arduino is preset for a specific time interval to reset itself, and if due to some issue controller gets stuck in a loop or is not able to execute the program flow, it won't be able to reset the WDT and hence WDT resets arduino restoring the system to its intended functionality. for e.g.  Lets consider Arduino is set to reset WDT after every 2 sec and it follows the process, and suddenly there is an issue which occured making Arduino unable to reset the WDT, then WDT will reset Arduino just after 2 Seconds thus restoring arduino's performance. This type of functioning is required in mission critical applications for fail safe operation. 

In this example I'm using an Arduino UNO employing an ATmega328p chip as an example for setting up WDT reset, but it will apply to other boards as well maybe with some suitable adjustments. (Note: There are bugs in arduino pro mini bootloader with respect to WDT at the time of performing this example so I've ported Arduino UNO's bootloader in the Arduino pro mini to achieve the functionality)

Points to remember:

  • A 128KHz internal clock source is used by the WDT
  • When WDT is enabled it starts counting from 0 to a user selected value, and if it is not reset by the time it reaches the user selected value, it resets micro-controller.
  • For ATmega328p, WDT can be configured for 10 different time settings (time after which if not reset by the micro-controller, it resets microcontroller)
  • The configurable time settings are 16ms, 32ms, 64ms, 0.125s, 0.25s, 0.5s, 1s, 2s, 4s and 8s.

Caution:

  • A suitable delay is kept at the start of the code so as to ensure safe booting, or else Arduino may get bricked, and will need another Arduino to burn its boot loader.

You can view a similar article on Arduino Watchdog Timer with Interrupt and Reset [Here]

You can also find a great video tutorial on the concept below:


WDT video Tutorial




//Use of Arduino Watchdog Timer (WDT) for Reset

#include <avr/wdt.h>


void setup(){
  Serial.begin(9600);
  Serial.println("Setup started :");
  // make a delay before enable WDT 
  // this delay help to complete all initial tasks
  delay(2000);
  wdt_enable(WDTO_2S);
}

void loop(){
  Serial.println("LOOP started ! ");
  for(int i=0; i<=5; i++){
    Serial.print("Loop : ");
    Serial.print(i);
    Serial.println();
    delay(1000);
    wdt_reset();
  }
  //infinity loop to hang MCU for displaying WDT function
  while(1){}
}

Please do share if you have any interesting ideas to modify the code and the applications where you  used this.
Thank you😉

Adityapratap Singh

Post a Comment