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.

Share This