How to Blink LED Using Raspberry Pi Python - Simple Guide
To blink an LED using Raspberry Pi and Python, use the
RPi.GPIO library to control the GPIO pin connected to the LED. Set the pin as output, then turn it on and off in a loop with delays using time.sleep().Syntax
Here is the basic syntax to blink an LED using the RPi.GPIO library:
GPIO.setmode(GPIO.BCM): Sets the pin numbering system to BCM (Broadcom chip-specific).GPIO.setup(pin_number, GPIO.OUT): Configures the chosen pin as an output.GPIO.output(pin_number, GPIO.HIGH): Turns the LED on.GPIO.output(pin_number, GPIO.LOW): Turns the LED off.time.sleep(seconds): Pauses the program for the given seconds.GPIO.cleanup(): Resets the GPIO pins to a safe state.
python
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # Use BCM pin numbering GPIO.setup(18, GPIO.OUT) # Set GPIO 18 as output try: while True: GPIO.output(18, GPIO.HIGH) # LED on time.sleep(1) # Wait 1 second GPIO.output(18, GPIO.LOW) # LED off time.sleep(1) # Wait 1 second except KeyboardInterrupt: GPIO.cleanup() # Clean up GPIO on CTRL+C exit
Example
This example blinks an LED connected to GPIO pin 18 on and off every second. It runs until you stop it with CTRL+C.
python
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) # Use BCM pin numbering GPIO.setup(18, GPIO.OUT) # Set GPIO 18 as output try: while True: GPIO.output(18, GPIO.HIGH) # Turn LED on print("LED ON") time.sleep(1) # Wait 1 second GPIO.output(18, GPIO.LOW) # Turn LED off print("LED OFF") time.sleep(1) # Wait 1 second except KeyboardInterrupt: GPIO.cleanup() # Reset GPIO settings print("Program stopped and GPIO cleaned up")
Output
LED ON
LED OFF
LED ON
LED OFF
... (repeats every second until stopped)
Program stopped and GPIO cleaned up
Common Pitfalls
Common mistakes when blinking an LED on Raspberry Pi include:
- Not setting the GPIO mode with
GPIO.setmode()before setup. - Forgetting to call
GPIO.cleanup()which can cause warnings or pin conflicts on next run. - Using the wrong pin numbering system (BCM vs BOARD) and wiring the LED to the wrong pin.
- Not using a resistor with the LED, which can damage the LED or the Pi.
- Running the script without root permissions (use
sudoto run the script).
python
import RPi.GPIO as GPIO import time # Wrong: Missing GPIO.setmode() GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.HIGH) # This will cause an error # Correct way: GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OUT) GPIO.output(18, GPIO.HIGH)
Quick Reference
Here is a quick cheat sheet for blinking an LED on Raspberry Pi using Python:
| Command | Description |
|---|---|
| GPIO.setmode(GPIO.BCM) | Set pin numbering to BCM mode |
| GPIO.setup(pin, GPIO.OUT) | Set a pin as output |
| GPIO.output(pin, GPIO.HIGH) | Turn LED on |
| GPIO.output(pin, GPIO.LOW) | Turn LED off |
| time.sleep(seconds) | Pause program for given seconds |
| GPIO.cleanup() | Reset GPIO pins safely |
Key Takeaways
Always set GPIO mode with GPIO.setmode() before configuring pins.
Use GPIO.setup() to set the LED pin as output before controlling it.
Use time.sleep() to create visible delays between LED on and off states.
Call GPIO.cleanup() at the end to reset pins and avoid warnings.
Run your Python script with sudo to access GPIO pins on Raspberry Pi.