4.2) Push Button Interfacing Using AVR (Atmega16)

posted by Hamid Sayyed • November 12, 2025 0 Comments

Push buttons are simple mechanical switches that allow a user to give digital input to a microcontroller. They are widely used in embedded systems for controlling devices, resetting circuits, or triggering specific events. In ATmega16, a push button can be connected to any input pin and read by the program to perform a particular task. The microcontroller continuously monitors the button state and performs an operation whenever it is pressed or released. In this experiment, we will learn how to interface a push button with an LED on ATmega16 and write a C program to toggle the LED every time the button is pressed. This concept introduces students to the fundamentals of input–output synchronization and helps them understand how simple human interaction can control digital electronics effectively.

We will use a single push button connected to Port D pin 0 and an LED connected to Port B pin 0. Each time the button is pressed, the LED will change its state — turning ON if it was OFF and vice versa. The LED will stay in its new state even after the button is released until it is pressed again.

Basic Concept

The push button works as a digital input device. When pressed, it connects the input pin to logic LOW (0V), and when released, the pull-up resistor keeps it HIGH (5V). This arrangement is known as an active-low input configuration. The microcontroller can detect this change by reading the input pin logic. The program logic uses a variable to remember the LED’s state, and each valid button press toggles this state.

Note: Mechanical switches like push buttons produce small unwanted fluctuations called “bounce”. A short software delay (debounce delay) is added after detecting a press to avoid multiple detections from a single press.

C Program for Push Button Interfacing with ATmega16

The following program turns ON the LED when the button is pressed once and keeps it ON. When the button is pressed again, it turns OFF the LED. The program uses simple I/O and a delay for switch debouncing.


#include 
#include 

int main(void)
{
    DDRB |= (1 << PB0);    // Set PB0 as output (LED)
    DDRD &= ~(1 << PD0);   // Set PD0 as input (Button)
    PORTD |= (1 << PD0);   // Enable pull-up resistor on PD0

    uint8_t led_state = 0; // Store LED state

    while (1)
    {
        // Check if button is pressed (active low)
        if (!(PIND & (1 << PD0)))
        {
            _delay_ms(50); // Debounce delay
            if (!(PIND & (1 << PD0))) // Confirm still pressed
            {
                // Toggle LED state
                led_state = !led_state;

                if (led_state)
                    PORTB |= (1 << PB0);   // Turn ON LED
                else
                    PORTB &= ~(1 << PB0);  // Turn OFF LED

                // Wait until button is released
                while (!(PIND & (1 << PD0)));
                _delay_ms(50); // Debounce release
            }
        }
    }
}

  

Animated Circuit Diagram

The animation below shows the working of the above code. The push button (on Port D) is connected to the ATmega16, and the LED (on Port B) toggles its state each time the button is pressed. The LED remains in its last state until the next press, demonstrating a toggle action.

PD0
PB0

Explanation of the Program

In this code, the button is connected to Port D pin 0, and the LED is connected to Port B pin 0. The pull-up resistor keeps the button input HIGH when not pressed, and when pressed, the pin reads LOW. The program continuously monitors the state of PD0. Whenever a LOW signal is detected, it toggles the variable led_state and updates the LED output accordingly. The small delay helps in debouncing, ensuring that a single press does not register multiple toggles due to mechanical noise.

Applications

  • Used for mode selection or control inputs in embedded devices.
  • Commonly used in menu navigation and user interface systems.
  • Used in testing and debugging circuits for hardware control.
  • Forms the basis of keypad and switch matrix interfaces.

Conclusion

Push button interfacing with ATmega16 is a fundamental experiment to understand how human input can be read and processed by a microcontroller. The toggle LED example introduces concepts like switch debouncing, input polling, and state retention. Mastering this basic interface is essential before learning advanced input systems such as keypads and interrupt-based event handling.

Comments

Post a Comment

Subscribe to Post Comments [Atom]