0
0
Arduinoprogramming~20 mins

Arduino sleep modes - Practice Problems & Coding Challenges

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