Raspberry Pi Multiple LEDs

Scale up from one LED to several by wiring each with its own resistor and controlling them together with gpiozero's LEDBoard class, building toward a simple traffic-light pattern.

Wiring Multiple LEDs

Each additional LED needs its own current-limiting resistor and its own GPIO pin - resistors are never shared between LEDs, because each LED draws its own current independently. All the LEDs' cathodes can share the same GND rail on the breadboard, since ground is common across the whole circuit; only the resistor-and-anode side needs a dedicated GPIO pin per LED.

Note: Every GPIO pin you use adds to the Pi's total current draw. A single LED at a sensible brightness draws only a few milliamps, so two or three LEDs are no problem, but if you start adding many bright LEDs (or other current-hungry components) at once, check that the combined draw stays within safe limits for your board, or drive larger loads through a transistor instead of directly from a GPIO pin.

The LEDBoard Class

Controlling three separate `LED` objects one at a time gets repetitive. gpiozero's `LEDBoard` class groups multiple LEDs into a single object, so you can turn them all on or off together, or address any individual LED by a name you choose when you create the board.

Creating and controlling an LEDBoard

from gpiozero import LEDBoard

lights = LEDBoard(red=17, yellow=27, green=22)

lights.on()              # all three LEDs on at once
lights.off()             # all three off at once
lights.red.on()          # address one LED by the name you gave it
lights.value = (1, 0, 1) # tuple sets red=on, yellow=off, green=on, in pin order
  • Named attributes (`lights.red`, `lights.yellow`, `lights.green`) - each behaves exactly like an individual `LED` object
  • `lights.value` - a tuple you can read or set to control every LED's state in one line
  • `lights.on()` / `lights.off()` - control every LED on the board simultaneously
  • Indexing like `lights[0]` also works, following the order the pins were declared in

A Simplified Traffic Light

A classic beginner project is a traffic light sequence: red for a while, a brief yellow transition, then green, then yellow again before returning to red. This isn't wired to any real intersection logic, but it's a good exercise in sequencing multiple outputs with timed delays - the same pattern you'd use for any multi-stage indicator.

A traffic light sequence

from gpiozero import LEDBoard
from time import sleep

lights = LEDBoard(red=17, yellow=27, green=22)

while True:
    lights.red.on()
    sleep(3)
    lights.red.off()
    lights.yellow.on()
    sleep(1)
    lights.yellow.off()
    lights.green.on()
    sleep(3)
    lights.green.off()
    lights.yellow.on()
    sleep(1)
    lights.yellow.off()
ColorBCM PinPhysical Pin
RedGPIO1711
YellowGPIO2713
GreenGPIO2215
Note: For patterns that repeat with more than a couple of states, `itertools.cycle()` combined with a list of `(led, duration)` pairs can replace a long chain of repeated on()/sleep()/off() calls, making the sequence easier to extend without duplicating code.

Exercise: Raspberry Pi Digital Output

Before you can turn a GPIO pin HIGH or LOW to control an LED, what must you first do in code?