4.1) LED Interfacing with AVR (Atmega16)

posted by Hamid Sayyed • November 12, 2025 0 Comments

LEDs are one of the simplest yet most important components used in embedded system experiments. They are often used as indicators to show the status of a process, error, power, or data transfer. In ATmega16, LEDs can be easily controlled by configuring its I/O ports as output and toggling the pins using C programs. When the output pin is set to logic HIGH, current flows through the LED, causing it to glow, and when set to LOW, the LED turns off. Understanding LED interfacing gives students the foundation to control other electronic devices like relays, motors, and sensors. In this post, we will explore the concept of LED interfacing with ATmega16, see how to write code to blink an LED, and visualize the circuit with an animated diagram that shows how data from the microcontroller controls the LED in real time.

Basic Concept of LED Interfacing

Each I/O port pin of the ATmega16 can be configured as input or output using the DDR (Data Direction Register). To make a pin output, the corresponding DDR bit is set to 1. The PORT register controls the actual logic level: writing 1 makes the pin HIGH, and 0 makes it LOW. An LED can be connected either in active high or active low mode. In the active high configuration, the LED turns ON when the port pin outputs HIGH, while in active low mode, it turns ON when the port pin outputs LOW.

Note: Always connect a resistor (typically 220Ω – 330Ω) in series with the LED to limit current and protect both the LED and microcontroller pin.

C Program to Blink LED using ATmega16

The following program demonstrates how to blink an LED connected to Port B, pin 0 of ATmega16. The LED will turn ON for a short delay and then turn OFF repeatedly.

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

int main(void)
{
    DDRB = 0x01;          // Configure PB0 as output
    while(1)
    {
        PORTB = 0x01;     // LED ON
        _delay_ms(500);   // Delay 500 ms
        PORTB = 0x00;     // LED OFF
        _delay_ms(500);   // Delay 500 ms
    }
}
  

Animated Output of Above Code

The following animated block diagram shows how ATmega16 controls an LED using its Port B pin. The square block represents the microcontroller, and the circular LED alternates between ON (glowing) and OFF (dim) states, just like the code behavior.

Explanation of the Code

In the above program, DDRB is used to configure Port B as an output port. The instruction DDRB = 0x01; sets bit 0 of Port B as output, meaning PB0 is now ready to drive an LED. Inside the infinite while(1) loop, the program continuously toggles the LED by writing 1 (HIGH) and 0 (LOW) to PB0. The _delay_ms() function from the <util/delay.h> library introduces a delay of 500 milliseconds between each ON and OFF state. As a result, the LED blinks at a frequency of approximately 1 Hz.

Applications of LED Interfacing

  • Status indicators in control systems.
  • Error or fault indication in embedded devices.
  • Debugging tool during program testing.
  • Visual confirmation of logic levels in circuits.

Conclusion

LED interfacing with ATmega16 is one of the most fundamental and essential experiments in embedded system learning. It helps beginners understand how to configure I/O ports, control output signals, and apply delays using C code. Once this basic understanding is clear, the same concept can be extended to control buzzers, motors, and even complex sensors using digital output pins.

Comments

Post a Comment

Subscribe to Post Comments [Atom]