📖 Scenario: You are building a simple Arduino project that saves power by going to sleep and wakes up only when a button is pressed. This is useful for battery-powered devices that need to stay off most of the time and react only to user input.
🎯 Goal: Create an Arduino program that puts the board to sleep and wakes it up using an external interrupt triggered by a button press.
📋 What You'll Learn
Use the LowPower library to manage sleep mode
Set up a button connected to pin 2 to trigger an interrupt
Use an interrupt service routine (ISR) to wake the Arduino
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 often need to save power by sleeping and waking only when needed.
💼 Career
Understanding interrupts and power management is essential for embedded systems engineers and IoT developers to build efficient, low-power devices.
Progress0 / 4 steps
1
Setup button input and Serial communication
Write code to set up the button on pin 2 as an input with an internal pull-up resistor. Also, start the Serial communication at 9600 baud in the setup() function.
Arduino
Hint
Use pinMode to set the button pin as input with pull-up. Use Serial.begin(9600) to start serial communication.
2
Configure interrupt to wake from sleep
Add code to attach an interrupt on pin 2 that triggers on a falling edge. Create an empty interrupt service routine called wakeUp that will be called when the interrupt occurs.
Arduino
Hint
Use attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING) to set the interrupt. The ISR wakeUp should be empty or very short.
3
Put Arduino to sleep and wake on interrupt
Include the LowPower.h library at the top. In the loop() function, put the Arduino to sleep using LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF). The Arduino will wake up when the interrupt triggers.
Arduino
Hint
Include the LowPower library and call LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF) inside loop() to sleep indefinitely until interrupt.
4
Print message after waking up
Add a Serial.println("Woke up!") statement right after the sleep call in the loop() function to show when the Arduino wakes up.
Arduino
Hint
Use Serial.println("Woke up!") after the sleep call to show the wake-up message.
Practice
(1/5)
1. What is the main purpose of using an interrupt to wake an Arduino from sleep mode?
easy
A. To allow the Arduino to respond immediately to an external event while saving power
B. To make the Arduino run faster during normal operation
C. To reset the Arduino automatically after a delay
D. To increase the Arduino's memory capacity
Solution
Step 1: Understand sleep mode purpose
Arduino sleep mode reduces power consumption by stopping most activities.
Step 2: Role of interrupts in sleep mode
Interrupts allow the Arduino to wake up immediately when an external event occurs, saving power while staying responsive.
Final Answer:
To allow the Arduino to respond immediately to an external event while saving power -> Option A
Quick Check:
Interrupt wakes Arduino from sleep to save power [OK]
Hint: Interrupts wake Arduino instantly during sleep to save power [OK]
Common Mistakes:
Thinking interrupts speed up normal running
Confusing interrupts with reset functions
Believing interrupts increase memory
2. Which of the following is the correct way to attach an interrupt to pin 2 that triggers on a RISING signal?
easy
A. attachInterrupt(2, ISR, FALLING);
B. attachInterrupt(digitalPinToInterrupt(2), ISR, RISING);
C. attachInterrupt(digitalPinToInterrupt(3), ISR, CHANGE);
D. attachInterrupt(2, ISR, LOW);
Solution
Step 1: Use digitalPinToInterrupt() for pin mapping
Pin 2 must be converted using digitalPinToInterrupt(2) for portability.
Step 2: Set correct trigger mode
RISING triggers interrupt when signal goes from LOW to HIGH.
Final Answer:
attachInterrupt(digitalPinToInterrupt(2), ISR, RISING); -> Option B
Quick Check:
Correct pin and RISING trigger = attachInterrupt(digitalPinToInterrupt(2), ISR, RISING); [OK]
Hint: Always use digitalPinToInterrupt(pin) with correct trigger [OK]
Common Mistakes:
Using pin number directly without digitalPinToInterrupt()
Choosing wrong trigger mode like FALLING or LOW
Attaching interrupt to wrong pin number
3. What will be the output on the serial monitor after running this code if a button connected to pin 2 is pressed once?
A. attachInterrupt() uses pin number directly instead of digitalPinToInterrupt()
B. ISR function should not be empty
C. sleep_mode() should be called inside loop()
D. sleep_enable() must be called after sleep_mode()
Solution
Step 1: Check attachInterrupt() usage
Pin number must be converted with digitalPinToInterrupt(pin) for correct interrupt mapping.
Step 2: Validate other parts
Empty ISR is allowed; sleep_mode() can be in setup; sleep_enable() must be before sleep_mode().
Final Answer:
attachInterrupt() uses pin number directly instead of digitalPinToInterrupt() -> Option A
Quick Check:
Pin mapping requires digitalPinToInterrupt() [OK]
Hint: Always convert pin with digitalPinToInterrupt() in attachInterrupt() [OK]
Common Mistakes:
Not using digitalPinToInterrupt() for pin mapping
Thinking ISR must have code
Misplacing sleep_enable() call
5. You want to put your Arduino to sleep and wake it up only when a button connected to pin 4 is pressed. Which sequence correctly implements this behavior?
hard
A. attachInterrupt(digitalPinToInterrupt(4), wakeISR, LOW); sleep_enable(); sleep_mode();
B. attachInterrupt(4, wakeISR, RISING); sleep_disable(); sleep_mode();
C. attachInterrupt(digitalPinToInterrupt(4), wakeISR, FALLING); sleep_enable(); sleep_mode();
D. attachInterrupt(digitalPinToInterrupt(4), wakeISR, CHANGE); sleep_enable(); sleep_disable();
Solution
Step 1: Attach interrupt correctly
Use digitalPinToInterrupt(4) and FALLING trigger for button press (assuming active LOW button).
Step 2: Enable sleep and enter sleep mode
Call sleep_enable() before sleep_mode() to allow waking on interrupt.
Final Answer:
attachInterrupt(digitalPinToInterrupt(4), wakeISR, FALLING); sleep_enable(); sleep_mode(); -> Option C