0%

Arduino resets when disabling Watchdog Timer!

While working on a small arduino project that will run on batteries I needed to enable deep sleep mode to reduce the energy consumption when not needed until the arduino is awaken by an external interrupt. However, I am also using the Watchdog Timer (WDT) to make sure the system will not hang up or get into in infinite loop due to some bug or unknown situation. The maximum time that the WDT on the Atmega328p supports is 8 seconds, and if 8 seconds has passed before calling wdt_reset() the WDT will reset the microcontroller, however, my boards will sleep for way longer than 8 seconds so I have to disable the WDT before going to sleep and enable it back once waken up.

Disabling WDT should be an easy thing to do by simply calling wdt_disable(), however, calling this statement also caused the atmega328 to reset!

After looking into the content of the header file “avr/wdt.h” it seems like the Arduino IDE or AVR compiler (not sure who is responsible) is not defining the correct board/chip even though I set the correct board & processor in the Arduino IDE. And the way I solved the issue was to define the correct microcontroller before including the wdt.h:

1
2
#define \_\_AVR\_ATmega328P__
#include <avr/wdt.h>

Now the code works as intended without causing a reset.