Push Button switch Program using ATmega8


In the Prev. tutorial of Led Blink we saw uC(Micro Controller) gives data to LED for ON/ OFF but in this tutotrial we’ll see how uC takes Data From Push Button Sensor.

Here Push Button behave as a Digital Input Sensor, As its name “digital Input” uC takes data in form of ‘0’ or ‘1’;  

suppose uC initially is in high state (i.e. ‘1’) and it takes signal at low state (i.e. ‘0’) as shown below in timing diagram. That is called Pull-Up condition or Active Low which is discussed below.

Let’s take an example of push button

Now Vcc (5v) is connected to PC0 of ATmega8 through a 10K resistor and push button connected to PC0 and another terminal to Gnd. As PC0 connected directly to Vcc now it takes High signal (5v or 1), so we can say PC0 is in High state. When push button pushed PC0 gets Low signal (Gnd or 0) because when push button is pressed PC0 directly get short with Gnd


  • Now you can observe when push button is pushed Gnd will be shorted with Vcc, to avoid short circuit just put a 10k resistor in bet Vcc and PC0.
  • Similarly, you can do for Pull-Down or Active-High circuit.


In the above circuit diagram push button is connected to PC0 through Pull-Up concept and Led is connected to PB0 for output. Like when push button is pushed Led will ON when button is released Led get OFF.



void main(){
	DDRC = 0x00;
	DDRB = 0x01;

		if((PINC&0x01) == 0){
			PORTB = 0x01;
			PORTB = 0x00;

Source Code


DDRC = 0x00;

DDRB = 0x01;

DDR(x) is used for declaration of pins, it assigns pin that you use it for either input or output operation.

‘0’ Means input

‘1’ Means output

As we know ATmega8 is a 8bit uC (microcontroller) , so if I use PC0 for input operation then PORTC 0th location becomes ‘0’

Rest of location of PORTC we can put either 0 or 1 it’s doesn’t matter because we are interested in DDC0 which should be ‘0’ because we take input operation at PC0.

DDRC = 0b00000000; //it is in binary

             = 0x00; //in hexadecimal

Similarly, we use PB0 for output so, DDB0 should be 1

So, DDRB = 0b00000001;//in binary

                  = 0x01; //in hex

while(2) //for infinity loop

 if((PINC&0x01) == 0)

this above statement is used for push condition as we know the circuit based on pull up method we get ‘0’ value at push condition n get ‘1’ at release condition.  

Only we know about PC0 value that is ‘1’ at normal condition and ‘0’ at pushed time. We don’t know about rest of pin in PORTC that may be 1 or 0, like lets take an example initially PC6 and PC4  are in ‘1’ state, if we compare PINC == 0X00 at push condition it won’t work because whole PINC status is not 0 only PC0 is ‘0’ so we ‘&’ 0x01with PINC to mask ‘0’ to the all rest of pin in PORTC

PORTB = 0x01; //used for ON led and

PORTB = 0x00;// for OFF led

If don’t know about above statement please go through the ATmega8-Digital-Output


Download the source file and extract it then open “main.c” file using Programmers Notepad[WinAVR] then go to Tools and click on “Make All” and Then ”Program”. if you face any trouble let me knoe in the comment box.

Have a nice Day!! smiley

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