Arduino Power Down

How to put the Arduino to sleep

Preface

If you run your Arduino on a battery it’s important to save as much energy as possible. One of the best ways to do this is to put your Arduino into a sleep mode. For example, if you want to read a sensor every five minutes you can put it to sleep for 5 mins, wake up measure and sleep again.

This tutorial shows how to put the Arduino in one of the sleep modes and wake it up with an external trigger.

What you need

Arduino (favors Uno, Nano)

at least one jumper wire to simulate the interrupt

Sleep Modes

The Atmega 328 provides six sleep modes. You can find an overview here (Page 34). The best sleep mode to save most energy is the power down mode. It disables most internal things and you can only wake it up with an external trigger or the WDT. So this mode will be used in this example.

AVR-libc

In order to use the sleep modes, the Atmega provides you need to include the avr-libc. The library gives you more control over you Arduino. The sleep modes are defined in the avr/sleep.h header file.

#include <avr/sleep.h>

You can find the official documentation here: https://www.nongnu.org/avr-libc/user-manual/group__avr__sleep.html

Code example

The following code will put the Arduino into the power down mode:

void sleepFunction() {

sleep_enable(); // set the sleep enable bit

set_sleep_mode(SLEEP_MODE_PWR_DOWN); // attach the interrupt pin

// use int0 pin => pin 2 on the arduino board

// define which function to call if the interrupt is triggered

// If you don't want to trigger on a falling edge you

// need to change the third parameter

attachInterrupt(0, interruptFunction, LOW); // some code ... sleep_cpu(); // put the arduino into sleep mode // code after wake up

} void interruptFunction() {

// some code ... sleep_disable(); // clear the sleep enable bit

detachInterrupt(0); // detatch the interrupt

}

The sleepFunction first sets the sleep enable bit. The set_sleep_mode defines which sleep mode will be executed. If you want to use another mode you just need to change the parameter. In order to wake up the Arduino, you need an external interrupt. If an interrupt is triggered the interruptFunction will be called. At first, it will execute some code and then disable the sleep enable bit and detach the interrupt event. The sleep_cpu() function finally puts the Arduino into the selected sleep mode.

You can find the whole code here: https://gist.github.com/maraujop/893408

Test

You could simply test it with a jumper wire. Put the first end into the pin 2 on the board. The second end could be unplugged if you set the interrupt pin mode to pullup. Otherwise, you need to insert it into VCC. When the Arduino enters the sleep mode (LED turn off) you need to insert the second end to GND to rise the trigger event and wake up the Arduino.

It’s more comfortable to use a buzzer for this. Simply connect it to pin 2 and to GND. If you push the button the Arduino will wake up.

Conclusion

It’s the best way to save very much energy but it’s not everything. To reach the optimum there are a few more things you need to do.

To use this sleep function you will need external hardware which will raise the interrupt in a periodic time to let the Arduino do his work. You could use a monoflop for example. In my opinion, the best way is to use an RTC module.

References