Skip to content

Blink an LED with GPIO

Wire an LED and a 330 Ω resistor to GPIO 17, then blink it from Python using gpiozero.

TODO

Add a wiring diagram to images/.

Starter code

blink.py
# Blink an LED on GPIO 17: on for one second, off for one second, forever.
from time import sleep

from gpiozero import LED

led = LED(17)

while True:
    # TODO: turn the LED on, wait 1 second, turn it off, wait 1 second
    pass
Solution
blink.py
# Blink an LED on GPIO 17: on for one second, off for one second, forever.
from time import sleep

from gpiozero import LED

led = LED(17)

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

Run it on the Pi:

python3 blink.py

Press Ctrl+C to stop.