Bird
Raised Fist0
Arduinoprogramming~20 mins

Wake-up from sleep with interrupt in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Sleep Interrupt Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output after waking up from sleep?

Consider this Arduino code that puts the microcontroller to sleep and wakes it up using an external interrupt. What will be printed to the serial monitor after the interrupt triggers?

Arduino
volatile bool wokeUp = false;

void wakeUp() {
  wokeUp = true;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING);
  Serial.println("Going to sleep...");
  delay(1000);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  if (wokeUp) {
    Serial.println("Woke up from interrupt!");
  } else {
    Serial.println("Did not wake up.");
  }
}

void loop() {
  // Empty loop
}
AGoing to sleep...\nDid not wake up.
BWoke up from interrupt!\nGoing to sleep...
CGoing to sleep...\nWoke up from interrupt!
DDid not wake up.\nGoing to sleep...
Attempts:
2 left
💡 Hint

Think about whether FALLING mode wakes from SLEEP_MODE_PWR_DOWN.

🧠 Conceptual
intermediate
1:30remaining
Which interrupt mode wakes the Arduino from sleep?

When using attachInterrupt() to wake an Arduino from sleep, which interrupt mode will successfully wake the device from SLEEP_MODE_PWR_DOWN?

AFALLING
BCHANGE
CLOW
DRISING
Attempts:
2 left
💡 Hint

Only certain interrupt modes can wake the microcontroller from the deepest sleep mode.

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

Examine the code below. The Arduino never wakes up after going to sleep. What is the cause?

Arduino
volatile bool wokeUp = false;

void wakeUp() {
  wokeUp = true;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, FALLING);
  Serial.println("Going to sleep...");
  delay(1000);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  if (wokeUp) {
    Serial.println("Woke up from interrupt!");
  } else {
    Serial.println("Did not wake up.");
  }
}

void loop() {}
AThe interrupt number 2 is incorrect; should use digitalPinToInterrupt(2).
BThe sleep_disable() is called before sleep_cpu().
CThe variable wokeUp is not declared volatile.
DThe interrupt mode FALLING does not wake from SLEEP_MODE_PWR_DOWN.
Attempts:
2 left
💡 Hint

Check the interrupt mode used with SLEEP_MODE_PWR_DOWN.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in this sleep code

Which option contains the correct syntax to put the Arduino into power-down sleep mode and wake on an interrupt?

A
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
sleep_disable
B
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu();
sleep_disable();
C
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
sleep_enable();
sleep_cpu;
sleep_disable();
D
set_sleep_mode(SLEEP_MODE_PWR_DOWN)
sleep_enable()
sleep_cpu()
sleep_disable()
Attempts:
2 left
💡 Hint

Look for missing semicolons or missing parentheses in function calls.

🚀 Application
expert
3:00remaining
How many times does the Arduino wake up in this loop?

Given the code below, how many times will the Arduino print "Woke up!" if the button connected to pin 2 is pressed 3 times?

Arduino
volatile int wakeCount = 0;

void wakeUp() {
  wakeCount++;
}

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  attachInterrupt(digitalPinToInterrupt(2), wakeUp, LOW);
}

void loop() {
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sleep_cpu();
  sleep_disable();
  if (wakeCount > 0) {
    Serial.println("Woke up!");
    wakeCount = 0;
  }
  delay(500);
}
A1
B0
C3
DDepends on how long the button is held
Attempts:
2 left
💡 Hint

Consider how the LOW interrupt mode behaves and how the wakeCount variable is reset.

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

  1. Step 1: Understand sleep mode purpose

    Arduino sleep mode reduces power consumption by stopping most activities.
  2. 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.
  3. Final Answer:

    To allow the Arduino to respond immediately to an external event while saving power -> Option A
  4. 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

  1. Step 1: Use digitalPinToInterrupt() for pin mapping

    Pin 2 must be converted using digitalPinToInterrupt(2) for portability.
  2. Step 2: Set correct trigger mode

    RISING triggers interrupt when signal goes from LOW to HIGH.
  3. Final Answer:

    attachInterrupt(digitalPinToInterrupt(2), ISR, RISING); -> Option B
  4. 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?
void ISR() {
  Serial.println("Woke up!");
}

void setup() {
  Serial.begin(9600);
  attachInterrupt(digitalPinToInterrupt(2), ISR, RISING);
  sleep_enable();
  sleep_mode();
  Serial.println("Sleeping...");
}

void loop() {
  // Empty loop
}
medium
A. No output at all
B. Woke up!\nSleeping...
C. Sleeping... (no further output)
D. Sleeping...\nWoke up!

Solution

  1. Step 1: Analyze print locations

    "Sleeping..." prints after sleep_mode(), so it executes after waking up.
  2. Step 2: ISR execution order

    ISR prints "Woke up!" immediately upon interrupt, before resuming code after sleep_mode().
  3. Final Answer:

    Sleeping...\nWoke up! -> Option D
  4. Quick Check:

    Code after sleep_mode() runs after ISR, so output order is Sleeping... then Woke up! [OK]
Hint: ISR runs immediately on interrupt, but code after sleep_mode() runs after ISR [OK]
Common Mistakes:
  • Thinking "Sleeping..." prints before sleep
  • Believing no output after wake
  • Expecting Sleeping... before ISR
4. Identify the error in this code snippet that tries to wake Arduino from sleep on pin 3 interrupt:
void ISR() {
  // Empty ISR
}

void setup() {
  attachInterrupt(3, ISR, FALLING);
  sleep_enable();
  sleep_mode();
}

void loop() {
  // Empty
}
medium
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

  1. Step 1: Check attachInterrupt() usage

    Pin number must be converted with digitalPinToInterrupt(pin) for correct interrupt mapping.
  2. Step 2: Validate other parts

    Empty ISR is allowed; sleep_mode() can be in setup; sleep_enable() must be before sleep_mode().
  3. Final Answer:

    attachInterrupt() uses pin number directly instead of digitalPinToInterrupt() -> Option A
  4. 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

  1. Step 1: Attach interrupt correctly

    Use digitalPinToInterrupt(4) and FALLING trigger for button press (assuming active LOW button).
  2. Step 2: Enable sleep and enter sleep mode

    Call sleep_enable() before sleep_mode() to allow waking on interrupt.
  3. Final Answer:

    attachInterrupt(digitalPinToInterrupt(4), wakeISR, FALLING); sleep_enable(); sleep_mode(); -> Option C
  4. Quick Check:

    Correct pin, trigger, and sleep calls = attachInterrupt(digitalPinToInterrupt(4), wakeISR, FALLING); sleep_enable(); sleep_mode(); [OK]
Hint: Use digitalPinToInterrupt() with sleep_enable() before sleep_mode() [OK]
Common Mistakes:
  • Using sleep_disable() before sleep_mode()
  • Wrong trigger mode like LOW or CHANGE for button press
  • Not enabling sleep before entering sleep_mode()
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