0
0
Arduinoprogramming~10 mins

Arduino sleep modes - Step-by-Step Execution

Choose your learning style9 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().