Skip to content

LED Blink Using ATmega8 – AVR GPIO Output Tutorial (Beginner Friendly)

ATmega8 GPIO Output Circuit Diagram for LED Blink – AVR Microcontroller Project

Blinking an LED is the simplest way to get started with ATmega8 programming. The basic steps are:

• Configure a GPIO pin as output.
• Turn the LED ON and OFF with a delay.

Components Required

• ATmega8 microcontroller
• LED
• 220Ω resistor (for current limiting)
• AVR Programmer (like USBasp)
• Breadboard and Jumper Wires
• 5V Power Supply

Programming Diagram

AVR and USBasp Programming Connection

Circuit Diagram

Atmega8 Led Blink circuit diagram

Circuit Connection

• Connect LED’s positive leg (anode) to ATmega8’s PB0 (Pin 14)
• Connect LED’s negative leg (cathode) to GND through a 330Ω resistor

ATmega8 PinConnection
PB0 (Pin 14)LED Anode
GNDLED Cathode (through 220Ω resistor)

Code

#define F_CPU 12000000UL  // Set to your microcontroller's clock speed
#include <avr/io.h>
#include <util/delay.h>   // Include delay library

int main(void) {
	DDRB |= (1 << PB0);  // Set PB0 as output
	
	while (1) {
		PORTB |= (1 << PB0);  // Turn LED ON
		_delay_ms(500);       // Wait for 500ms

		PORTB &= ~(1 << PB0); // Turn LED OFF
		_delay_ms(500);       // Wait for 500ms
	}

}

Code Breakdown

Setting Up the Pin

• DDRB |= (1 << PB0);

This sets PB0 as an output pin. DDRB (Data Direction Register for Port B) controls the mode of pins.
• 1 << PB0 means “set the 0th bit of DDRB to 1,” making it an output pin.

Turning the LED ON

• PORTB |= (1 << PB0);

This sets PB0 HIGH, turning the LED ON.

Delay

• _delay_ms(500);

 Waits for 500 milliseconds.

Turning the LED OFF

• PORTB &= ~(1 << PB0);

This clears the PB0 bit, setting it LOW, turning the LED OFF.

Infinite Loop

• while (1) { … }

Ensures that the LED keeps blinking continuously.

Leave a Reply

Your email address will not be published. Required fields are marked *