Bird
Raised Fist0
Embedded Cprogramming~10 mins

First embedded program (LED blink) in Embedded C - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - First embedded program (LED blink)
Start
↓
Setup LED pin as output
↓
Turn LED ON
↓
Wait some time
↓
Turn LED OFF
↓
Wait some time
↓
Repeat loop
The program sets up the LED pin, then repeatedly turns the LED on and off with delays in between.
Execution Sample
Embedded C
int main() {
  setup_pin_output();
  while(1) {
    led_on();
    delay();
    led_off();
    delay();
  }
  return 0;
}
This code turns an LED on and off repeatedly with delays, making it blink.
Execution Table
StepActionPin StateDelayLoop Iteration
1Setup LED pin as outputOFF00
2Turn LED ONON01
3Wait delayONdelay time1
4Turn LED OFFOFFdelay time1
5Wait delayOFFdelay time1
6Turn LED ONONdelay time2
7Wait delayONdelay time2
8Turn LED OFFOFFdelay time2
9Wait delayOFFdelay time2
...Repeat steps 6-9Alternatingdelay time3 and beyond
💡 The loop runs forever, blinking the LED on and off repeatedly.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
Pin StateOFFONOFFONOFFAlternates ON/OFF
Delay00delay timedelay timedelay timedelay time
Loop Iteration01122Increases indefinitely
Key Moments - 3 Insights
Why does the LED keep blinking and never stop?
Because the program uses an infinite loop (while(1)) as shown in execution_table rows 2-9, it repeats turning the LED on and off forever.
What does the delay do between turning the LED on and off?
The delay pauses the program so the LED stays on or off long enough to see the blink, as shown in execution_table steps 3 and 5.
Why do we set the pin as output before blinking the LED?
Setting the pin as output (step 1) tells the microcontroller to send signals to the LED; without this, the LED won't respond.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the Pin State at Step 4?
AUndefined
BON
COFF
DBlinking
💡 Hint
Check the 'Pin State' column at Step 4 in the execution_table.
At which step does the program first wait for a delay?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look for the first 'Wait delay' action in the execution_table.
If the delay time is increased, how does it affect the blinking?
ALED blinks faster
BLED blinks slower
CLED stays always ON
DLED stays always OFF
💡 Hint
Refer to the 'Delay' column in variable_tracker and think about how longer delay affects blink speed.
Concept Snapshot
First embedded program (LED blink):
- Setup LED pin as output
- Loop forever:
  - Turn LED ON
  - Wait delay
  - Turn LED OFF
  - Wait delay
- Result: LED blinks repeatedly with visible on/off times
Full Transcript
This program is the first embedded example to blink an LED. It starts by setting the LED pin as output so the microcontroller can control it. Then it enters an infinite loop where it turns the LED on, waits some time, turns it off, and waits again. This cycle repeats forever, making the LED blink. The delay is important so the blinking is visible to our eyes. The execution table shows each step and how the pin state changes. The variable tracker follows the pin state, delay, and loop count. Key moments explain why the loop never ends, why delay is needed, and why setting the pin as output is necessary. The quiz tests understanding of pin state changes, delay timing, and blink speed effects.

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

  1. Step 1: Understand the program's goal

    The program is designed to control an LED by switching it on and off.
  2. Step 2: Identify the blinking action

    Blinking means turning the LED on, then off repeatedly to create a visible effect.
  3. Final Answer:

    To turn the LED on and off repeatedly -> Option B
  4. 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

  1. Step 1: Understand DDR register usage

    DDRB controls the data direction of port B pins; setting a bit to 1 makes it output.
  2. 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.
  3. Final Answer:

    DDRB |= (1 << 5); -> Option A
  4. 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?
DDRB |= (1 << 5);
while(1) {
  PORTB |= (1 << 5);
  _delay_ms(500);
  PORTB &= ~(1 << 5);
  _delay_ms(500);
}
medium
A. LED stays ON permanently
B. LED blinks with 1 second ON and 1 second OFF
C. LED stays OFF permanently
D. LED blinks with 500ms ON and 500ms OFF

Solution

  1. Step 1: Analyze pin direction and control

    DDRB |= (1 << 5) sets pin 5 as output. PORTB |= (1 << 5) turns LED ON. PORTB &= ~(1 << 5) turns LED OFF.
  2. Step 2: Understand delay timing

    Each _delay_ms(500) pauses for 500 milliseconds, so LED is ON for 500ms, then OFF for 500ms, repeating.
  3. Final Answer:

    LED blinks with 500ms ON and 500ms OFF -> Option D
  4. Quick Check:

    Delay 500ms ON/OFF = blinking every 1 second [OK]
Hint: Delay 500ms twice means 1 second blink cycle [OK]
Common Mistakes:
  • Confusing delay time units
  • Assuming LED stays always ON or OFF
  • Ignoring bitwise operations on PORTB
4. Identify the error in the following embedded C code for blinking an LED:
DDRB = (1 << 5);
while(1) {
  PORTB = (1 << 5);
  _delay_ms(500);
  PORTB = 0;
  _delay_ms(500);
}
medium
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

  1. 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.
  2. 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.
  3. Final Answer:

    DDRB should use |= instead of = to avoid clearing other pins -> Option A
  4. 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

  1. Step 1: Understand blinking speed relation

    Blink speed depends on how often the LED state changes. Delays control timing between toggles.
  2. 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.
  3. Final Answer:

    Toggle the LED pin twice inside the same delay period -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard