0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program to Blink LED - Simple Example

An Embedded C program to blink an LED toggles a GPIO pin connected to the LED on and off with a delay using code like while(1) { PORTB ^= (1 << PB0); _delay_ms(500); } to create the blinking effect.
📋

Examples

InputLED connected to PORTB pin 0
OutputLED turns on and off every 500 milliseconds repeatedly
InputChange delay to 1000 ms
OutputLED blinks slower, turning on and off every 1 second
InputLED connected to PORTD pin 2
OutputModify code to toggle PORTD pin 2, LED blinks on that pin
🧠

How to Think About It

To blink an LED, you set the microcontroller pin connected to the LED as output. Then, you turn the pin on (LED on), wait some time, turn it off (LED off), and wait again. This cycle repeats forever to create the blinking effect.
📐

Algorithm

1
Set the LED pin as output
2
Start an infinite loop
3
Turn the LED pin ON
4
Wait for a fixed delay
5
Turn the LED pin OFF
6
Wait for the same delay
7
Repeat the loop
💻

Code

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

int main(void) {
    DDRB |= (1 << PB0); // Set PORTB0 as output
    while(1) {
        PORTB ^= (1 << PB0); // Toggle PORTB0
        _delay_ms(500);     // Wait 500 ms
    }
    return 0;
}
Output
LED connected to PORTB0 blinks on and off every 500 milliseconds
🔍

Dry Run

Let's trace blinking LED connected to PORTB0 through the code

1

Set PORTB0 as output

DDRB = 0b00000001 (PORTB0 pin set to output)

2

Toggle PORTB0 first time

PORTB = 0b00000001 (LED ON)

3

Wait 500 ms

Delay function pauses execution

4

Toggle PORTB0 second time

PORTB = 0b00000000 (LED OFF)

5

Wait 500 ms

Delay function pauses execution

6

Repeat toggling

PORTB toggles between 0b00000001 and 0b00000000 repeatedly

IterationPORTB ValueLED State
10b00000001ON
20b00000000OFF
30b00000001ON
40b00000000OFF
💡

Why This Works

Step 1: Configure pin as output

Using DDRB |= (1 << PB0); sets the pin connected to the LED as output so it can drive the LED.

Step 2: Toggle LED pin

The PORTB ^= (1 << PB0); flips the pin state from HIGH to LOW or LOW to HIGH, turning the LED on or off.

Step 3: Delay creates visible blink

The _delay_ms(500); pauses the program so the LED stays on or off long enough for human eyes to see the blink.

🔄

Alternative Approaches

Using separate ON and OFF commands
embedded_c
#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    DDRB |= (1 << PB0);
    while(1) {
        PORTB |= (1 << PB0);  // LED ON
        _delay_ms(500);
        PORTB &= ~(1 << PB0); // LED OFF
        _delay_ms(500);
    }
    return 0;
}
This method explicitly sets and clears the pin instead of toggling, which can be clearer but uses more instructions.
Using timer interrupt for blinking
embedded_c
// Timer interrupt code is more complex and hardware-specific
// It allows blinking without blocking delay, better for multitasking
Using timer interrupts avoids blocking delays but requires more setup and understanding of microcontroller timers.

Complexity: O(1) time, O(1) space

Time Complexity

The blinking loop runs infinitely but each iteration takes constant time due to fixed delay; no loops depend on input size.

Space Complexity

Uses only a few registers and no extra memory; constant space usage.

Which Approach is Fastest?

Toggling the pin with XOR is slightly faster than separate ON/OFF commands, but difference is minimal for blinking.

ApproachTimeSpaceBest For
Toggle with XORO(1)O(1)Simple and fast blinking
Separate ON/OFF commandsO(1)O(1)Clearer code, easy to understand
Timer interruptO(1)O(1)Non-blocking blinking in complex apps
💡
Use DDRx to set pin direction and PORTx to control pin output for blinking LEDs.
⚠️
Forgetting to set the pin as output before toggling causes the LED not to blink.