0
0
Iot-protocolsProgramBeginner · 2 min read

Raspberry Pi Program to Blink LED with Python

Use the RPi.GPIO library in Python to blink an LED by setting a GPIO pin as output and toggling it on and off with GPIO.output(pin, GPIO.HIGH) and GPIO.output(pin, GPIO.LOW) inside a loop with delays.
📋

Examples

InputBlink LED on GPIO pin 18 for 5 seconds
OutputLED turns on and off repeatedly every second for 5 seconds
InputBlink LED on GPIO pin 17 for 3 seconds
OutputLED blinks on and off every second for 3 seconds
InputBlink LED on GPIO pin 22 for 0 seconds
OutputNo blinking occurs, program exits immediately
🧠

How to Think About It

To blink an LED on a Raspberry Pi, first choose a GPIO pin connected to the LED. Set this pin as an output. Then, repeatedly turn the pin on (power the LED) and off (turn it off) with a pause in between to make the blinking visible. Use a loop to repeat this action for the desired time or number of blinks.
📐

Algorithm

1
Import the GPIO and time libraries.
2
Set the GPIO mode to BCM to use GPIO pin numbers.
3
Set the chosen GPIO pin as an output pin.
4
Create a loop to turn the LED on by setting the pin HIGH.
5
Wait for a short delay (e.g., 1 second).
6
Turn the LED off by setting the pin LOW.
7
Wait for the same delay.
8
Repeat the on/off cycle for the desired number of times or duration.
9
Clean up the GPIO settings after finishing.
💻

Code

python
import RPi.GPIO as GPIO
import time

LED_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)

try:
    for _ in range(5):  # Blink 5 times
        GPIO.output(LED_PIN, GPIO.HIGH)  # LED on
        print("LED ON")
        time.sleep(1)  # Wait 1 second
        GPIO.output(LED_PIN, GPIO.LOW)   # LED off
        print("LED OFF")
        time.sleep(1)  # Wait 1 second
finally:
    GPIO.cleanup()  # Reset GPIO settings
    print("GPIO cleaned up")
Output
LED ON LED OFF LED ON LED OFF LED ON LED OFF LED ON LED OFF LED ON LED OFF GPIO cleaned up
🔍

Dry Run

Let's trace blinking the LED 2 times through the code

1

Setup GPIO

Set GPIO mode to BCM and configure pin 18 as output.

2

First blink ON

Set pin 18 HIGH to turn LED on; print 'LED ON'; wait 1 second.

3

First blink OFF

Set pin 18 LOW to turn LED off; print 'LED OFF'; wait 1 second.

4

Second blink ON

Set pin 18 HIGH again; print 'LED ON'; wait 1 second.

5

Second blink OFF

Set pin 18 LOW; print 'LED OFF'; wait 1 second.

6

Cleanup

Call GPIO.cleanup() to reset pins; print 'GPIO cleaned up'.

IterationLED StatePrinted Output
1ONLED ON
1OFFLED OFF
2ONLED ON
2OFFLED OFF
💡

Why This Works

Step 1: GPIO Setup

We use GPIO.setmode(GPIO.BCM) to refer to pins by their GPIO numbers and GPIO.setup() to set the LED pin as output so we can control it.

Step 2: Blinking Loop

Inside the loop, GPIO.output(pin, GPIO.HIGH) powers the LED on, and GPIO.output(pin, GPIO.LOW) turns it off, with time.sleep() pauses to make the blinking visible.

Step 3: Cleanup

Calling GPIO.cleanup() resets the GPIO pins to a safe state to avoid warnings or conflicts when running other programs.

🔄

Alternative Approaches

Using gpiozero library
python
from gpiozero import LED
from time import sleep

led = LED(18)

for _ in range(5):
    led.on()
    print("LED ON")
    sleep(1)
    led.off()
    print("LED OFF")
    sleep(1)
gpiozero is simpler and more beginner-friendly but less flexible than RPi.GPIO.
Blinking with PWM for brightness control
python
import RPi.GPIO as GPIO
import time

LED_PIN = 18
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED_PIN, GPIO.OUT)
pwm = GPIO.PWM(LED_PIN, 1000)  # 1kHz frequency
pwm.start(0)

try:
    for duty_cycle in range(0, 101, 20):
        pwm.ChangeDutyCycle(duty_cycle)
        print(f"LED brightness {duty_cycle}%")
        time.sleep(1)
finally:
    pwm.stop()
    GPIO.cleanup()
PWM allows controlling LED brightness instead of just on/off blinking.

Complexity: O(n) time, O(1) space

Time Complexity

The program runs a loop for n blinks, so time grows linearly with the number of blinks.

Space Complexity

Uses a fixed amount of memory for GPIO setup and variables, so space is constant.

Which Approach is Fastest?

All approaches run in similar time; gpiozero is simpler but adds a small overhead, PWM adds complexity for brightness control.

ApproachTimeSpaceBest For
RPi.GPIO basic blinkO(n)O(1)Simple on/off blinking
gpiozero libraryO(n)O(1)Beginner-friendly, easy code
RPi.GPIO with PWMO(n)O(1)Controlling LED brightness
💡
Always call GPIO.cleanup() at the end to reset pins and avoid warnings.
⚠️
Forgetting to set the GPIO mode to BCM or BOARD before configuring pins causes errors.