Bird
Raised Fist0
Arduinoprogramming~20 mins

Arduino sleep modes - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Arduino Sleep Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output when using sleep_mode() with SLEEP_MODE_IDLE?

Consider the following Arduino sketch that uses SLEEP_MODE_IDLE. What will be printed on the serial monitor?

Arduino
#include <avr/sleep.h>

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("Before sleep");
  set_sleep_mode(SLEEP_MODE_IDLE);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  Serial.println("After sleep");
}

void loop() {
  // Empty loop
}
A
Before sleep
After sleep
B
Before sleep
C
After sleep
DNo output (program hangs)
Attempts:
2 left
💡 Hint

Idle mode stops the CPU but keeps peripherals running, so the program continues after sleep.

❓ Predict Output
intermediate
2:00remaining
What happens after sleep_mode() with SLEEP_MODE_PWR_DOWN?

Given this Arduino code using SLEEP_MODE_PWR_DOWN, what will be the output on the serial monitor?

Arduino
#include <avr/sleep.h>

void setup() {
  Serial.begin(9600);
  delay(1000);
  Serial.println("Start");
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  Serial.println("Wake up");
}

void loop() {
  // Empty
}
A
Start
B
Start
Wake up
C
Wake up
DNo output (device stays asleep)
Attempts:
2 left
💡 Hint

Power-down mode stops almost everything; without an interrupt, the device never wakes.

🔧 Debug
advanced
2:00remaining
Why does this code not wake up from sleep?

This Arduino code uses SLEEP_MODE_PWR_DOWN but never wakes up. What is the main reason?

Arduino
#include <avr/sleep.h>

void setup() {
  Serial.begin(9600);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  Serial.println("Awake");
}

void loop() {}
ASerial.begin() must be called after sleep_mode()
BNo interrupt is attached to wake the MCU from power-down mode
Csleep_disable() is called too early
DSLEEP_MODE_PWR_DOWN is not supported on Arduino boards
Attempts:
2 left
💡 Hint

Power-down mode requires an external or pin change interrupt to wake up.

🧠 Conceptual
advanced
2:00remaining
Which sleep mode uses the least power but requires an external interrupt to wake?

Choose the correct Arduino sleep mode that uses the least power and can only be exited by an external interrupt or reset.

ASLEEP_MODE_IDLE
BSLEEP_MODE_ADC
CSLEEP_MODE_STANDBY
DSLEEP_MODE_PWR_DOWN
Attempts:
2 left
💡 Hint

Think about which mode stops almost all clocks and peripherals.

❓ Predict Output
expert
3:00remaining
How many times does the LED blink in this sleep-wake cycle?

Analyze this Arduino sketch that uses sleep and an interrupt to wake. How many times will the LED blink after pressing the button once?

Arduino
#include <avr/sleep.h>

volatile bool woke = false;

void wakeUp() {
  woke = true;
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING);
  Serial.begin(9600);
  delay(1000);
  Serial.println("Going to sleep");
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_mode();
  sleep_disable();
  if (woke) {
    for (int i = 0; i < 3; i++) {
      digitalWrite(LED_BUILTIN, HIGH);
      delay(200);
      digitalWrite(LED_BUILTIN, LOW);
      delay(200);
    }
    Serial.println("Woke up and blinked LED");
  }
}

void loop() {}
A0 times
B1 time
C3 times
DInfinite times
Attempts:
2 left
💡 Hint

The interrupt sets a flag that triggers the blinking loop once after waking.

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