Bird
Raised Fist0
Embedded Cprogramming~20 mins

Low-power design patterns in Embedded C - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Low-Power Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of low-power mode entry code
What is the output of this embedded C code snippet that simulates entering a low-power mode and waking up?
Embedded C
#include <stdio.h>
#include <stdbool.h>

volatile bool wakeup_flag = false;

void enter_low_power_mode() {
    printf("Entering low power mode...\n");
    // Simulate low power mode by waiting for wakeup_flag
    while (!wakeup_flag) {
        // CPU sleeps here
    }
    printf("Woke up from low power mode!\n");
}

int main() {
    enter_low_power_mode();
    wakeup_flag = true;
    return 0;
}
A
Woke up from low power mode!
Entering low power mode...
BNo output (infinite loop)
C
Entering low power mode...
Woke up from low power mode!
DEntering low power mode...
Attempts:
2 left
💡 Hint
Think about when wakeup_flag is set and how the while loop behaves.
🧠 Conceptual
intermediate
1:30remaining
Best low-power design pattern for periodic sensor reading
Which low-power design pattern is best suited for a device that needs to read a sensor every 10 seconds and sleep otherwise?
AUse a timer interrupt to wake device every 10 seconds and sleep in between
BBusy-wait loop checking sensor every millisecond
CKeep CPU running at full speed and poll sensor continuously
DDisable all interrupts and run sensor reading in main loop
Attempts:
2 left
💡 Hint
Think about how to save power by sleeping and waking only when needed.
🔧 Debug
advanced
2:30remaining
Identify the bug causing high power consumption
This embedded C code is intended to put the device into low-power mode but the device never wakes up. What is the bug?
Embedded C
void low_power_sleep() {
    // Disable interrupts
    __disable_irq();
    // Enter sleep mode
    __WFI();
    // Enable interrupts
    __enable_irq();
}

int main() {
    while (1) {
        low_power_sleep();
        // Do some work after waking
    }
    return 0;
}
AThe __WFI() instruction is missing a semicolon
BInterrupts are disabled before sleep and enabled after, so wakeup interrupts never happen
CThe while loop should be a for loop
DThe function low_power_sleep() is never called
Attempts:
2 left
💡 Hint
Think about how interrupts and sleep instructions interact.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in low-power mode code
Which option contains the correct syntax for entering low-power mode using a macro?
Embedded C
#define ENTER_LOW_POWER() __asm__("WFI")

void sleep_mode() {
    ENTER_LOW_POWER()
}
A
#define ENTER_LOW_POWER() __asm__("WFI")

void sleep_mode() {
    ENTER_LOW_POWER();
}
B
#define ENTER_LOW_POWER() __asm__("WFI")

void sleep_mode() {
    ENTER_LOW_POWER()
}
C
#define ENTER_LOW_POWER() __asm__("WFI")

void sleep_mode() {
    ENTER_LOW_POWER
}
D
#define ENTER_LOW_POWER() __asm__("WFI");

void sleep_mode() {
    ENTER_LOW_POWER();
}
Attempts:
2 left
💡 Hint
Check macro usage and semicolon placement.
🚀 Application
expert
2:00remaining
Calculate total sleep time in a low-power cycle
An embedded device sleeps for 5 seconds, wakes for 1 second to perform tasks, then repeats. After 10 cycles, what is the total time spent sleeping?
A10 seconds
B55 seconds
C50 seconds
D60 seconds
Attempts:
2 left
💡 Hint
Multiply sleep time per cycle by number of cycles.

Practice

(1/5)
1. Which of the following best describes the purpose of low-power design patterns in embedded C programming?
easy
A. To reduce battery consumption by putting the CPU and peripherals into sleep modes
B. To increase the clock speed of the CPU for faster processing
C. To add more features to the embedded device
D. To improve the graphical user interface responsiveness

Solution

  1. Step 1: Understand low-power design patterns

    These patterns focus on saving battery by reducing power use in the CPU and peripherals.
  2. Step 2: Compare options to the purpose

    Only To reduce battery consumption by putting the CPU and peripherals into sleep modes talks about reducing battery consumption by sleep modes, which matches the purpose.
  3. Final Answer:

    To reduce battery consumption by putting the CPU and peripherals into sleep modes -> Option A
  4. Quick Check:

    Low-power design = save battery by sleep modes [OK]
Hint: Low-power means saving battery by sleeping parts [OK]
Common Mistakes:
  • Confusing low-power with performance improvement
  • Thinking it adds features instead of saving power
  • Assuming it improves UI responsiveness
