0
0
AutocadHow-ToBeginner · 4 min read

How to Use Sleep Mode in Arduino for Power Saving

To use sleep mode in Arduino, include the avr/sleep.h library, set the desired sleep mode with set_sleep_mode(), and call sleep_mode() to put the board to sleep. Use interrupts to wake the Arduino from sleep and continue running your program.
📐

Syntax

Here is the basic syntax to use sleep mode in Arduino:

  • #include <avr/sleep.h>: Includes the sleep library.
  • set_sleep_mode(mode);: Sets the sleep mode (e.g., SLEEP_MODE_IDLE, SLEEP_MODE_PWR_DOWN).
  • sleep_enable();: Enables sleep mode.
  • sleep_mode();: Puts the Arduino to sleep.
  • sleep_disable();: Disables sleep mode after waking up.

You usually set up an interrupt to wake the Arduino from sleep.

arduino
#include <avr/sleep.h>

void setup() {
  // Setup code here
}

void loop() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN); // Choose sleep mode
  sleep_enable();                     // Enable sleep mode
  sleep_mode();                       // Go to sleep
  sleep_disable();                    // Wake up here
  // Continue running
}
💻

Example

This example puts the Arduino into power-down sleep mode and wakes it up when a button connected to pin 2 is pressed. It demonstrates how to save power and respond to external events.

arduino
#include <avr/sleep.h>
#include <avr/interrupt.h>

const int buttonPin = 2;
volatile bool wokeUp = false;

void wakeUp() {
  wokeUp = true; // This function is called when interrupt occurs
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(buttonPin, INPUT_PULLUP); // Button with pull-up resistor
  attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, FALLING);
  Serial.begin(9600);
}

void loop() {
  Serial.println("Going to sleep...");
  delay(1000); // Wait a moment before sleeping

  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  noInterrupts();           // Disable interrupts temporarily
  attachInterrupt(digitalPinToInterrupt(buttonPin), wakeUp, FALLING); // Ensure interrupt attached
  interrupts();             // Enable interrupts
  sleep_mode();             // Sleep here

  sleep_disable();          // Wake up here
  Serial.println("Woke up!");
  digitalWrite(LED_BUILTIN, HIGH); // Turn LED on to show wake up
  delay(2000);
  digitalWrite(LED_BUILTIN, LOW);
}
Output
Going to sleep... Woke up!
⚠️

Common Pitfalls

Common mistakes when using sleep mode in Arduino include:

  • Not setting the correct sleep mode for your needs, causing higher power use.
  • Forgetting to enable interrupts before sleeping, so the Arduino never wakes up.
  • Not detaching or properly managing interrupts, which can cause unexpected behavior.
  • Using delay() inside sleep mode, which does not work as expected because the CPU is stopped.

Always test your wake-up source and ensure interrupts are correctly configured.

arduino
/* Wrong way: No interrupt enabled, Arduino never wakes up */
#include <avr/sleep.h>

void setup() {
  Serial.begin(9600);
}

void loop() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode(); // Arduino sleeps but no interrupt to wake up
  sleep_disable();
  Serial.println("This will never print if no interrupt");
  delay(1000);
}

/* Right way: Attach interrupt to wake up */
#include <avr/sleep.h>
#include <avr/interrupt.h>

volatile bool wokeUp = false;

void wakeUp() {
  wokeUp = true;
}

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING);
}

void loop() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  if (wokeUp) {
    Serial.println("Woke up by interrupt!");
    wokeUp = false;
  }
  delay(1000);
}
📊

Quick Reference

Here is a quick summary of common sleep modes and their power usage:

Sleep ModeDescriptionPower Consumption
SLEEP_MODE_IDLECPU stopped, peripherals runningModerate
SLEEP_MODE_ADCCPU and ADC stoppedLower
SLEEP_MODE_PWR_SAVECPU stopped, timer runningLow
SLEEP_MODE_STANDBYCPU stopped, oscillator runningVery Low
SLEEP_MODE_PWR_DOWNAlmost everything stoppedLowest

Key Takeaways

Include avr/sleep.h and set the sleep mode before calling sleep_mode().
Use interrupts to wake the Arduino from sleep; otherwise, it will stay asleep indefinitely.
Choose the sleep mode that balances power saving and required functionality.
Avoid using delay() during sleep as it does not work when the CPU is stopped.
Test your sleep and wake-up code carefully to ensure reliable operation.