Watchdog Timer In Arduino - Tutorialspoint

  • Home
  • Whiteboard
  • Online Compilers
  • Practice
  • Articles
  • AI Assistant
  • Jobs
  • Tools
  • Corporate Training
  • Courses
  • Certifications
Menu Categories Login
  • Switch theme
  • SQL
  • HTML
  • CSS
  • Javascript
  • Python
  • Java
  • C
  • C++
  • PHP
  • Scala
  • C#
  • Tailwind CSS
  • Node.js
  • MySQL
  • MongoDB
  • PL/SQL
  • Swift
  • Bootstrap
  • R
  • Machine Learning
  • Blockchain
  • Angular
  • React Native
  • Computer Fundamentals
  • Compiler Design
  • Operating System
  • Data Structure and Algorithms
  • Computer Network
  • DBMS
  • Excel
Technical Questions and Answers
  • Data Structure Data Structure
  • Networking Networking
  • RDBMS RDBMS
  • Operating System Operating System
  • Java Java
  • MS Excel MS Excel
  • iOS iOS
  • HTML HTML
  • CSS CSS
  • Android Android
  • Python Python
  • C Programming C Programming
  • C++ C++
  • C# C#
  • MongoDB MongoDB
  • MySQL MySQL
  • Javascript Javascript
  • PHP PHP
  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who
Watchdog timer in Arduino ArduinoHardwareSoftware & Coding

A watchdog timer is an essential part of any microcontroller. It resets the program if the program gets stuck anywhere. Very briefly, this is how the watchdog timer works −

  • The timer keeps incrementing.

  • The program has to ensure that it keeps resetting the timer, i.e. does not allow it to overflow.

  • If the timer overflows, it means that the program was stuck somewhere and therefore was unable to reset the timer. An interrupt is generated on timer overflow which resets the microcontroller.

To implement watchdog timer in Arduino, we use the avr wdt library.

The code is given below −

#include<avr/wdt.h> void setup() {    Serial.begin(9600);    wdt_disable(); //Disable WDT    delay(3000);    wdt_enable(WDTO_2S); //Enable WDT with a timeout of 2 seconds    Serial.println("WDT Enabled"); } void loop() {    for(int i = 0; i<5; i++)    {       Serial.println("Looping");       delay(1000);       wdt_reset(); //Reset the watchdog    }    while(1); //Watchdog timer should get triggered here }

As you can see, we initialize Serial and first disable watchdog timer. Then a delay of 3 seconds is introduced. The program won't reset here, because the watchdog is disabled. Now, the watchdog timer with a timeout of 2 seconds is enabled. It means, if the program doesn't reset this timer within every 2 seconds, then the watchdog will get triggered and restart the microcontroller.

Within the loop, we first print to the Serial for 5 seconds, making sure to reset the watchdog every second. The program works fine so far. Then, we enter into an infinite while loop. Over here, since we are not resetting the wdt, it will get triggered and restart the Arduino.

Note that the preset watchdog timeout values are available from 15 ms to 8 s.

Yash Sanghvi Yash Sanghvi Updated on: 2021-07-24T12:14:23+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started Print Page Previous Next Advertisements

Tag » Arduino Reset Watchdog Timer