📖 Scenario: You have a small Arduino project that runs on battery power. To save battery, you want the Arduino to sleep when it is not doing anything and wake up when a button is pressed.
🎯 Goal: Build a simple Arduino program that puts the board into sleep mode and wakes it up using a button press.
📋 What You'll Learn
Use the avr/sleep.h library to control sleep modes
Set up a button on pin 2 to wake the Arduino from sleep
Put the Arduino into sleep mode inside the loop()
Print a message to the Serial Monitor when the Arduino wakes up
💡 Why This Matters
🌍 Real World
Battery-powered devices like remote sensors or wearable gadgets use sleep modes to save power and extend battery life.
💼 Career
Understanding sleep modes is important for embedded systems engineers working on low-power IoT devices and hardware optimization.
Progress0 / 4 steps
1
Setup button input and Serial communication
Write code to set pin 2 as an input with an internal pull-up resistor. Also, start Serial communication at 9600 baud in the setup() function.
Arduino
Hint
Use pinMode(2, INPUT_PULLUP); to set the button pin. Use Serial.begin(9600); to start serial communication.
2
Configure sleep mode and interrupt
Add code to set the sleep mode to SLEEP_MODE_PWR_DOWN and attach an interrupt to pin 2 that will wake the Arduino when the button is pressed.
Arduino
Hint
Use set_sleep_mode(SLEEP_MODE_PWR_DOWN); to set the sleep mode. Use attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW); to attach the interrupt.
3
Put Arduino to sleep inside loop
Inside the loop(), add code to put the Arduino to sleep using sleep_enable() and sleep_cpu(). Make sure to disable sleep after waking up.
Arduino
Hint
Use sleep_enable(); before sleep_cpu(); and sleep_disable(); after waking up. Print messages before and after sleep.
4
Test and observe output
Upload the complete program and observe the Serial Monitor. The Arduino should print Going to sleep..., then sleep until the button on pin 2 is pressed, then print Woke up!. Write the exact output you see after pressing the button once.
Arduino
Hint
Open the Serial Monitor at 9600 baud. Press the button connected to pin 2 once. You should see the messages printed before and after sleep.
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
Step 1: Understand sleep mode setup
The function set_sleep_mode() is used to choose which sleep mode the Arduino will enter.
Step 2: Differentiate from sleep execution
The function sleep_cpu() actually puts the CPU to sleep, but does not set the mode.
Final Answer:
set_sleep_mode() -> Option A
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
Step 1: Identify the sleep function
The function sleep_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 are sleep() or enter_sleep().
Final Answer:
sleep_cpu(); -> Option B
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
Step 1: Analyze sleep mode setting
The code sets the sleep mode to power-down with set_sleep_mode(SLEEP_MODE_PWR_DOWN);.
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.
Final Answer:
The CPU enters power-down sleep mode and stays asleep until an interrupt wakes it -> Option A
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: