Let your Arduino get enough sleep

The sensor I am building is based on an Arduino Pro Mini (Atmega328p) with ESP8266 wifi module and a sensor. It will run on batteries with a sample rate of 1 sample per hour. With such low sampling rate it is very obvious that I need to make my microcontroller and the wifi module sleep to reduce power consumption. To wake up the Atmega328p MCU from power down sleep I have to use either the Watchdog Timer (WDT) or an external interrupt. The problem with the WDT on that MCU is that the maximum sleep time it supports is 8 seconds. And to make it sleep longer you need to put the sleep statement in a loop that puts it back into sleep until it reaches a certain count value then resume working.

This is an example of such solution:

for (int i = 1; i <= count ; i++) {
    sleep_cpu ();
}

However, in 1 hour of needed sleep the MCU will wake up 450 times!

Continue reading “Let your Arduino get enough sleep”

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:

#define __AVR_ATmega328P__
#include <avr/wdt.h>

Now the code works as intended without causing a reset.