Every electronics journey starts with a blinking LED, and on the Raspberry Pi it takes only a few lines of Python. In this lesson you connect an LED and a resistor to one of the Pi's GPIO pins and switch it on and off from a program. You learn how the pins are numbered, why the LED needs a resistor, and how gpiozero turns a pin into an LED object that you can simply tell to turn on() and off(). At the end you make the LED send SOS in Morse code.
What you'll learn
- How the GPIO pins are numbered: GPIO numbers and pin positions
- Why an LED needs a resistor, and how to work out how much current flows
- How to switch a GPIO pin on and off with gpiozero's LED
- How a while loop repeats code forever
- How to write your own function in Python
What you need
- Raspberry Pi 4 Model B with Raspberry Pi OS on a microSD card (see the first lesson) and its USB-C power supply
- 1 LED (red, yellow or green work best) and a 220 Ω resistor (ESP32 IoT Starter Kit)
- Breadboard (ESP32 IoT Starter Kit)
- Female-to-male jumper wires, to go from the Pi's pins to the breadboard. If you don't have any, plug a female-female and a male-male Dupont wire from the ESP32 IoT Starter Kit together.
Look after your Pi: the GPIO pins work at 3.3 V. Never connect 5 V (or the 5V pins) to a GPIO pin, never connect a GPIO pin straight to GND or to 3.3 V with a plain wire, never connect a 3V3 or 5V pin to GND, and always shut the Pi down and unplug it before you change the wiring. A single wrong wire can damage the Pi for good.
Wiring
The GPIO pins: the 40 pins along the edge of the Pi are its GPIO header. Python uses the GPIO numbers (GPIO 17, GPIO 27 ...), not the pin's position on the header, so the wiring tables give both, for example "GPIO 17 (pin 11)". Pin 1 is the one nearest the microSD card slot end of the header, in the row closest to the middle of the board, and it has a square solder pad underneath. Type pinout in a terminal on the Pi to see a map of all 40 pins.

Build it step by step
Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.
| From | To |
|---|---|
| GPIO 17 (pin 11) | 220 Ω resistor → LED long leg (+, the anode) |
| LED short leg (−, the cathode, next to the flat edge) | The blue (−) rail of the breadboard |
| The blue (−) rail | GND (pin 9) |
Shut down the Pi and unplug it before you build the circuit. Check every wire twice, then switch it on again.
The program
Open Thonny (Raspberry menu → Programming → Thonny), paste the program into a new file, save it in your home folder as blink.py, and click the green Run button. Press the red Stop button (or Ctrl+C) to stop it. gpiozero is already installed on Raspberry Pi OS.
# blink.py: blink an LED on GPIO 17
from gpiozero import LED
from time import sleep
led = LED(17) # GPIO 17 is pin 11 on the header
while True: # repeat forever (press Stop to end)
led.on()
print("LED on")
sleep(0.5) # wait half a second
led.off()
print("LED off")
sleep(0.5)
Try it
- Run the program. The LED blinks once a second, and the Shell prints "LED on" and "LED off".
- Make it blink faster: change both
sleep(0.5)lines tosleep(0.1). How fast can it blink before your eyes see it as always on? - Make it a heartbeat: a short flash (0.1 s on), a short gap, another short flash, then a long gap (1 s).
- Press Stop. The LED goes off: gpiozero switches the pin off when the program ends.
- Now try SOS. Save this second program as
sos.pyand run it:
# sos.py: flash S O S in Morse code, then wait
from gpiozero import LED
from time import sleep
led = LED(17)
DOT = 0.2 # one dot lasts 0.2 seconds; everything else is measured in dots
def flash(length):
led.on()
sleep(length)
led.off()
sleep(DOT) # a short gap after every flash
while True:
for letter in ["...", "---", "..."]:
for sign in letter:
flash(DOT if sign == "." else 3 * DOT)
sleep(2 * DOT) # a longer gap between letters
print("SOS sent")
sleep(2)
The LED flashes three short, three long and three short flashes, then waits two seconds and starts again.
What you should see

How it works

When the program sets GPIO 17 on, the pin is connected to 3.3 V inside the Pi; when it is off, the pin is at 0 V. With 3.3 V on the pin, current flows through the resistor and the LED to GND, and the LED lights up. An LED only lets current through one way: from the long leg (+) to the short leg (−). Put it the other way round and it stays dark, but nothing breaks.
Why the resistor? A red LED needs about 2 V. The other 1.3 V must go somewhere: the resistor takes it. With 220 Ω the current is 1.3 V ÷ 220 Ω ≈ 6 mA, which is bright enough and safe. Without the resistor the LED would try to draw far more current than a GPIO pin can give, and you could damage the pin or the Pi. A Pi pin should give no more than about 16 mA. Blue and white LEDs need about 3 V, so on a 3.3 V pin they only glow faintly.
LED(17) creates an LED object for GPIO 17. gpiozero sets up the pin as an output for you, and switches it off when the program ends. while True: repeats the indented lines forever. In sos.py, def flash(length): makes your own function: a named block of code that you can run again and again with a different length. Morse code is measured in dots: a dash is three dots long, and the gaps between flashes, letters and words are one, three and seven dots.
gpiozero can also blink by itself: led.blink(on_time=0.5, off_time=0.5) starts blinking in the background while your program does something else.
Try this next
- Use
led.blink(on_time=0.2, off_time=0.8)andfrom signal import pause+pause()instead of the loop. The LED blinks while the program is free for other work. - Add a second LED with its own 220 Ω resistor on GPIO 27 (pin 13) and make the two LEDs blink in turn, like a railway crossing.
- Ask for a word with
input()and flash it in Morse code. Start with a few letters in a dictionary:MORSE = {"S": "...", "O": "---"}. - Next lesson: read a push button and switch the LED with it.
Troubleshooting
- The LED never lights: turn it round (long leg towards the resistor and GPIO 17). Check that the LED and the resistor are in the same numbered column of the breadboard where they meet.
-
Nothing happens and there is no error: check that the wire goes to the GPIO number in the program, not the pin with that position number. GPIO 17 is pin 11. Run
pinoutin a terminal to check. - The LED is very dim: check that you used 220 Ω (red, red, brown, or red, red, black, black on 5-band resistors) and not 10 kΩ (brown, black, orange, or brown, black, black, red).
- "GPIO busy" or "pin already in use": another program (maybe an earlier run that is still going) is using the pin. Press Stop in Thonny, close other programs that use GPIO, and run it again.
-
Always shut down properly: click the Raspberry menu → Shutdown, or type
sudo shutdown now, and wait until the green light stops flashing before you unplug the power. Pulling the plug while the Pi is working can damage the files on the microSD card.

