0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program to Blink LED with Delay

Use a GPIO pin set as output and toggle it ON and OFF with a delay loop in between, like PORTB |= (1 << PB0); delay(); PORTB &= ~(1 << PB0); delay(); to blink an LED.
📋

Examples

InputLED connected to PORTB pin 0, delay 500ms
OutputLED turns ON for 500ms, then OFF for 500ms repeatedly
InputLED connected to PORTD pin 2, delay 1000ms
OutputLED blinks ON and OFF every 1 second
InputLED connected to PORTC pin 5, delay 200ms
OutputLED blinks ON and OFF every 200 milliseconds
🧠

How to Think About It

To blink an LED, first set the microcontroller pin connected to the LED as an output. Then, turn the LED ON by setting the pin high, wait for some time using a delay, turn the LED OFF by setting the pin low, and wait again. Repeat this cycle forever to create a blinking effect.
📐

Algorithm

1
Configure the LED pin as output.
2
Set the LED pin HIGH to turn ON the LED.
3
Wait for a delay period.
4
Set the LED pin LOW to turn OFF the LED.
5
Wait for the same delay period.
6
Repeat steps 2 to 5 indefinitely.
💻

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);  // LED ON
        _delay_ms(500);       // Delay 500 ms
        PORTB &= ~(1 << PB0); // LED OFF
        _delay_ms(500);       // Delay 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 with 500ms delay through the code

1

Set PORTB0 as output

DDRB = 0x01 (binary 00000001), so pin PB0 is output

2

Turn LED ON

PORTB |= 0x01 sets PB0 high, LED lights up

3

Delay 500ms

Program waits for 500 milliseconds

4

Turn LED OFF

PORTB &= ~0x01 clears PB0, LED turns off

5

Delay 500ms

Program waits for 500 milliseconds

6

Repeat

Loop back to step 2 to continue blinking

StepPORTBLED State
100000001Configured as output
200000001ON
300000001ON (delay)
400000000OFF
500000000OFF (delay)
💡

Why This Works

Step 1: Configure pin as output

Setting the data direction register bit for the LED pin to 1 makes it an output so we can control voltage.

Step 2: Turn LED ON and OFF

Setting the pin high with PORTB |= (1 << PB0) supplies voltage to LED, turning it ON; clearing it turns LED OFF.

Step 3: Use delay for visible blinking

The _delay_ms() function pauses the program so the LED stays ON or OFF long enough to see the blink.

🔄

Alternative Approaches

Using timer interrupts
embedded_c
#include <avr/io.h>
#include <avr/interrupt.h>

volatile int led_state = 0;

ISR(TIMER0_COMPA_vect) {
    if (led_state) {
        PORTB &= ~(1 << PB0);
        led_state = 0;
    } else {
        PORTB |= (1 << PB0);
        led_state = 1;
    }
}

int main(void) {
    DDRB |= (1 << PB0);
    TCCR0A = (1 << WGM01);
    OCR0A = 195;
    TIMSK0 = (1 << OCIE0A);
    TCCR0B = (1 << CS02) | (1 << CS00);
    sei();
    while(1) {}
}
Uses hardware timer interrupt for precise blinking without blocking CPU, but is more complex.
Using a software loop delay
embedded_c
#include <avr/io.h>

void delay(void) {
    volatile unsigned int i;
    for(i=0; i<30000; i++) {}
}

int main(void) {
    DDRB |= (1 << PB0);
    while(1) {
        PORTB |= (1 << PB0);
        delay();
        PORTB &= ~(1 << PB0);
        delay();
    }
}
Simple delay loop without libraries but less accurate and CPU is busy during delay.

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

Time Complexity

The blinking loop runs indefinitely with fixed delay; no loops depend on input size, so time complexity is constant O(1).

Space Complexity

Uses fixed registers and variables; no extra memory allocation, so space complexity is O(1).

Which Approach is Fastest?

Using hardware timer interrupts is more efficient as it frees CPU during delay, while software loops block CPU.

ApproachTimeSpaceBest For
Simple delay loopO(1)O(1)Easy to implement, less accurate
_delay_ms() functionO(1)O(1)Accurate delay, simple code
Timer interruptsO(1)O(1)Efficient multitasking, precise timing
💡
Use hardware timers for accurate LED blinking without blocking your program.
⚠️
Forgetting to set the LED pin as output causes the LED not to blink.