Bird
Raised Fist0
Arduinoprogramming~10 mins

Arduino sleep modes - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Arduino sleep modes
Start Program
↓
Setup Sleep Mode
↓
Enable Sleep
↓
Enter Sleep
↓
Sleep Mode Active
↓
Wake Up Interrupt
↓
Resume Execution
↓
Repeat or End
The Arduino sets up a sleep mode, enables it, then enters sleep. It stays asleep until an interrupt wakes it, then resumes execution.
Execution Sample
Arduino
#include <avr/sleep.h>
void setup() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_cpu();
}
void loop() {}
This code puts the Arduino into the deepest sleep mode once during setup.
Execution Table
StepActionSleep Mode SetSleep EnabledCPU StateOutput
1Start setup()NoneDisabledAwakeNone
2Call set_sleep_mode(SLEEP_MODE_PWR_DOWN)Power DownDisabledAwakeNone
3Call sleep_enable()Power DownEnabledAwakeNone
4Call sleep_cpu()Power DownEnabledSleepingNone
5CPU sleeps until interruptPower DownEnabledSleepingNone
6Interrupt wakes CPUPower DownDisabledAwakeNone
7Resume normal executionPower DownDisabledAwakeNone
8End of setup(), loop() runs emptyPower DownDisabledAwakeNone
💡 CPU wakes up on interrupt, sleep_cpu() ends, program continues
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 6Final
sleep_modeNonePower DownPower DownPower DownPower DownPower Down
sleep_enabledDisabledDisabledEnabledEnabledDisabledDisabled
cpu_stateAwakeAwakeAwakeSleepingAwakeAwake
Key Moments - 3 Insights
Why does the CPU state change from 'Sleeping' back to 'Awake' after an interrupt?
When an interrupt occurs, it wakes the CPU from sleep mode, so the CPU state changes from 'Sleeping' to 'Awake' as shown between steps 5 and 6 in the execution table.
Does the Arduino keep sleeping forever after sleep_cpu() is called?
No, the Arduino stays asleep only until an interrupt wakes it. After waking, the program continues normally, as seen in step 7.
Is sleep_enable() enough to put the Arduino to sleep?
No, sleep_enable() just prepares the sleep mode. The actual sleep starts when sleep_cpu() is called, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the CPU state immediately after calling sleep_cpu()?
ASleeping
BAwake
CDisabled
DPower Down
💡 Hint
Check the 'CPU State' column at step 4 in the execution table.
At which step does the CPU wake up from sleep mode?
AStep 3
BStep 5
CStep 6
DStep 8
💡 Hint
Look for the step where 'CPU State' changes from 'Sleeping' to 'Awake' in the execution table.
If sleep_enable() was not called, what would be the CPU state after sleep_cpu()?
ASleeping
BAwake
CPower Down
DUndefined
💡 Hint
Refer to the variable_tracker for 'sleep_enabled' and 'cpu_state' changes.
Concept Snapshot
Arduino Sleep Modes Quick Guide:
- Use set_sleep_mode() to choose sleep type.
- Call sleep_enable() to prepare sleep.
- Call sleep_cpu() to enter sleep.
- CPU sleeps until an interrupt wakes it.
- After waking, program continues normally.
Full Transcript
This visual trace shows how Arduino sleep modes work step-by-step. First, the program starts awake. Then it sets the sleep mode to Power Down. Next, sleep is enabled. When sleep_cpu() is called, the CPU goes to sleep. It stays asleep until an interrupt wakes it up. After waking, the CPU state returns to awake and the program continues. Variables like sleep_mode and sleep_enabled track the setup, while cpu_state shows if the CPU is awake or sleeping. Key moments include understanding that sleep_cpu() actually puts the CPU to sleep, and interrupts wake it back up. The quizzes test understanding of CPU state changes and the role of sleep_enable().

Practice

(1/5)
1. Which Arduino function is used to set the type of sleep mode before entering sleep?
easy
A. set_sleep_mode()
B. sleep_cpu()
C. attachInterrupt()
D. delay()

Solution

  1. Step 1: Understand sleep mode setup

    The function set_sleep_mode() is used to choose which sleep mode the Arduino will enter.
  2. Step 2: Differentiate from sleep execution

    The function sleep_cpu() actually puts the CPU to sleep, but does not set the mode.
  3. Final Answer:

    set_sleep_mode() -> Option A
  4. Quick Check:

    Set mode = set_sleep_mode() [OK]
