0
0
Raspberry Piprogramming~7 mins

Multiple LED patterns in Raspberry Pi

Choose your learning style9 modes available
Introduction

We use multiple LED patterns to make lights blink in different ways. This helps show different signals or fun effects.

To show different alerts using lights on a Raspberry Pi.
To create simple light shows for decorations or learning.
To test if multiple LEDs are working correctly.
To make a game or project more interactive with light signals.
Syntax
Raspberry Pi
import RPi.GPIO as GPIO
import time

# Setup GPIO pins
GPIO.setmode(GPIO.BCM)
LED_PINS = [17, 27, 22]
for pin in LED_PINS:
    GPIO.setup(pin, GPIO.OUT)

# Turn on LEDs in a pattern
for pin in LED_PINS:
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(pin, GPIO.LOW)

GPIO.cleanup()

Use GPIO.setmode(GPIO.BCM) to refer to pins by their Broadcom number.

Always clean up GPIO pins with GPIO.cleanup() to avoid warnings.

Examples
This turns on each LED one by one with a short delay, then turns it off.
Raspberry Pi
for pin in LED_PINS:
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.3)
    GPIO.output(pin, GPIO.LOW)
This blinks all LEDs on and off together three times.
Raspberry Pi
for _ in range(3):
    for pin in LED_PINS:
        GPIO.output(pin, GPIO.HIGH)
    time.sleep(1)
    for pin in LED_PINS:
        GPIO.output(pin, GPIO.LOW)
    time.sleep(1)
This lights LEDs in order, then turns them off in reverse order.
Raspberry Pi
for pin in LED_PINS:
    GPIO.output(pin, GPIO.HIGH)
    time.sleep(0.2)
for pin in reversed(LED_PINS):
    GPIO.output(pin, GPIO.LOW)
    time.sleep(0.2)
Sample Program

This program runs three LED patterns repeatedly until you stop it with Ctrl+C. It safely cleans up the pins afterward.

Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
LED_PINS = [17, 27, 22]
for pin in LED_PINS:
    GPIO.setup(pin, GPIO.OUT)

try:
    while True:
        # Pattern 1: Turn on LEDs one by one
        for pin in LED_PINS:
            GPIO.output(pin, GPIO.HIGH)
            time.sleep(0.5)
            GPIO.output(pin, GPIO.LOW)

        # Pattern 2: Blink all LEDs together
        for _ in range(3):
            for pin in LED_PINS:
                GPIO.output(pin, GPIO.HIGH)
            time.sleep(0.5)
            for pin in LED_PINS:
                GPIO.output(pin, GPIO.LOW)
            time.sleep(0.5)

        # Pattern 3: Light up in order, turn off in reverse
        for pin in LED_PINS:
            GPIO.output(pin, GPIO.HIGH)
            time.sleep(0.3)
        for pin in reversed(LED_PINS):
            GPIO.output(pin, GPIO.LOW)
            time.sleep(0.3)

except KeyboardInterrupt:
    pass

finally:
    GPIO.cleanup()
OutputSuccess
Important Notes

Make sure your LEDs have proper resistors to avoid damage.

Use try-except-finally to handle stopping the program safely.

Delays control how fast the LEDs blink or change.

Summary

Multiple LED patterns help show different light effects on Raspberry Pi.

Use loops and delays to create patterns like blinking or chasing lights.

Always clean up GPIO pins to keep your Raspberry Pi safe.