0
0
Arduinoprogramming~20 mins

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

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