Arduino sleep modes help save battery by putting the board into low power states when it is not doing anything important.
Arduino sleep modes
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.
#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 }
#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() {}
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.
#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 }
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.
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.
Practice
Solution
Step 1: Understand sleep mode setup
The functionset_sleep_mode()is used to choose which sleep mode the Arduino will enter.Step 2: Differentiate from sleep execution
The functionsleep_cpu()actually puts the CPU to sleep, but does not set the mode.Final Answer:
set_sleep_mode() -> Option AQuick Check:
Set mode = set_sleep_mode() [OK]
- Confusing set_sleep_mode() with sleep_cpu()
- Thinking attachInterrupt() sets sleep mode
- Using delay() to enter sleep
Solution
Step 1: Identify the sleep function
The functionsleep_cpu()is the correct call to put the CPU into sleep mode after setting it.Step 2: Check other options
sleep_mode()is not a valid function, nor aresleep()orenter_sleep().Final Answer:
sleep_cpu(); -> Option BQuick Check:
Sleep call = sleep_cpu() [OK]
- Using sleep_mode() instead of sleep_cpu()
- Trying to call sleep() which doesn't exist
- Confusing function names
set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_cpu(); // CPU sleeps here sleep_disable();
Solution
Step 1: Analyze sleep mode setting
The code sets the sleep mode to power-down withset_sleep_mode(SLEEP_MODE_PWR_DOWN);.Step 2: Understand sleep flow
Callingsleep_enable()thensleep_cpu()puts the CPU to sleep in power-down mode. It stays asleep until an interrupt or reset wakes it. After waking,sleep_disable()disables sleep.Final Answer:
The CPU enters power-down sleep mode and stays asleep until an interrupt wakes it -> Option AQuick Check:
Power-down sleep = stays asleep until interrupt [OK]
- Assuming CPU wakes immediately without interrupt
- Forgetting sleep_enable() disables sleep
- Confusing idle mode with power-down
set_sleep_mode(SLEEP_MODE_IDLE); sleep_cpu(); sleep_enable();
Solution
Step 1: Check function order
The functionsleep_enable()must be called beforesleep_cpu()to enable sleep mode properly.Step 2: Verify other parts
set_sleep_mode()is correctly called with a valid mode.sleep_cpu()is the correct function to enter sleep.Final Answer:
sleep_enable() must be called before sleep_cpu() -> Option DQuick Check:
Enable sleep before sleeping [OK]
- Calling sleep_cpu() before sleep_enable()
- Confusing sleep_cpu() with sleep_mode()
- Missing parameters in set_sleep_mode()
Solution
Step 1: Choose lowest power sleep mode
SLEEP_MODE_PWR_DOWN uses the least power by shutting down most parts of the chip.Step 2: Select wake method
To wake from power-down mode, an external interrupt on pin 2 (using attachInterrupt) is needed because polling or delay() won't wake the CPU.Final Answer:
Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 -> Option CQuick Check:
Lowest power + external interrupt wake = Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 [OK]
- Using idle mode which uses more power
- Trying to wake with delay() which doesn't work
- Polling button instead of using interrupt
Ready to prove it?
Pass the test and this pattern moves unknown
