Thuta Learning
IntermediateHardwarebeginner

Controlling GPIO with Python

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Understand controlling GPIO with Python, no intimidation required
  • Get comfortable running your own hardware wiring and code
  • Apply this concept right away in a real project

Let's think about it this way for a moment

gpiozero is a beginner-friendly library for controlling the Raspberry Pi's GPIO from Python (its syntax is simpler than RPi.GPIO) — it comes preinstalled on Pi boards. Import the LED class, pass it a pin number (BCM), and you get an LED object — just call methods like .on(), .off(), and .blink() to control the physical LED.

Let's connect this to a real-world scenario

Import with from gpiozero import LED, then led = LED(17) declares BCM pin 17 as GPIO 17 — call led.on() and the LED lights up, led.off() and it turns off, and led.blink() and it blinks continuously (you can adjust the speed with the interval parameter).

Let's look at an example together

python
from gpiozero import LED
from time import sleep

led = LED(17)  # BCM pin 17

# Simple on/off
led.on()
sleep(1)
led.off()

# Built-in blink (0.5s on, 0.5s off, repeating)
led.blink()

# Keep the script running so blink() continues
from signal import pause
pause()
You should see
You'll see the physical LED turn on for 1 second, turn off, and then blink continuously.

5-Minute Try-It

If your LED is already wired up (from the previous lesson), write your own gpiozero script and run through on/off/blink.

A quick word of caution

When you stop a script with Ctrl+C, the GPIO state (if the LED was on) can remain stuck — gpiozero handles cleanup automatically, but if you're using the RPi.GPIO library, you'll need to call GPIO.cleanup() manually.

Easy traps

  • Writing the physical pin number instead of the BCM number in code, so the LED doesn't work
  • Not calling signal.pause() (or adding a loop), so the script exits immediately and you never see the blink() effect

Try It Yourself

If your LED is already wired up (from the previous lesson), write your own gpiozero script and run through on/off/blink.

You'll know it worked when: You'll see the physical LED turn on for 1 second, turn off, and then blink continuously.