0
0
Embedded Cprogramming~20 mins

First embedded program (LED blink) in Embedded C - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Embedded LED Blink Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output behavior of this LED blink code?

Consider this embedded C code snippet for blinking an LED connected to PORTB pin 0. What will the LED do when this code runs?

Embedded C
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= (1 << 0); // Set PORTB0 as output
    while(1) {
        PORTB ^= (1 << 0); // Toggle PORTB0
        _delay_ms(500);
    }
    return 0;
}
AThe LED will stay constantly on without blinking.
BThe LED will stay constantly off without blinking.
CThe LED connected to PORTB0 will blink on and off every 500 milliseconds.
DThe code will cause a compile error due to missing header files.
Attempts:
2 left
💡 Hint

Look at how PORTB0 is toggled inside the infinite loop with a delay.

Predict Output
intermediate
2:00remaining
What error does this LED blink code produce?

Examine this code snippet intended to blink an LED on PORTD pin 2. What error will occur when compiling?

Embedded C
#include <avr/io.h>

int main(void) {
    DDRD = 1 << 2; // Set PORTD2 as output
    while(1) {
        PORTD = 1 << 2; // Turn LED on
        _delay_ms(1000);
        PORTD = 0 << 2; // Turn LED off
        _delay_ms(1000);
    }
    return 0;
}
ANo error; the LED will blink every 1 second.
BSyntaxError: _delay_ms is not declared or included.
CRuntime error: PORTD assignment causes undefined behavior.
DCompilation error: DDRD cannot be assigned with bit shift.
Attempts:
2 left
💡 Hint

Check if all required headers are included for delay functions.

🔧 Debug
advanced
2:30remaining
Why does this LED blink code fail to toggle the LED?

This code is supposed to blink an LED on PORTC pin 5, but the LED stays off. What is the bug?

Embedded C
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRC |= (1 << 5); // Set PORTC5 as output
    while(1) {
        PORTC = (1 << 5); // Turn LED on
        _delay_ms(300);
        PORTC = 0; // Turn LED off
        _delay_ms(300);
    }
    return 0;
}
APORTC assignment overwrites other pins; should use PORTC ^= (1 << 5) to toggle.
BDDRC is not set correctly; should use DDRC = (1 << 5) instead of |=.
CDelay time is too short to see blinking.
DMissing return statement in main function.
Attempts:
2 left
💡 Hint

Think about how PORTC is assigned inside the loop and what happens to other pins.

📝 Syntax
advanced
2:00remaining
Which option fixes the syntax error in this LED blink code?

Identify the option that corrects the syntax error in this code snippet:

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= (1 << 3) // Set PORTB3 as output
    while(1) {
        PORTB ^= (1 << 3);
        _delay_ms(250);
    }
    return 0;
}
ARemove the while loop braces {}
BReplace _delay_ms(250) with delay(250)
CChange DDRB |= (1 << 3) to DDRB = (1 << 3)
DAdd a semicolon after DDRB |= (1 << 3);
Attempts:
2 left
💡 Hint

Look carefully at the line before the while loop.

🚀 Application
expert
3:00remaining
How many times will the LED blink in 10 seconds?

This code blinks an LED connected to PORTD pin 1. How many times will the LED turn on and off in 10 seconds?

Embedded C
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRD |= (1 << 1); // Set PORTD1 as output
    for (int i = 0; i < 20; i++) {
        PORTD ^= (1 << 1); // Toggle LED
        _delay_ms(250);
    }
    return 0;
}
A10 times
B20 times
C40 times
D5 times
Attempts:
2 left
💡 Hint

Each toggle changes LED state. Count full on-off cycles in 10 seconds.