Interfacing of Push Button with Atmega16

In this tutorial post, I am going to guide you about interfacing of push button with Atmega16 microcontroller. I hope all of you have read my previous post of LED blinking or at-least has an idea about blinking of an LED. Before interfacing “push button”, let’s understand how to make ports work as an input port as well as an output port. We need to set ports as an input port if we want to take input similarly, we need to set ports as an output port if we want to give output. We can do this by declaring a suitable value to DDR register.

For declaring an input port

DDRB=0;         //Decimal 0, giving all the bits of DDRB register 0 value.
DDRB=0x00;  //Hexadecimal
DDRB=0b00000000;// binary

For declaring an output port

DDRB=255;         //Decimal number, setting all the bits of DDRB register high
DDRB=0xff;        // Hexadecimal,
DDRB=0b11111111; // Binary value

We can declare an individual pin as an input as well as an output pin.

DDRB=127;               //If you put that value to DDRB register, Pin7 would be an input pin and                                           //rest of the pin would be output pin.
DDRB=0x7f;             //Representing the same with hexadecimal numbers
DDRB=0b01111111; //Representing the same with binary.
DDRB=1<<2;          // Using shift operator to convert 2nd pin of port B as an output pin.

Push Button

Push Button AVR

A push-button works just like a switch works. It is used to give user input to the microcontroller. In this post, we are going to control an LED by push button. Yes, it’s possible, Don’t Freak Out.

For the interfacing of push-button with Atmega16 microcontroller, we need to declare a port as an input port. Then continuously monitoring the status of the pin it is connected. If the button is pressed or released, its value is going to change. Then according to the status of the pin, we will command LED to turn on or off.

Interfacing of push-button with Atmega16-Program

The program is pretty much straightforward. We are going to check the status of the particular pin- declared as the input pin. If it’s ‘1’, that implies that the switch is pressed and we have to turn on the LED. Similarly, Is the status of the pin is ‘0’, it means that the switch is released and we have to switch off the LED.

#include <avr/io.h>
#include <util/delay.h>
int main(void)
DDRA=0x00;// for input port-LED
DDRD=0xff;// for output port-Switch
          if(PINA==0x01)// checking the status of PIN, if it is '1', turns on the LED
          _delay_ms(100); // for debouncing of switch
           PORTD=0x01;   // Turning on the LED
           else if(PINA==0x00)// checking the status of PIN, if it is '0', turns off the LED
           _delay_ms(100); // For debouncing of switch
            PORTD=0x00;// Turning off the LED

Debouncing of Switch

We need to debounce the switch because it prevents from processing multiple presses. For example, if you do not introduce delay and press the push button, it will process a single press as multiple presses. Therefore, we need to introduce a delay. I have introduced a delay of 100 ms(Mili-Second).

Interfacing of push button with Atmega16-Execution 

For the execution of the program, you can refer the schematic given below drawn in Proteus. But here the thing is, this program doesn’t run as expected in my Proteus. But I can assure you this is a correct program because it is working fine on my development board. Sometimes, Proteus doesn’t simulate the program properly and that’s why I had recommended earlier to buy a development board of Atmega 16. Even though, you can refer to the schematic given below. For inserting the push button into the schematic, just search the ” push button ” and insert it(how to Simulate program on Proteus). In the development board, connect a push button to PIN A0 of Port A using a jumper wire. Connect LED with Pin D0 of Port D. Program your microcontroller(How to program your microcontroller) and if everything works fine, Congratulations!!

Interfacing of push button with Atmega32

Interfacing push button AVR Interfacing Push Button AVR


As mentioned by Aditya Abhinav in the comment, I should have added a resistor of 10K between the button and the ground in the Proteus. It’s working fine on the development board is because of the pulldown resistor. Why you should put this resistor?

This is explained well in this youtube video.

11 thoughts on “Interfacing of Push Button with Atmega16”

  1. Hello there, You’ve done an incredible job.
    I’ll certainly digg it and personally suggest to my friends.
    I am sure they will be benefited from this web site.

  2. Sir i want to make a program in which i used one push button on input and 8 leds on output port. I want in this program when i press the button one time then the first led is glow only one time and the others npt glow and when i press the button two times then the second led is glow two times and others are off…. and so on when i press the push button 8 time then the 8th led is glow 8 times and others will be off.
    Plz sir give me the solution and its program in embedded c

    1. Bonjour à tous je galère depuis qu’elle semaine, je suis débutant dans la programmation et j’aimerai écrire un code avec 3 boutons, 3 led sur de attiny, serai t’il possible sur les boutons de les mettre en automaintien, un seul appui pour qu il reste toujours à l’état haut même si l’on rappui dessus il reste toujours à l’état haut (allumé).
      Un grand merci !!

  3. I appreciate, cause I found just what I was looking for. You’ve ended my four day long hunt! God Bless you man. Have a great day. Bye

  4. It is not working in simulation because you need to provide both logic 1 and 0 I had the same issue just use a 3 pin switch(SW-SPDT) it will work just fine. Thanks for the article!!

    1. Oh, is it? thanks for the feedback. I will update this article with your suggestion ASAP. Thanks for the compliment!

  5. Aditya Abhinav

    I’m begginer here.. but if you put a ground in between the push button and PA0 pin. Then it works fine.
    Tell me if I’m wrong

    1. Yeah. You are right!
      Actually, that post was written a long time ago and I didn’t realize it then. I realized it afterward but forgot to update this article. Now I needed to update it(and maybe few more) ASAP. Thank you for bringing that up..:)

Leave a Comment

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

Share This
Scroll to Top