Project tutorial
What is Watchdog Timer ?

What is Watchdog Timer ? © MIT

Introduction to watchdog timer in microcontroller

  • 250 views
  • 0 comments
  • 1 respect

Components and supplies

Apps and online services

About this project

What the situation while your micro-controller confused in an infinity loop😖. Is there any case like hang or stuck your micro-controller while performing a task😖. What are the normal troubleshooting method ? Reset Button, Right ? Is it practically possible to do press on button all time ?😕. So, What about a device to do this task ? sounds good. Cool... !🙂.

Yes  ! Now we are discussing about such device which already inside on a micro-controller. That is Watchdog timer.

Watchdog Timer.

"A watchdog timer (WDT) is a hardware timer that automatically generates a  system reset if the main program neglects to periodically service it.  It is often used to automatically reset an embedded device that hangs  because of a software or hardware fault." (os.mbed.com/cookbook/WatchDog-Timer).

Feed the Dog !

If you have a dog in your home. You need to feed that dog on a regular interval. if you can't feed one day, it will bite you! Like this watchdog timer works.

We have a main part in program which runs over and over(loop). We are enabling watchdog timer is loaded with an initial value greater than the total delay in main program. Each time main program reset this timer. If any case 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. Likewise watchdog timer protect micro-controller from hang case.

So, let's try watchdog timer in Arduino  ;)

In Arduino UNO uses ATMEGA328P micro-controller.

Watchdog timer library

#include <avr/wdt.h>

Library is needed to use watchdog timer in Arduino

Enable Watchdog timer :

wdt_enable(WDT Reset Timer);

To enable watchdog timer, WDT RESET TIMER varies from 15ms - 8s

Eg: wdt_enable(WDT0_8S); --Enabled watchdog timer for 8Seconds

Reset watchdog timer

wdt_reset();

This function is used for resetting the watchdog timer. Reset function uses inside loop(). If your program uses a larger delay() which greater than threshold delay of watchdog timer, add reset function before that delay too. Else, It will reset Micro-controller before completing that task.

Disabling Watchdog timer

wdt_disable();

Example code :

#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_4S);
}
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
while(1){}
}

Output

Shoot your feedback, questions, discussion in the comment section.

Code

Full codeArduino
#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_4S);
}

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
  while(1){}
}

Comments

Similar projects you might like

More than an Hour Timer

Project tutorial by Ian Cumming

  • 6,907 views
  • 1 comment
  • 6 respects

GymGeneral - A Sports Interval Timer

Project tutorial by Mr_Glenn

  • 4,008 views
  • 0 comments
  • 4 respects

Jigsaw Puzzle Timer

Project tutorial by HomeMadeGarbage

  • 2,119 views
  • 1 comment
  • 4 respects

Arduino Kitchen Timer

Project tutorial by Team I and myself

  • 73,675 views
  • 45 comments
  • 99 respects

Stopwatch and Lap Timer with Arduino

Project tutorial by LAGSILVA

  • 22,643 views
  • 20 comments
  • 50 respects

Arduino Mutitasking: Clock + Stopwatch + Timer

Project tutorial by LAGSILVA

  • 24,695 views
  • 13 comments
  • 70 respects
Add projectSign up / Login