What if your Arduino could rest like you do, saving energy until it's really needed?
Why Arduino sleep modes? - Purpose & Use Cases
Imagine you have a battery-powered Arduino project, like a weather sensor outside your house. You want it to run for weeks without changing batteries. But if the Arduino stays fully awake all the time, it quickly uses up the battery.
Manually turning off parts of the Arduino or adding delays can help a bit, but it's tricky and often still wastes power. You might forget to turn off some parts, or the Arduino might miss important events because it's sleeping the wrong way.
Arduino sleep modes let you put the board into different low-power states easily. The Arduino can sleep until something important happens, like a button press or a sensor signal, then wake up and work. This saves battery without missing anything important.
delay(10000); // wait 10 seconds, wastes power // no real sleep mode used
#include <avr/sleep.h>
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu(); // Arduino sleeps until interruptIt enables your Arduino projects to run longer on batteries by using power only when needed, making them smarter and more efficient.
A remote wildlife camera that sleeps most of the time and wakes only when motion is detected, saving battery and capturing important moments.
Manual power saving is hard and often incomplete.
Arduino sleep modes provide easy, reliable low-power states.
They help projects run longer and respond only when needed.
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
