0
0
Arduinoprogramming~5 mins

Arduino sleep modes

Choose your learning style9 modes available
Introduction

Arduino sleep modes help save battery by putting the board into low power states when it is not doing anything important.

When your Arduino project runs on batteries and you want it to last longer.
When your device waits for a button press or sensor event and can sleep in the meantime.
When you want to reduce power use in a remote sensor or data logger.
When your Arduino controls something only occasionally and can rest between tasks.
Syntax
Arduino
1. Include the sleep library:
#include <avr/sleep.h>

2. Set the sleep mode:
set_sleep_mode(SLEEP_MODE_IDLE); // or other modes like SLEEP_MODE_PWR_DOWN

3. Put Arduino to sleep:
sleep_enable();
sleep_cpu();
sleep_disable();

You must include <avr/sleep.h> to use sleep functions.

Choose the sleep mode based on how much power you want to save and what should stay active.

Examples
This example puts Arduino into the IDLE sleep mode once in setup, then wakes up immediately (for demo).
Arduino
#include <avr/sleep.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Going to sleep now");
  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  Serial.println("Woke up!");
}

void loop() {
  // Nothing here
}
This example uses the deepest sleep mode (POWER DOWN) to save the most power. Wakes after about 8 seconds using the Watchdog timer.
Arduino
#include <avr/sleep.h>
#include <avr/wdt.h>

void goToSleep() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  wdt_enable(WDTO_8S);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  wdt_disable();
}

void setup() {
  Serial.begin(9600);
  Serial.println("Sleeping deeply...");
  goToSleep();
  Serial.println("Awake now!");
}

void loop() {}
Sample Program

This program puts Arduino into a power-save sleep mode 5 times, woken each time by the 1 second watchdog timer (total ~5 seconds), then prints a message.

Arduino
#include <avr/sleep.h>
#include <avr/wdt.h>

void setup() {
  Serial.begin(9600);
  delay(1000); // Wait for serial to start
  Serial.println("Arduino will sleep for 5 seconds...");

  set_sleep_mode(SLEEP_MODE_PWR_SAVE);
  sleep_enable();

  // Sleep 5 times, ~1s each using watchdog timer
  for (int i = 0; i < 5; i++) {
    wdt_enable(WDTO_1S);
    sleep_cpu();
    wdt_disable();
  }

  sleep_disable();
  Serial.println("Arduino woke up after sleep!");
}

void loop() {
  // Nothing here
}
OutputSuccess
Important Notes

Not all Arduino boards support all sleep modes; check your board's datasheet.

To wake from deep sleep, you usually need an interrupt like a button press or timer.

Use sleep_disable() after waking to return to normal operation.

Summary

Arduino sleep modes help save power by stopping or slowing parts of the chip.

Use set_sleep_mode() and sleep_cpu() to enter sleep.

Wake up using interrupts or timers to continue your program.