---
agent:
  instructions: "Use this search API to find relevant content across this site (products with prices and stock, collections, guides, articles, courses, help and pages) with keywords or natural-language queries, instead of crawling individual pages. Replace {query} with your URL-encoded query. Results are returned in Markdown."
  endpoint: "https://piaustralia.com.au/.well-known/search?q={query}"
  method: "GET"
  documentation: "https://developers.cloudflare.com/ai-search/how-to/search-for-agents/"
---

# Controlling GPIO from Python with gpiozero

Light an LED, read a button, fade with PWM and react to a motion sensor in a few lines of Python, and why gpiozero works on a Raspberry Pi 5 when older libraries don't.

## What it is

gpiozero is a Python library that lets you treat the parts on your breadboard as objects: an `LED`, a `Button`, a `MotionSensor`. You tell it which pin a part is on and call simple commands like `on()` or `wait_for_press()`. It handles the low-level pin setup for you.

## Installing it

It's already installed on Raspberry Pi OS, both the desktop and Lite versions. If it's missing on another system:

```
sudo apt update
sudo apt install python3-gpiozero
```

If you work inside a Python virtual environment, create it with `--system-site-packages` so it can see the gpiozero and lgpio that came with the OS:

```
python3 -m venv my-project --system-site-packages
source my-project/bin/activate
```

## Pin numbers

gpiozero uses **GPIO (BCM) numbers**, not physical pin numbers. `LED(17)` means GPIO17, which is physical pin 11. If you'd rather count physical pins, pass a string such as `"BOARD11"`. Our [GPIO header guide](/docs/gpio-pinout) has the full table.

## Blink an LED

Wire an LED's long leg through a resistor (around 330 Ω) to GPIO17, and its short leg to a ground pin. Save this as `blink.py`:

```python
from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(0.5)
    led.off()
    sleep(0.5)
```

The same thing in two lines: `led.blink()` followed by `pause()` (from Python's `signal` module).

## Read a button

Connect one side of a push button to GPIO27 and the other to a ground pin. No resistor is needed, because `Button` switches on the Pi's internal pull-up by default. (If you wire the button to 3.3 V instead, use `Button(27, pull_up=False)`.)

```python
from gpiozero import LED, Button
from signal import pause

led = LED(17)
button = Button(27)

button.when_pressed = led.on
button.when_released = led.off

pause()
```

Note that you assign `led.on`, without brackets. You're handing gpiozero the function to call later, not calling it now. If a cheap button triggers more than once per press, add `bounce_time=0.05` to `Button`.

## Fade with PWM

`PWMLED` dims an LED by switching it on and off very quickly. Set its `value` anywhere from 0 (off) to 1 (full brightness):

```python
from gpiozero import PWMLED
from signal import pause
from time import sleep

led = PWMLED(17)

led.value = 0.25   # quarter brightness
sleep(2)
led.value = 1      # full brightness
sleep(2)
led.pulse()        # now fade in and out on its own

pause()
```

## A sensor: motion detection

A PIR motion sensor has three pins: power, ground and a signal output. Connect power to the 5 V or 3.3 V pin its maker specifies, ground to a ground pin, and the output to GPIO4. Check first that the output signal is 3.3 V (the PIR sensor we stock is); anything that sends 5 V back to a GPIO pin can damage the Pi.

```python
from gpiozero import MotionSensor, LED
from signal import pause

pir = MotionSensor(4)
led = LED(17)

pir.when_motion = led.on
pir.when_no_motion = led.off

pause()
```

gpiozero has ready-made classes for many other parts: `DistanceSensor` for HC-SR04 ultrasonic sensors, `LightSensor`, `Buzzer`, `Servo`, `Motor` and `RGBLED` among them. An HC-SR04's echo pin sends back 5 V, so it needs a voltage divider (two resistors) before it reaches the Pi. The 3.3 V version, the HC-SR04P, doesn't.

## Running your script

From a terminal:

```
python3 blink.py
```

Press **Ctrl+C** to stop it. On the desktop you can open the file in Thonny and press Run instead.

When a script reaches its last line it ends, and gpiozero releases the pins, which can switch your LED straight back off. That's why the event-driven examples above finish with `pause()`: it keeps the script alive, waiting for button presses or motion.

## Why it works on a Raspberry Pi 5

On a Pi 5 the GPIO pins don't belong to the main processor any more. They're run by a separate chip, the RP1, which handles most of the board's input and output.

Older libraries such as **RPi.GPIO** worked by writing straight into the main processor's GPIO registers. On a Pi 5 those registers aren't where the pins are, so RPi.GPIO stops with an error. The pigpio library has the same problem.

gpiozero doesn't touch the hardware itself. It hands the work to a "pin factory", and its default, **lgpio**, goes through Linux's standard GPIO driver, which works on every Raspberry Pi model including the Pi 5. Of the pin libraries gpiozero supports, lgpio is the only one that works on a Pi 5, so leave it as the default.

If you have an older project written for RPi.GPIO, the cleanest fix is to rewrite it with gpiozero; it's usually shorter afterwards. Alternatively, a compatibility package called **rpi-lgpio** provides the RPi.GPIO commands on top of lgpio. It replaces RPi.GPIO rather than sitting alongside it, so the two can't be installed in the same Python environment.

## Parts in this guide

- [Simple Electronics with GPIO Zero, 2nd edition](https://piaustralia.com.au/products/simple-electronics-with-gpio-zero-2nd-edition.md)
- [Basic Starter Kit for Raspberry Pi](https://piaustralia.com.au/products/basic-starter-kit-for-raspberry-pi.md)
- [PIR Motion Sensor](https://piaustralia.com.au/products/pir-motion-sensor.md)
- [5mm LED Mixed Pack (50 pcs)](https://piaustralia.com.au/products/5mm-led-mixed-pack-50-pcs.md)
- [Assembled Pi T-Cobbler Plus - GPIO Breakout for Raspberry Pi](https://piaustralia.com.au/products/assembled-pi-t-cobbler-plus-gpio-breakout-for-raspberry-pi.md)
- [Breadboard](https://piaustralia.com.au/products/breadboard.md)

Written by Pi Australia from GPIO Zero documentation, GPIO Zero basic recipes, GPIO Zero input devices reference, GPIO Zero pins and pin factories reference, Installing GPIO Zero.