Hint: Set mode first with set_sleep_mode() before sleeping [OK]
Common Mistakes:
  • Confusing set_sleep_mode() with sleep_cpu()
  • Thinking attachInterrupt() sets sleep mode
  • Using delay() to enter sleep
2. Which of the following is the correct syntax to put the Arduino into sleep mode after setting it?
easy
A. sleep_mode();
B. sleep_cpu();
C. sleep();
D. enter_sleep();

Solution

  1. Step 1: Identify the sleep function

    The function sleep_cpu() is the correct call to put the CPU into sleep mode after setting it.
  2. Step 2: Check other options

    sleep_mode() is not a valid function, nor are sleep() or enter_sleep().
  3. Final Answer:

    sleep_cpu(); -> Option B
  4. Quick Check:

    Sleep call = sleep_cpu() [OK]
Hint: Use sleep_cpu() to enter sleep after setting mode [OK]
Common Mistakes:
  • Using sleep_mode() instead of sleep_cpu()
  • Trying to call sleep() which doesn't exist
  • Confusing function names
3. What will the following Arduino code do?
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
// CPU sleeps here
sleep_disable();
medium
A. The CPU enters power-down sleep mode and stays asleep until an interrupt wakes it
B. The CPU enters power-down sleep mode and wakes immediately
C. The CPU does not sleep because sleep_enable() is missing
D. The CPU enters idle mode instead of power-down

Solution

  1. Step 1: Analyze sleep mode setting

    The code sets the sleep mode to power-down with set_sleep_mode(SLEEP_MODE_PWR_DOWN);.
  2. Step 2: Understand sleep flow

    Calling sleep_enable() then sleep_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.
  3. Final Answer:

    The CPU enters power-down sleep mode and stays asleep until an interrupt wakes it -> Option A
  4. Quick Check:

    Power-down sleep = stays asleep until interrupt [OK]
Hint: Power-down sleep waits for interrupt to wake CPU [OK]
Common Mistakes:
  • Assuming CPU wakes immediately without interrupt
  • Forgetting sleep_enable() disables sleep
  • Confusing idle mode with power-down
4. Identify the error in this Arduino sleep code snippet:
set_sleep_mode(SLEEP_MODE_IDLE);
sleep_cpu();
sleep_enable();
medium
A. set_sleep_mode() is missing a parameter
B. No error, code is correct
C. sleep_cpu() should be replaced with sleep_mode()
D. sleep_enable() must be called before sleep_cpu()

Solution

  1. Step 1: Check function order

    The function sleep_enable() must be called before sleep_cpu() to enable sleep mode properly.
  2. Step 2: Verify other parts

    set_sleep_mode() is correctly called with a valid mode. sleep_cpu() is the correct function to enter sleep.
  3. Final Answer:

    sleep_enable() must be called before sleep_cpu() -> Option D
  4. Quick Check:

    Enable sleep before sleeping [OK]
Hint: Call sleep_enable() before sleep_cpu() to avoid errors [OK]
Common Mistakes:
  • Calling sleep_cpu() before sleep_enable()
  • Confusing sleep_cpu() with sleep_mode()
  • Missing parameters in set_sleep_mode()
5. You want to save maximum power on your Arduino but still wake it up using a button press on pin 2. Which sleep mode and wake method should you use?
hard
A. Use SLEEP_MODE_IDLE and poll the button in loop()
B. Use SLEEP_MODE_STANDBY and wake with delay()
C. Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2
D. Use SLEEP_MODE_ADC and check button state with analogRead()

Solution

  1. Step 1: Choose lowest power sleep mode

    SLEEP_MODE_PWR_DOWN uses the least power by shutting down most parts of the chip.
  2. 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.
  3. Final Answer:

    Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 -> Option C
  4. Quick Check:

    Lowest power + external interrupt wake = Use SLEEP_MODE_PWR_DOWN and attach an external interrupt on pin 2 [OK]
Hint: Lowest power sleep needs external interrupt wake [OK]
Common Mistakes:
  • Using idle mode which uses more power
  • Trying to wake with delay() which doesn't work
  • Polling button instead of using interrupt
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard