0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Blink LED Using Embedded C: Simple Guide

To blink an LED using Embedded C, configure the microcontroller's pin connected to the LED as output, then toggle its state between HIGH and LOW with delays in between. Use GPIO registers to control the pin and a delay loop or timer to create the blinking effect.
📐

Syntax

To blink an LED, you need to:

  • Set the pin as output: Configure the microcontroller pin connected to the LED as an output pin.
  • Turn the LED ON: Set the pin HIGH to power the LED.
  • Delay: Wait for some time to keep the LED ON.
  • Turn the LED OFF: Set the pin LOW to turn off the LED.
  • Delay again: Wait before turning it ON again to create a blink effect.
c
void setup() {
    // Set LED pin as output
    DDRB |= (1 << PB0);  // Example for AVR microcontroller, pin PB0
}

void loop() {
    PORTB |= (1 << PB0);  // Turn LED ON
    _delay_ms(500);       // Wait 500 milliseconds
    PORTB &= ~(1 << PB0); // Turn LED OFF
    _delay_ms(500);       // Wait 500 milliseconds
}
💻

Example

This example blinks an LED connected to pin PB0 of an AVR microcontroller. It turns the LED ON for 500 milliseconds, then OFF for 500 milliseconds repeatedly.

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

int main(void) {
    DDRB |= (1 << PB0);  // Set PB0 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 PB0 blinks ON and OFF every 500 milliseconds
⚠️

Common Pitfalls

Common mistakes when blinking an LED include:

  • Not setting the pin as output, so the LED won't turn on.
  • Forgetting to include delay, causing the LED to appear always ON or OFF.
  • Using wrong pin numbers or registers for your microcontroller.
  • Not enabling the microcontroller clock or peripherals if required.
c
/* Wrong: Pin not set as output */
int main(void) {
    while (1) {
        PORTB |= (1 << PB0); // Trying to turn LED ON
        _delay_ms(500);
        PORTB &= ~(1 << PB0); // Trying to turn LED OFF
        _delay_ms(500);
    }
    return 0;
}

/* Right: Pin set as output */
int main(void) {
    DDRB |= (1 << PB0); // Set PB0 as output
    while (1) {
        PORTB |= (1 << PB0);
        _delay_ms(500);
        PORTB &= ~(1 << PB0);
        _delay_ms(500);
    }
    return 0;
}
📊

Quick Reference

LED Blinking Steps Cheat Sheet:

  • DDRx |= (1 << PINx); - Set pin as output.
  • PORTx |= (1 << PINx); - Turn LED ON.
  • PORTx &= ~(1 << PINx); - Turn LED OFF.
  • _delay_ms(time); - Wait for specified milliseconds.

Replace x with your port letter and PINx with your pin number.

Key Takeaways

Always configure the LED pin as output before toggling it.
Use delays between turning the LED ON and OFF to create visible blinking.
Check your microcontroller's datasheet for correct port and pin names.
Avoid skipping delay or pin setup to prevent the LED from not blinking.
Test your code on hardware to confirm the LED blinks as expected.