Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท GPIO and Python

Blinking an LED with gpiozero

Wire an LED to a Raspberry Pi and blink it with Python and gpiozero: the circuit, the code, blink(), toggle, loops, and a virtual environment tip.

Every electronics journey starts with a blinking LED. It is the "Hello, World" of hardware, and it teaches you the whole chain: a circuit on a breadboard, a pin in your program and a visible result. In this lesson you will build the circuit, write the code with the gpiozero library and then experiment with patterns. Because a website cannot touch real pins, the runnable examples use a simulated LED; the real code is shown read-only and is almost identical.

The circuit

You need one LED, one 220-ohm resistor, a breadboard and two jumper wires. An LED has two legs: the longer one is the anode (positive) and the shorter one is the cathode (negative). Current only flows one way through it. Build the circuit like this, with the Pi powered off:

  1. Connect GPIO17 (physical pin 11) to one end of the resistor with a jumper wire.
  2. Connect the other end of the resistor to the LED's long leg.
  3. Connect the LED's short leg to a ground pin, for example physical pin 9 or 14.
flowchart LR
  A["GPIO17 (pin 11)"] --> B["220 ohm resistor"]
  B --> C["LED anode (long leg)"]
  C --> D["LED cathode (short leg)"]
  D --> E["GND (pin 9)"]

When GPIO17 goes high, current flows through the resistor and the LED to ground.

The resistor can sit on either side of the LED; what matters is that it is in the loop. Double-check the LED direction: if it never lights, flip it around.

The code

The gpiozero library makes hardware feel like ordinary Python objects. On a real Pi (read-only):

Python
from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

Save this as blink.py and run python3 blink.py. Press Ctrl+C to stop. The LED class takes the BCM pin number. on() makes the pin high, off() makes it low, and sleep() pauses for the given seconds.

gpiozero can do the loop for you:

Python
from gpiozero import LED
from signal import pause

led = LED(17)
led.blink(on_time=0.5, off_time=0.5)   # runs in a background thread
pause()                                 # keep the program alive

Other useful methods: led.toggle() flips the state, led.is_lit tells you whether it is on, and led.close() releases the pin. If you use with LED(17) as led: the pin is released automatically at the end of the block.

Practicing without hardware

The stand-in below defines a simulated LED class with the same method names. You can write and test the logic of a program (a pattern, a counter, a Morse code message) in the browser, then paste it on the Pi after changing only the import line.

Python Stand-in: a simulated LED blinking

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Patterns and Morse code

Because the LED is just an object, patterns are just loops. The next stand-in turns text into Morse code timings, the kind of list you would feed to led.on(), sleep(t) and led.off(). It uses no real delay, so the result prints instantly.

Python Stand-in: Morse code timings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

On a Pi, replacing the print with a loop over plan that calls led.on() or led.off() and then sleep(t) sends a real SOS.

Several LEDs

gpiozero also has LEDBoard, which controls a group at once, and TrafficLights, a ready-made red, amber and green set. Each LED needs its own resistor and its own pin.

Python
from gpiozero import LEDBoard
from time import sleep

leds = LEDBoard(17, 27, 22)
for led in leds:
    led.on()
    sleep(0.5)
    led.off()

Virtual environments and libraries

Recent Raspberry Pi OS versions protect the system Python, so pip install outside a virtual environment gives an "externally managed environment" error. For gpiozero this does not matter, because it comes pre-installed and via apt. For other libraries, create an environment that can still see the system packages:

Terminal
python3 -m venv --system-site-packages ~/venv
source ~/venv/bin/activate
pip install some-library

Troubleshooting a LED that stays dark

  • LED reversed: the long leg must go toward the pin.
  • Resistor missing or too large: use 220 to 470 ohms.
  • Wrong pin number: remember BCM versus physical.
  • Loose jumper: press the wires firmly into the breadboard.
  • Pin still claimed by an old program: close it or reboot.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which leg of an LED connects toward the GPIO pin side of the circuit?

    Current flows from anode to cathode; the anode is the longer leg.
  2. 2. What does led.toggle() do?

    toggle flips between on and off.
  3. 3. How does LED(17) identify its pin?

    gpiozero uses BCM numbering by default.
  4. 4. Which command stops a running blink.py in the terminal?

    Ctrl+C sends an interrupt that ends the running Python program.

Summary

  • The LED circuit is GPIO pin, resistor, LED anode to cathode, ground.
  • LED(17), on(), off(), toggle() and blink() control it in gpiozero.
  • A simulated class lets you test logic in the browser before running on hardware.
  • Use a virtual environment with system site packages for extra libraries.

Next: Buttons and Digital Input