📖 Scenario: You have a small board with a single LED light. You want to make the LED blink on and off repeatedly. This is the first step to learning how to control hardware with code.
🎯 Goal: Write a simple embedded C program that turns an LED on and off with a delay, making it blink continuously.
📋 What You'll Learn
Create a variable to represent the LED pin number
Create a delay function to pause the program
Write code to turn the LED on and off in a loop
Print or indicate the LED state changes (for simulation or debugging)
💡 Why This Matters
🌍 Real World
Blinking an LED is the first step in learning how to control hardware devices like lights, motors, or sensors using code.
💼 Career
Embedded programmers often start by controlling simple hardware outputs like LEDs before moving on to complex device control.
Progress0 / 4 steps
1
Set up the LED pin variable
Create an integer variable called led_pin and set it to 13, which represents the LED pin number.
Embedded C
Hint
Think of the LED pin as the address where the LED is connected. Pin 13 is common for built-in LEDs.
2
Create a delay function
Write a function called delay that takes an integer ms and uses a simple loop to create a delay approximately equal to ms milliseconds.
Embedded C
Hint
The delay function uses a loop to waste time. The volatile keyword prevents the compiler from optimizing the loop away.
3
Write the main loop to blink the LED
Write the main function that uses an infinite while loop. Inside the loop, turn the LED on by setting led_state to 1, call delay(500), then turn the LED off by setting led_state to 0, and call delay(500) again.
Embedded C
Hint
The infinite loop keeps the LED blinking forever. Changing led_state simulates turning the LED on and off.
4
Print the LED state changes
Add printf statements inside the loop in main to print "LED ON" when the LED turns on and "LED OFF" when it turns off.
Embedded C
Hint
Use printf("LED ON\n") and printf("LED OFF\n") to show the LED state changes.
Practice
(1/5)
1. What is the main purpose of the first embedded program that blinks an LED?
easy
A. To read data from a sensor
B. To turn the LED on and off repeatedly
C. To send data over a network
D. To display text on a screen
Solution
Step 1: Understand the program's goal
The program is designed to control an LED by switching it on and off.
Step 2: Identify the blinking action
Blinking means turning the LED on, then off repeatedly to create a visible effect.
Final Answer:
To turn the LED on and off repeatedly -> Option B
Quick Check:
LED blinking = on/off repeatedly [OK]
Hint: Blinking means LED toggles on and off repeatedly [OK]
Common Mistakes:
Confusing blinking with sensor reading
Thinking it sends data over network
Assuming it displays text
2. Which line correctly sets the pin connected to the LED as an output in embedded C?
easy
A. DDRB |= (1 << 5);
B. PORTB |= (1 << 5);
C. PINB |= (1 << 5);
D. DDRB &= ~(1 << 5);
Solution
Step 1: Understand DDR register usage
DDRB controls the data direction of port B pins; setting a bit to 1 makes it output.
Step 2: Analyze each option
DDRB |= (1 << 5); sets bit 5 of DDRB to 1, configuring pin 5 as output. PORTB |= (1 << 5); sets PORTB bit, which controls output level, not direction. PINB |= (1 << 5); sets PINB, which reads input pins. DDRB &= ~(1 << 5); clears bit 5 in DDRB, making it input.
Final Answer:
DDRB |= (1 << 5); -> Option A
Quick Check:
Set DDRB bit = output pin [OK]
Hint: Use DDRx |= (1 << pin) to set output pin [OK]
Common Mistakes:
Using PORT register to set direction
Using PIN register instead of DDR
Clearing DDR bit instead of setting
3. Given the code snippet below, what will be the output behavior of the LED?
A. DDRB should use |= instead of = to avoid clearing other pins
B. PORTB should use &= ~ instead of = to turn off LED
C. _delay_ms function is missing a header file
D. The code has no errors and will blink LED correctly
Solution
Step 1: Check DDRB assignment
Using DDRB = (1 << 5) overwrites all other bits, possibly affecting other pins. Using DDRB |= (1 << 5) sets only bit 5 without changing others.
Step 2: Verify PORTB usage
PORTB = (1 << 5) and PORTB = 0 are valid to turn LED ON and OFF by setting and clearing all bits respectively.
Final Answer:
DDRB should use |= instead of = to avoid clearing other pins -> Option A
Quick Check:
Use |= to set bits without clearing others [OK]
Hint: Use |= to set bits, not = which clears others [OK]
Common Mistakes:
Overwriting DDRB clears other pin directions
Confusing PORTB clearing methods
Assuming no header needed for delay
5. You want to modify the LED blink program to blink twice as fast without changing the delay function. Which change should you make?
hard
A. Change the LED pin to a different port
B. Reduce both delay calls to 250ms
C. Toggle the LED pin twice inside the same delay period
D. Increase the delay calls to 1000ms
Solution
Step 1: Understand blinking speed relation
Blink speed depends on how often the LED state changes. Delays control timing between toggles.
Step 2: Analyze options
Reduce both delay calls to 250ms changes delay times, but question says without changing delay function. Toggle the LED pin twice inside the same delay period toggles LED pin twice in same delay period, effectively doubling blink frequency. Change the LED pin to a different port changes port, unrelated to speed. Increase the delay calls to 1000ms increases delay, slowing blink.
Final Answer:
Toggle the LED pin twice inside the same delay period -> Option C
Quick Check:
More toggles per delay = faster blink [OK]
Hint: Toggle LED more often per delay to speed up blink [OK]
Common Mistakes:
Changing delay values despite instructions
Changing port does not affect blink speed
Increasing delay slows blinking
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.