Raspberry Pi Blinking an LED

Build your first working circuit and program - an LED wired to a GPIO pin - and learn how gpiozero's LED class turns a few lines of Python into reliable on/off and blinking control.

Wiring the LED

Connect the LED's longer leg (the anode) through a current-limiting resistor to a GPIO pin - GPIO17 is a common, safe choice - and connect the shorter leg (the cathode) to any GND pin on the header. The resistor can sit on either side of the LED in the series path; what matters is that it's somewhere between the GPIO pin and ground so it always limits the current.

Note: Skipping the resistor, or reversing the LED's polarity, are the two most common mistakes at this stage. Reversing polarity simply means the LED won't light (LEDs are diodes and only conduct one way), but skipping the resistor can draw excessive current and damage the LED or the GPIO pin - always double check both before powering on.

The gpiozero LED Object

gpiozero represents the LED as a Python object rather than a raw pin number you have to remember to configure. When you create `LED(17)`, gpiozero automatically sets GPIO17 as an output pin behind the scenes - you never call a separate 'setup' step. The number you pass is the BCM GPIO number printed on most pinout diagrams, not the physical pin position on the header.

Turning the LED on, off, and toggling it

from gpiozero import LED
from time import sleep

led = LED(17)  # BCM GPIO17, physical pin 11

led.on()
sleep(1)
led.off()
sleep(1)
led.toggle()       # flips whatever state it was previously in
print(led.is_lit)  # True or False, reflecting the current state
  • `led.on()` - drives the pin HIGH, lighting the LED
  • `led.off()` - drives the pin LOW, turning the LED off
  • `led.toggle()` - switches to the opposite of its current state
  • `led.is_lit` - a read-only property that's True when the LED is currently on
  • `led.close()` - releases the GPIO pin so it can be reused or safely shut down

Blinking Automatically with .blink()

Rather than writing your own on/off/sleep loop, gpiozero's LED class has a built-in `.blink()` method that handles the timing for you. By default it runs on a background thread, so your program keeps executing other code while the LED blinks in the background - useful when you want blinking alongside other logic running at the same time.

Blinking with custom timing

from gpiozero import LED
from time import sleep

led = LED(17)

led.blink(on_time=0.5, off_time=0.5, n=10, background=True)
print("This prints immediately, while the LED keeps blinking in the background")
sleep(6)  # give the background blink time to finish its 10 cycles
ParameterMeaning
on_timeSeconds the LED stays lit each cycle (default 1)
off_timeSeconds the LED stays off each cycle (default 1)
nNumber of blink cycles; None means blink forever (default)
backgroundIf True, blinking runs on a separate thread and control returns immediately; if False, blink() blocks until finished

If you need tighter control - for example, changing the blink pattern based on a sensor reading between flashes - you can skip `.blink()` entirely and write your own loop with `led.on()`, `sleep()`, and `led.off()`. This gives up the convenience of `.blink()` but lets you react to changing conditions between each flash, which a fixed on_time/off_time pattern can't do.