Summary of Switch Status & LED with AVR microcontroller
Summary: This article explains interfacing seven LEDs and one pushbutton with an Atmega32 by wiring LEDs to PORTD pins PD0–PD6 as outputs and the button to PD7 as input. It outlines required header files, function prototypes for pressed and released button handling, global arrays for tracking pressed/released states and current LED index, DDRD and PORTD initialization, and an infinite main loop. The approach cycles LEDs using a single switch with expandable array sizes for more buttons/LEDs.
- Atmega32 microcontroller
- 7 LEDs
- 1 pushbutton switch
- Current limiting resistors for LEDs
- Pull-up or pull-down resistor for switch (handled by PORTD = 0b10000000 in article)
- Power supply (Vcc and GND)
- Connection wires and breadboard or PCB
- Atmel Studio software
Generally we know about interfacing button switches, LED with Atmega32 microcontroller. We know that interface switch’s to one port, LED’s to another port and if we press any switch the corresponding LED will glow. But all the LED’s operate with single switch we don’t know.
Here we are developing the program for operating 7 LED’s with one switch. The circuit connections and programming is so simple. We will discuss here about that. First we see the circuit connections. Let us take any one port of Atemga32 controller is PortD. Connect 7 LED’s to PortD and connect on switch to remaining pin of the PortD as shown in circuit diagram.
Now let us start the coding for microcontroller. First open the Atmel Studio software of any version. Create a new project with your desired file name. Here I am taking the project name as “button switch”. In the text window type the program mentioned below and we will discuss each step clearly.
#include<avr/io.h>
#include<util/delay.h>
These are the header files for Atmega32 microcontroller main function and delay function.
void pressedbutton (int a);
void releasedbutton (int b);
This is the prototype declarations for the code blocks. These are mentioned at the beginning of the program. One is for button pressed and another is for button released. Next thing ‘a’ and ‘b’ are the two integer values. We don’t know which button is pressed, so ‘a’ stores the button pressed value and as same like ‘b’ also. We don’t know which button is released, so ‘b’ reads the released button status.
int pressed_level[1];
int released_level[1];
int pressed[1];
int lednumber[1];
This is the declaration of the global variables. The variables are represented in arrays because number represents the capacity of the array. If we want to increase the buttons the array size may change according to our requirements. For example we need to add two buttons for two ports and operating the 14 LED’s we need to replace 1 with 2. Here ‘pressed’ is the global variable to store the status of pressed button. Declaring the new variable as lednumber which can represents which LED is in on position.
Next about main function, we will have much aware about main function right…, next port initialization. Here we are taking PORTD among form the four ports. In PORTD from pin0 to pin6 we are declaring as outputs because we are interfacing LED’s to these pins only and pin7 is declaring as input because the button is connected to this pin only.
DDRD = 0b01111111;
PORTD = 0b10000000;
The above statements represent the port initialisation ‘0b’ means representing in binary values. Instead of ‘0b01111111’ we can represent with 0X7F also. The only difference is number format ‘01111111’ represents the binary value and ‘0X7F’ represents the hexadecimal value. In data direction register ‘0’ represents the input and ‘1’ represents the output. So in PORTD 7th declared as input and ‘0 to 6th’ pin declared as outputs and by using the PORT register all the pins ‘0 to 6’ make as output low and 7th pin as input high.
while (1)
{
}
This is loop condition that executes the condition infinite number of times.
For more detail: Switch Status & LED with AVR microcontroller
- Which Atmega32 port is used for LEDs and switch?
The article uses PORTD: PD0 to PD6 for LEDs and PD7 for the switch. - How are the data direction and initial values set for PORTD?
DDRD = 0b01111111 sets PD0–PD6 as outputs and PD7 as input; PORTD = 0b10000000 sets PD7 high and PD0–PD6 low. - What header files are required for the program?
The article uses avr/io.h and util/delay.h. - How does the code track which LED is on?
It uses global arrays including lednumber and pressed to store LED index and button status. - Can the design be expanded to more LEDs and buttons?
Yes; the article states array sizes can be increased to add buttons and operate more LEDs. - Where is the button connected on PORTD?
The button is connected to pin PD7 (the remaining pin of PORTD). - What does 0b01111111 represent in the code?
It is a binary literal setting pin directions: 1 for outputs (PD0–PD6) and 0 for input (PD7). - What development environment is recommended?
The article recommends using Atmel Studio of any version to create the project.

