Led blink using ATmega8

ATmega8 GPIO 

ATmega8 Provides GPIO pins (General Purpose Input Output) for digital operation that may be digital input or digital output. Atmega8 having 3 PORTs (PORTB, PORTC, PORTD) AND they give 23 GPIO pins.

PORTB – 8 GPIO pins (PB0 – PB7)

PORTC – 7 GPIO pins (PC0 – PC6)

PORTD – 8 GPIO pins (PD0 – PD7)

ATmega8 Datasheet here

We can take any one of digital line to produce digital output so let’s see how its work!!

Some Digital output devices like LED, Seven Segment, Relays, LED matrix, digital Watch etc. here we interface one led with ATmega8


In the above discussion as we know ATmega8 having 3 PORTs and each PORT having some GPIO pins (i.e. 23 GPIO pins in ATmega8), now the thing is we can connect our Led to anyone these GPIO pins 

Here I connect Led +ve lead to PORTB0 (i.e. PB0) through a resistor (220E) because led required 2-3v but ATmega8 uC output is 5v that’s why we need a resistor in series with led. and Led -ve lead connected to Gnd pin of ATmega8 as per circuit diagram shown above.

How to calculate resistor value:

Lets take led draw current = 10mA-18mA

Led need voltage = 3v

So voltage drop across resistor = 2v

R = V/I

    = 2v/10mA = 200E (for 10mA)

R = V/I

So, in 112E or 200E led will glow in full brightness for safety purpose we can put resistor little higher than we calculated theoretically so we can use any value in bet 150E to 330E so here I use 220E

    = 2v/18mA = 112E (for 18mA)


Before we start coding, we should aware of 3 things which will help in programming i.e.

DDR(x) – Data direction resistor

            As it name shows this syntax gives direction to the resistor whether its working for input operation or output operation.

DDR(x) = 1 used for output

DDR(x) = 0 used for input

PORT(x) – Output from pin x

            This syntax used for produce high signal in a particular channel as it define

For example, PORTB = 0x01;

Means this command high the PB0 channel (on the other hand we can say PB0 became 5v)

PIN(x) – Input to pin x

            This syntax is used for take input in a particular channel

For example, a digital sensor is connected to PORTC channel0(i.e. PC0)

So, syntax is (PINC & 0X01) == 0X00 I’ll explain this code in digital input session.



void main(){
	DDRB = 0X01;
	while(1){//Infinity loop
		PORTB = 0X01;
		PORTB = 0x00;


#include:- it includes input output header file which is in avr folder; syntax like DDR, PROT, PIN are derive from this header file.

#include :- it includes delay header file which is in util folder; syntax like _delay_ms(), _delay_us()  are derived from this header file.

DDRB = 0X01;

In the above code we take PORTB to program the led, as we know ATmega8 is 8bit uC so PORTB having 8bit register storage location like below

Now we connected led to PB0; led is a digital output device as shown below

As we discussed above in case of DDR(x) declaration  we used ‘1’ for output and ‘0’ for input, here we take output from PB0 so PB0 bit location should be ‘1’ and rest of bit location (PB1, PB2,—-,PB7) that you can put either ‘0’ or ‘1’ it doesn’t matter but PB0 should be ‘1’

If you want to represent DDR(x) in binary it will be

DDRB = 0b00000001;

In HEX it will be

DDRB = 0x01; it is not case sensitive you can use either 0X01 or 0x01 but DDR(x) must be in uppercase

Note: here we use ‘B’ port channel to programme the led if you use ‘C’ port or ‘D’ port it would be like DDRC or DDRD

PORTB = 0X01;

As we discussed above in case of PORT(x) command ‘1’ used for generate HIGH signal(5v) and ‘0’ used for low signal(Gnd).

Like DDR(x), If you want to represent PORT(x) in binary it will be

PORTB = 0b00000001;// for ON the led

In HEX it will be

PORTB = 0x01; it is not case sensitive you can use either 0X01 or 0x01 but PORT(x) must be in uppercase

PORTB = 0x00; // for OFF the led


            This command produces 1000 milli second delay that is equal to 1 sec.


So whole code is about your led is ON for 1 sec and OFF for 1 sec it behaves like a blink circuit. For continuous ON n OFF here we use a infinity loop statement i.e. while (1)

Leave a Comment

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

Scroll to Top


Start your journey with us.

I hope this message finds you well. I’m writing to inquire about your offerings in IoT, robotics, and software solutions courses. Could you please provide details regarding the curriculum, duration, and enrollment process? Thank you in advance for your assistance.

Inquiry Now