Description
As the title suggests, Watchdog timer reset is used to remove issues/anomaly ocurring during system functioning. For brief description refer my other article Arduino Watchdog Timer with Reset.
In this example I'm using an Arduino UNO employing an ATmega328p chip as an example for setting up WDT interrupt and 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)
Here if WDT is not reset when the counter reaches the user defined value, the WDT first triggers an interrupt (May be used to save to save program progess or useful information) and then resets the micro-controller.
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 also find a great video tutorial on the concept below:
WDT video Tutorial
//Use of Arduino Watchdog Timer (WDT) for Interrupt and Reset
#include <avr/wdt.h>
volatile bool flag = LOW;
ISR (WDT_vect){
flag = HIGH;
wdt_enable(WDTO_4S); //WDT interrupt & reset timer set for 4 Seconds
}
void setup(){
Serial.begin(9600);
Serial.println("Setup started :");
// make a delay before enable WDT
// this delay help to complete all initial tasks
delay(1000);
noInterrupts (); // timed sequence below
MCUSR = 0; // reset various flags
WDTCSR |= 0b00011000; // see ATmega328p datasheet, set WDCE, WDE
WDTCSR = 0b01000000 | 0b100000; // set WDIE, and appropriate delay
wdt_reset();
interrupts();
}
void loop(){
if(flag == HIGH){
Serial.println("Triggered");
flag = LOW;
}
}
Please do share if you have any interesting ideas to modify the code and the applications where you used this.Thank you😉Adityapratap Singh
