
Project-1 : How to Blink an LED with Raspberry Pi 5 and Python
Nitin ThakkarShare
Introduction:
If you're new to Raspberry Pi 5, one of the easiest ways to start experimenting with GPIO (General Purpose Input/Output) pins is to control an LED. This project will guide you through writing a Python script that makes an LED blink on and off every second. Not only will you learn about basic Python programming, but you'll also get an understanding of how to interface with hardware components using the GPIO pins on your Raspberry Pi.
Prerequisites:
Before starting the project, ensure that you have the following:
-
Raspberry Pi 5
- Red LED
- Yellow LED
- Green LED
- 220-ohm resistor
- Breadboard and jumper wires
- Python 3 is installed on your Raspberry Pi (pre-installed on most models).
- RPi.GPIO library (this is the library used to control GPIO pins on the Raspberry Pi).
Circuit Connections:
Component | Raspberry Pi Physical Pin | Description |
---|---|---|
LED Anode (+) | Pin 5 (GPIO 3) | Controls the LED (output signal) |
LED Cathode (−) | → 220Ω Resistor | Current limiting resistor |
Resistor (Other leg) | Pin 6 or 9 (GND) | Ground connection |

1. Step-by-Step Hardware Setup:
-
Connect the LED to the Breadboard:
Place the LED onto the breadboard. Ensure that the longer leg (anode) is connected to the GPIO pin through a resistor, and the shorter leg (cathode) is connected to the ground. -
Connect Resistor:
Use a 220Ω resistor from the cathode leg to the ground rail of the breadboard. -
Connect the GPIO to the LED:
Use a jumper wire to connect GPIO Pin 3 (Physical Pin 5) to the anode of the LED. -
Connect Ground:
Use another jumper wire to connect the resistor’s other leg to Pin 6 or 9 (GND) on the Raspberry Pi.
2. Connecting the Anode to the GPIO Pin:
- Connect the longer leg (anode) of the LED to GPIO pin 3 on your Raspberry Pi 5. You can use a jumper wire to make this connection. GPIO pin 3 corresponds to physical pin 5 on the Raspberry Pi's header.
3. Adding a Resistor:
- Insert a 220-ohm resistor into the breadboard. One leg should connect to the cathode of the LED, the other to a ground pin on the Raspberry Pi 5.
4. Connect to Ground:
- Use a jumper wire to connect the other end of the resistor to any of the GND pins on the Raspberry Pi 5 (e.g., pin 6 or pin 9).
- This ensures the LED receives appropriate current, as the resistor limits flow to safe levels.
5. Double-check the Wiring:
- Ensure all connections are secure before running the script. The LED must be in series with the resistor and correctly connected to GPIO and ground.
Writing the Python Code:
1. Open the terminal and create a Python script:
import time
# Set up the GPIO mode
GPIO.setmode(GPIO.BOARD)
# Set up GPIO pin 3 as an output pin
GPIO.setup(3, GPIO.OUT)
# Blink the LED indefinitely
try:
while True:
GPIO.output(3, True) # Turn the LED on
time.sleep(1)
GPIO.output(3, False) # Turn the LED off
time.sleep(1)
except KeyboardInterrupt:
print("Exiting program...")
finally:
GPIO.cleanup() # Clean up GPIO settings
This script:
- Imports required libraries.
- Sets GPIO mode to BOARD (physical pin numbering).
- Configures pin 3 as output.
- Blinks the LED on and off every second in an infinite loop.
2. Save the file and exit with Ctrl + X, then Y, and Enter.
Running the Code:
Save the file as led_blink.py
. Execute it using the command below:
-
Stopping the Script: Press
Ctrl + C
to stop the running Python script. - The sudo command ensures the script has the necessary permissions to access the GPIO pins.
- If the LED doesn’t blink, double-check your wiring and make sure the GPIO PINs in the code match your setup.
- Stopping the Script: Press Ctrl + C to exit the loop.
- This runs the script with the necessary GPIO access permissions.
- If the LED doesn’t blink, double-check wiring and PINs.
Stopping the Script:
To stop the script anytime, press Ctrl + C in the terminal window.
Conclusion:
In this simple Raspberry Pi 5 project, you learned how to control an LED using Python. This foundation can be expanded to include sensors, inputs, or logic-controlled GPIO behaviors for more advanced projects.