Exp2 - Raspberry Pi Interfacing
Exp2 - Raspberry Pi Interfacing
Experiment: 2
Student Name: Amir Philip Adam UID: 22BEC10019
Branch: Electronics and Communication Section/Group: 22BEC-2/A
Semester: 6th Date of Performance:13/01/25
Subject Name: Raspberry Pi and its Interfacing
Subject Code: 22ECH-321
3.Theory:
The General Purpose Input/Output (GPIO) pins on Raspberry Pi allow hardware control and
interfacing. A basic example of GPIO functionality is turning an LED ON and OFF.
Digital Output: Raspberry Pi GPIO pins can output HIGH (3.3V) or LOW (0V) states.
LED Operation: A digital HIGH state can illuminate an LED connected through a current-
limiting resistor. Toggling the state in a time sequence can create a blinking effect.
1
University Institute of Engineering
Department of Electronics &Communications
Engineering
4. Circuit Diagram:
Connect the longer leg (anode) of the LED to a GPIO pin (e.g., GPIO 2) via a 330 Ω resistor.
Connect the shorter leg (cathode) of the LED to the GND pin of the Raspberry Pi.
Fig 2.1 shows the circuit for interfacing an LED with Raspberry Pi 5.
5. Proceedures
Connect the components as per the circuit diagram.
Open any code editor or IDE eg Thonny,Nano or VS Code.
Open terminal and run the command: sudo apt install python3 gpiozero.
Write the Python program to control the LED.
Save the Python code with .py extension.
Run the programming by the command sudo python3 file_name.py
2
University Institute of Engineering
Department of Electronics &Communications
Engineering
6. Code
led=LED(2)
try:
while True:
led.on()
sleep(1)
led.off()
sleep(2)
except KeyboardInterrupt:
Print(“Program stopped by the user”)
try:
while True:
signal = int(input("Enter the signal (0 or 1): "))
if signal == 1:
led.on() # Turn LED ON
print("LED is ON")
elif signal == 0:
led.off()
print("LED is OFF")
else:
print("Invalid signal, please enter 0 or 1")
except KeyboardInterrupt:
print("Program stopped by the user")
finally:
led.close()
3
University Institute of Engineering
Department of Electronics &Communications
Engineering
7. Screenshots
Fig 2.2 shows VS Code editor where the Program to blink an LED is written.
4
University Institute of Engineering
Department of Electronics &Communications
Engineering
8. Result
The LED was successfully interfaced with the Raspberry Pi. The program successfully turned the LED ON
for 1 second and OFF for 2 seconds in a continuous loop, demonstrating the GPIO control functionality of
the Raspberry Pi.
5
University Institute of Engineering
Department of Electronics &Communications
Engineering