In the previous post we learned how to blink an LED using Atmega16. In this post we will see how we can interface push button with the microcontroller.
Our objective will be to control the glowing of an LED with the help of a push button.The following is the circuit diagram:
In this circuit the push button S1 is connected to PD0 pin of PORTD and the LED is connected to the PC0 pin of PORTC.
Start by including the required header files and declaring PORTC as output and PORTD as input port.
As you can notice in the code, we have activated the internal pull-up resistance on port D. To know more about activating pull-up resistance, please refer to the following article-
http://avrlogic.blogspot.in/2014/11/pull-up-resistance.html
Now to detect if the switch is pressed or not, we use an if statement.
Just as PORTx command is used to write a logic level to port x, PINx command is used to read the logic level of a port x.
Now, since the switch is connected to PD0, we mask all bits of PORTD, except PD0 and check its logic level. If the logic level is LOW, the control goes within if statement.
The logic levels on PORTC are then inverted. A delay of 40 ms is added to remove debounce.
Hence, on pressing the switch once, the LED will start glowing. On pressing it again, the LED switches off.
The detection of the switch being pressed is placed within an infinite loop since we want this operation to continue indefinitely. This method of checking continuously is known as Pollling.
Therefore, we have successfully interfaced a switch with the Atmega16 microcontroller.
Our objective will be to control the glowing of an LED with the help of a push button.The following is the circuit diagram:
![]() |
| Circuit Diagram |
In this circuit the push button S1 is connected to PD0 pin of PORTD and the LED is connected to the PC0 pin of PORTC.
Start by including the required header files and declaring PORTC as output and PORTD as input port.
As you can notice in the code, we have activated the internal pull-up resistance on port D. To know more about activating pull-up resistance, please refer to the following article-
http://avrlogic.blogspot.in/2014/11/pull-up-resistance.html
Now to detect if the switch is pressed or not, we use an if statement.
Just as PORTx command is used to write a logic level to port x, PINx command is used to read the logic level of a port x.
Now, since the switch is connected to PD0, we mask all bits of PORTD, except PD0 and check its logic level. If the logic level is LOW, the control goes within if statement.
The logic levels on PORTC are then inverted. A delay of 40 ms is added to remove debounce.
Hence, on pressing the switch once, the LED will start glowing. On pressing it again, the LED switches off.
The detection of the switch being pressed is placed within an infinite loop since we want this operation to continue indefinitely. This method of checking continuously is known as Pollling.
Therefore, we have successfully interfaced a switch with the Atmega16 microcontroller.
