Tuesday, March 28, 2017

Arduino and watchdog timer

by / Saturday, 16 June 2012 / Published in Arduino

arduino-wdt Have you ever encountered situations when your Arduino-based device hangs?

There are many reasons for this - from millis() overflow in 49 days to moisture in outdoor sensors (causing them not to respond)...

But what if your device is not easily accessible and\or works for long periods without human supervision? Such situations should be prevented in this case. You don't want your plant watering system that you left to look after your plants and flowers during your vacation to hang on the second day, leaving your loved plants without water, really?

Most MCUs, including those used in Arduino, has a special feature, called Watchdog timer. Once it is configured, it checks for 'heartbeat' signals (or commands) that your MCU should issue. As soon as the time since the last pulse exceeds configured treshold, watchdog initiates reset sequence. This is really convenient, and ensures that your system will be in hanged state for no more than watchdog threshold.

I once encountered sudden hangs of my standalone arduino-based device. I run all possible tests, double-checked all circuitry, replaced MCU - with no effect. Finally I examined wires to external OneWire DS18B20 temperature sensor. The resistance between Vcc and GND was 600 Ohm! Then I looked through the window - it was foggy and raining... The insulation of the sensor was rather old and had a leak. As soon as I disconnected the wires leading from the sensor all problems were gone.
Semyon Tushev

I don't know, why there's not even a word about this in Arduino docs. Such things save a lot of trouble.

Quoting ATMega Datasheet:

ATmega48/88/168/328 has an Enhanced Watchdog Timer (WDT). The WDT is a timer counting
cycles of a separate on-chip 128 kHz oscillator. The WDT gives an interrupt or a system reset
when the counter reaches a given time-out value. In normal operation mode, it is required that
the system uses the WDR - Watchdog Timer Reset - instruction to restart the counter before the
time-out value is reached. If the system doesn't restart the counter, an interrupt or system reset
will be issued.

Using Watchdog timer in Arduino projects

First of all, you need to include corresponding header file:

#include <avr/wdt.h>

In the next step we should enable WDT as well as configure it:


In most cases you'll have to put that line to init section - like setup(){} function, however, Watchdog timer may be enabled at any moment.

WDTO_2S is a constant that means threshold interval of 2 seconds. (It is defined inside header file). If Watchdog receive no "pat" signal for this period, it will "bite" back - reboot the MCU.

Threshold values are pre-programmed (you cannot set any value you wish).


Threshold value Constant name Supported on
15 ms WDTO_15MS ATMega 8, 168, 328, 1280, 2560
30 ms WDTO_30MS ATMega 8, 168, 328, 1280, 2560
60 ms WDTO_60MS ATMega 8, 168, 328, 1280, 2560
120 ms WDTO_120MS ATMega 8, 168, 328, 1280, 2560
250 ms WDTO_250MS ATMega 8, 168, 328, 1280, 2560
500 ms WDTO_500MS ATMega 8, 168, 328, 1280, 2560
1 s WDTO_1S ATMega 8, 168, 328, 1280, 2560
2 s WDTO_2S ATMega 8, 168, 328, 1280, 2560
4 s WDTO_4S ATMega 168, 328, 1280, 2560
8 s WDTO_8S ATMega 168, 328, 1280, 2560

Finally, you should put the following line that tells watchdog that everything's ok.


Sure, it should be called from app's cycle more often than threshold period you have set for watchdog ends (at least, during normal operation mode). The beginning of loop() cycle could be a good idea, however, this depends on your program.

Also it's necessary to include it to every time-consuming operation. For instance, if you have data transmission cycle in your main loop, and this cycle lasts for a couple of seconds, you should put wdt_reset(); there as well, or your board may reset during transmission.

When choosing a treshold, take all time- and timing-related values into account: delays, function tresholds, bus timeouts and speed. Threshold value should always be greater than the largest value - at least 1.5 times greater in most cases. However, this depend on your application.

Another thing to take in mind is that it will reset the MCU, but not another hardware in your project, so you have to do it manually during Arduino initialization - if you have such hardware, sure.

The complete setup look like:
#include <avr/wdt.h>
void setup(){
void loop(){
//do stuff here
In case that stuff takes more than 2s, the board will reset itself.


Quoting ATMega Datasheet:

Note: If the Watchdog is accidentally enabled, for example by a runaway pointer or brown-out
condition, the device will be reset and the Watchdog Timer will stay enabled. If the code is not
set up to handle the Watchdog, this might lead to an eternal loop of time-out resets. To avoid this
situation, the application software should always clear the Watchdog System Reset Flag
(WDRF) and the WDE control bit in the initialisation routine, even if the Watchdog is not in use.

In older bootloaders (for 168, 2K size), the code checks whether it is running after WDT reset or not, and skips waiting for new program in this case. (See this file, line 286).

However, I could not find something similar in Optiboot bootloader (If I'm wrong, correct me please!), used by Arduino UNO and other new boards. It seems that they sacrificed this check for size. So, it is possible that watchdog timer will be enabled after reset, so -  if you set it's value too low, your board may never come out of bootloader stage. So, to be always on the safe side, set watchdog to longer periods (2S and more).

I hope this article will help you to make your projects more safe and reliable ;)

Read 11897 times Last modified on Sunday, 12 March 2017 11:32

Do you like this? Please share!

Simon Tushev

Simon is IT professional with interests in web design, electronics, photography and astronomy. He writes about PHP, Yii, Joomla!, Arduino and several other topics.


Here I publish articles related to my areas of interest, my brief notes that may save some time to others, as well as some of my works.

This site is a work in progress. I will add more sections in the future, such as

  • More photos to photogallery
  • More Joomla!-related articles
  • ...



New Posts

Using dynamic GeoIP module with nginx (CentOS)

nginx introduced dynamic module support in v. 1.9.11. This brief article provides step-by-step procedure...

13-04-2016 in Linux, BSD, Unix


Visual difference between pseudo and true random data

This is is "random" image generated using PHP rand() function: And this is an image...

20-11-2015 in Blog


Best EEPROM practices

(this article is a work in progress). My own advice will be added later...   Having...

12-10-2015 in Arduino

Scroll to top

We use cookies, just to track visits to our website, we store no personal details Privacy policy