2. Which of the following is the correct way to put the CPU into sleep mode using a standard instruction in embedded C?
easy
A. enter_sleep_mode();
B. sleep();
C. power_down();
D. __WFI();

Solution

  1. Step 1: Identify the standard CPU sleep instruction

    The __WFI() instruction is a common CPU instruction to wait for interrupt and enter sleep mode safely.
  2. Step 2: Check other options

    Other options like sleep(), power_down(), enter_sleep_mode() are not standard embedded C instructions.
  3. Final Answer:

    __WFI(); -> Option D
  4. Quick Check:

    Use __WFI() to sleep CPU [OK]
Hint: Use __WFI() to enter CPU sleep mode [OK]
Common Mistakes:
  • Using non-standard function names
  • Forgetting the double underscore in __WFI()
  • Confusing sleep() with standard CPU instructions
3. Consider the following code snippet in embedded C:
void enter_low_power() {
    disable_peripherals();
    __WFI();
    enable_peripherals();
}
What is the expected behavior when this function runs?
medium
A. The CPU sleeps until an interrupt occurs, peripherals are disabled before sleep and enabled after wake-up
B. The CPU runs continuously without sleeping, peripherals remain disabled
C. The CPU sleeps but peripherals stay enabled, causing high power use
D. The function causes a syntax error and does not compile

Solution

  1. Step 1: Analyze the function steps

    First, peripherals are disabled to save power, then __WFI() puts CPU to sleep waiting for interrupt, finally peripherals are re-enabled after waking.
  2. Step 2: Understand expected behavior

    The CPU sleeps until an interrupt wakes it, peripherals are off during sleep to save power, then turned back on.
  3. Final Answer:

    The CPU sleeps until an interrupt occurs, peripherals are disabled before sleep and enabled after wake-up -> Option A
  4. Quick Check:

    Disable peripherals + __WFI() = sleep with low power [OK]
Hint: Disable peripherals before __WFI() to save power during sleep [OK]
Common Mistakes:
  • Assuming peripherals stay enabled during sleep
  • Thinking CPU does not sleep after __WFI()
  • Confusing function behavior with syntax errors
4. The following code is intended to put the CPU into low-power mode, but it does not work as expected:
void sleep_mode() {
    __WFI;
}
What is the error and how to fix it?
medium
A. The function should disable peripherals before __WFI; add disable_peripherals();
B. __WFI is not a valid instruction; replace with sleep();
C. __WFI is missing parentheses; fix by writing __WFI();
D. The function needs a return statement; add return 0;

Solution

  1. Step 1: Identify syntax error in __WFI usage

    The code uses __WFI without parentheses, which is incorrect syntax for a function-like instruction.
  2. Step 2: Correct the syntax

    Add parentheses to call __WFI as a function: __WFI(); fixes the error.
  3. Final Answer:

    __WFI is missing parentheses; fix by writing __WFI(); -> Option C
  4. Quick Check:

    __WFI() needs parentheses to call [OK]
Hint: Always use parentheses with __WFI() instruction [OK]
Common Mistakes:
  • Omitting parentheses on __WFI
  • Replacing __WFI with non-standard functions
  • Ignoring peripheral disable step (not syntax error here)
5. You want to design a low-power routine that puts the CPU to sleep only if no peripherals are active. Given the functions are_peripherals_active() returning 1 if active, 0 if not, and __WFI() to sleep, which code snippet correctly implements this logic?
hard
A. if (are_peripherals_active()) { __WFI(); }
B. if (!are_peripherals_active()) { __WFI(); }
C. while (are_peripherals_active()) { __WFI(); }
D. do { __WFI(); } while (are_peripherals_active());

Solution

  1. Step 1: Understand the condition to sleep

    The CPU should sleep only if no peripherals are active, so condition is peripherals inactive (0).
  2. Step 2: Match condition with code

    Using if (!are_peripherals_active()) means if peripherals are not active, then call __WFI() to sleep.
  3. Final Answer:

    if (!are_peripherals_active()) { __WFI(); } -> Option B
  4. Quick Check:

    Sleep only if peripherals inactive = if (!active) [OK]
Hint: Sleep only when peripherals inactive: use if (!are_peripherals_active()) [OK]
Common Mistakes:
  • Sleeping when peripherals are active
  • Using loops causing repeated sleep calls
  • Confusing condition negation
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