LAB NO.
2
Title: Interfacing LEDs and Switches with 8051 Microcontroller
Objective:
To learn how to interface LEDs and switches with the 8051 microcontroller and write a program
to control the LED based on the switch input.
Apparatus / Software Required:
8051 Microcontroller (AT89C51)
LEDs
Push Button Switches
Resistors (220Ω, 10kΩ)
Keil µVision (for code)
Proteus Design Suite (for simulation)
Theory:
LEDs are output devices that glow when voltage is applied. Switches are input devices that allow
current to pass when pressed.
In 8051, each I/O pin can be programmed as input or output. By configuring Port 1 (or any other
port) appropriately, we can control LEDs and read switch states.
Working Principle:
When a switch is pressed, the corresponding pin reads LOW (0) due to grounding.
When the switch is not pressed, the pin reads HIGH (1) due to pull-up resistor.
LED turns ON when the microcontroller sends a LOW signal to its cathode side (if
active-low).
Circuit Description:
Connect LEDs to Port 2 (e.g., P2.0–P2.3) via 220Ω resistors.
Connect push-button switches to Port 1 (e.g., P1.0–P1.3) with pull-up resistors (10kΩ).
Ground one side of each switch.
Connect a crystal oscillator and reset circuit for the microcontroller.
Program Code (in C):
#include <reg51.h>
void main() {
P2 = 0xFF; // Turn off all LEDs (active low)
P1 = 0xFF; // Configure switches as input
while (1) {
P2 = ~P1; // Mirror switch state to LEDs (inverted)
}
}
Procedure:
1. Write the code in Keil µVision.
2. Compile the code to generate the HEX file.
3. Design the circuit in Proteus with 8051, LEDs, switches, and necessary connections.
4. Load the HEX file into the microcontroller.
5. Run the simulation.
6. Press switches one by one and observe the LEDs turning ON or OFF accordingly.
Result:
The LEDs responded correctly to switch presses. When a switch was pressed, the corresponding
LED turned ON, verifying successful interfacing.
Conclusion:
The experiment demonstrated how to interface switches and LEDs with the 8051
microcontroller. It also showed how microcontroller I/O ports can be used for reading digital
inputs and sending output signals.