Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Manufacturing Technology >> Manufacturing process

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups

Components and supplies

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups
Arduino UNO
×1

Apps and online services

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups
Arduino IDE

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.

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups

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.

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups

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

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups

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

Understanding the Watchdog Timer: How It Prevents Microcontroller Hang-Ups

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

Code

  • Full 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){}
}

Manufacturing process

  1. Understanding the Oxygen (O2) Sensor: Role, Placement, and Failure Signs
  2. Understanding AS9100: The Aerospace Quality Management Standard
  3. What Is A2 Steel? A Versatile, Cost‑Effective Tool‑Steel Grade for Industrial and Woodworking Applications
  4. Sand Casting Explained: The Fundamentals of Metal Mold Fabrication
  5. Insert Molding Explained: Boost Efficiency & Cut Costs in Multi-Material Production
  6. Understanding Metal Casting: Process, Benefits, and Applications
  7. Demystifying 3D Printing: A Clear Guide for Innovators
  8. Precision Grinding: Definition, Applications, and Benefits
  9. Understanding GD&T Symmetry: Definition, Application, and Alternatives
  10. Decoding 'N' in Pump Calculations: Its Role and Significance