Blink an LED With Python and GPIO on Raspberry Pi

· 2 min read raspberry-pigpiopythonelectronicsled

Blinking an LED is the hardware equivalent of hello world. It teaches pin numbering, wiring polarity, and controlling the physical world from code. Ten minutes of work, and everything you learn scales to relays, sensors, and robots.

What you need

  • Any Raspberry Pi with a 40-pin GPIO header
  • One LED, any color
  • One resistor, 220 to 330 ohms
  • A breadboard and two jumper wires (female-to-male if connecting directly to the header)

Step by step

  1. Shut down the Pi and disconnect power before touching the GPIO header; hot-wiring circuits risks shorts.
  2. Identify GPIO17, physical pin 11, using a pinout reference such as pinout.xyz. BCM numbering names pins by their GPIO number, not physical position.
  3. Wire the circuit: jumper from pin 11 to one leg of the resistor, the other resistor leg to the LED's long lead (anode), and the LED's short lead (cathode) back to any GND pin such as physical pin 9.
  4. Double-check polarity: current flows anode to cathode, and the resistor must be in series or the LED burns out.
  5. Power the Pi back up and install the friendly GPIO library if missing: sudo apt install python3-gpiozero.
  6. Create a file blink.py with three lines: import LED from gpiozero, create led = LED(17), then loop toggling with while True: led.toggle(); sleep(0.5) after importing sleep from time.
  7. Run it with python3 blink.py; the LED blinks twice per second. Press Ctrl+C to stop.
  8. Experiment: replace the loop with led.blink() for a background thread version, or read gpiozero button inputs next to build interactive circuits.

Because the loop is pure Python, this works identically whether you sit at a monitor or SSH in remotely, which is how most real projects run.

Common mistakes

  • Confusing BOARD and BCM pin numbering; gpiozero uses BCM numbers like 17, not physical pin 11.
  • Omitting the current-limiting resistor and killing the LED instantly.
  • Wiring the LED backwards; it simply will not light until flipped.
  • Drawing too much current: never drive motors or relay coils straight from a GPIO pin, use a transistor.

Related guides

Run the script remotely using [[raspberry-pi-headless-setup]]. Once blinking feels easy, automate a camera capture with [[raspberry-pi-camera-module-setup]] triggered by motion, or back up your project code with [[raspberry-pi-backup-restore-sd-card]].