Lab Manual: Interfacing LED and Push Button with Raspberry Pi
Objective:
To interface an LED and a push button with Raspberry Pi GPIO pins and control the LED using
Python programming.
Components Required:
- Raspberry Pi (any model with GPIO) - 1
- LED - 1
- Push Button - 1
- Resistors (220 Ohm for LED, 10k Ohm for pull-down) - 2
- Breadboard - 1
- Jumper Wires - As required
- Monitor, Keyboard, Mouse, and SD Card (with RPi OS) - 1 set
GPIO Pin Configuration:
- LED - GPIO27 (Pin 13)
- Button - GPIO17 (Pin 11)
Python Code:
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
LED_PIN = 27
BUTTON_PIN = 17
GPIO.setup(LED_PIN, GPIO.OUT)
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
print("Press the button to turn ON the LED...")
try:
while True:
if GPIO.input(BUTTON_PIN) == GPIO.HIGH:
GPIO.output(LED_PIN, GPIO.HIGH)
print("LED ON")
else:
GPIO.output(LED_PIN, GPIO.LOW)
print("LED OFF")
time.sleep(0.1)
except KeyboardInterrupt:
print("Program stopped")
finally:
GPIO.cleanup()
Procedure:
1. Connect the LED and resistor in series to GPIO27 and GND.
2. Connect one terminal of the button to GPIO17 and the other to GND.
3. Use a 10k Ohm pull-down resistor between GPIO17 and GND.
4. Boot Raspberry Pi and open the terminal.
5. Create a Python script using: nano led_button.py
6. Paste the Python code and save (Ctrl + O, Enter, Ctrl + X).
7. Run the script: python3 led_button.py
8. Press the push button and observe the LED behavior.
Expected Result:
When the push button is pressed, the LED turns ON. When the button is released, the LED turns
OFF.
Learning Outcomes:
- Understanding of GPIO pin configuration on Raspberry Pi.
- Use of Python to control hardware peripherals.
- Practical implementation of pull-down resistor logic.
- Real-time response to input signals.
Precautions:
- Ensure correct GPIO pin mapping to avoid short circuits.
- Do not connect LED directly without a current-limiting resistor.
- Handle GPIO pins carefully to avoid hardware damage.