Learn / Raspberry Pi / GPIO and sensors

Raspberry Pi GPIO with Python

ReviewedBeginnerFreeAbout 6 min
Pi 4
In short

Raspberry Pi GPIO with Python: the short answer

3 V and ground) and GPIO pins. 3 V and are not 5 V tolerant: connecting a 5 V sensor output straight to a GPIO pin can destroy the chip. Example: 330 Ω gives a clearly visible LED at about 4 mA, which is gentle on the GPIO pin.

Level
Beginner
Time
About 6 min
Topic
Raspberry Pi
You will learn to

Blink an LED and read a button from Python with gpiozero, without damaging the Pi's 3.3 V pins.

Before this: Digital Inputs and Outputs, Setting Up a Raspberry Pi

The 40-pin header on every modern Raspberry Pi carries power pins (5 V, 3.3 V and ground) and GPIO pins. GPIO pins work at 3.3 V and are not 5 V tolerant: connecting a 5 V sensor output straight to a GPIO pin can destroy the chip. Each pin can only supply a few milliamps safely (keep it under about 16 mA, and much less in total), so drive LEDs through a resistor and drive motors or relays through a transistor or driver board.

Pins have two numbering schemes. Physical numbers count the header from 1 to 40. GPIO (or BCM) numbers are the chip's names, and Python libraries use these: physical pin 11 is GPIO17. Print a pinout card or run the pinout command on the Pi to check before you wire anything.

Use the gpiozero library, which comes with Raspberry Pi OS and works on every model including the Pi 5. The older RPi.GPIO library does not work on the Pi 5, because its GPIO now lives on a separate chip called RP1. In gpiozero, LED(17) is an output and Button(27) is an input with the internal pull-up already switched on, so the button just connects the pin to ground.

What a GPIO pin can and cannot do

A pin is either an input or an output, set in software, and can also switch to a special function such as I²C, SPI, UART or PWM. At 3.3 V the pin reads high above about 2 V and low below about 0.8 V. An output can source or sink only a few milliamps safely, so it is for signals, not power.

Never connect a pin to 5 V, and be careful with modules that have a 5 V output: a 5 V sensor wired to the Pi can damage the pin or the board. Use a level shifter, or a divider for a slow signal, or choose a 3.3 V module.

Reading a button properly

A button needs a defined level when it is not pressed. gpiozero's Button turns on an internal pull-up by default, so the pin reads high when open and low when pressed, which is why you wire the other side to ground. Use button.when_pressed = function to react without a busy loop, and gpiozero's debouncing handles the bounce.

A floating input picks up noise and gives random presses. If you find a pin changing by itself, it is probably floating.

PWM, servos and timing on Linux

gpiozero's PWMLED and Servo create PWM in software on most pins, and it can wobble when the Pi is busy because Linux is not a real-time system. For a steady servo or a bright, flicker-free LED, use a hardware PWM pin or a small servo driver board such as the PCA9685, or leave timing to a microcontroller.

A time.sleep() delay is only approximate, and the loop can overshoot by milliseconds. For measurements that need tight spacing, record the time with each reading instead of trusting the delay.

Good habits that protect the board

Switch the Pi off before changing wiring. Check the pinout with the pinout command and with a printed card before connecting power. Add a resistor in series with each LED, and a transistor or a driver board for any load that takes more than a few milliamps. Wire everything first, double-check, then power on.

Common mistakes
  • Connecting a 5 V sensor output to a GPIO pin.
  • Mixing up physical pin numbers and GPIO numbers.
  • Powering a motor or relay coil from a GPIO pin.
Worked example

Pick the LED resistor for a 3.3 V GPIO pin

  1. A red LED drops about 2.0 V, so the resistor sees 3.3 − 2.0 = 1.3 V.
  2. Aim for about 4 mA to stay well inside the pin's limit: R = 1.3 ÷ 0.004 = 325 Ω.
  3. The nearest standard value is 330 Ω.
  4. Check: 1.3 ÷ 330 ≈ 3.9 mA.

Result. 330 Ω gives a clearly visible LED at about 4 mA, which is gentle on the GPIO pin.

Try it

Wire an LED with a 330 Ω resistor from GPIO17 to ground and a push button from GPIO27 to ground. Write a script with from gpiozero import LED, Button; led = LED(17); btn = Button(27); btn.when_pressed = led.toggle; and keep it running with from signal import pause; pause().

Check yourself

3 quick questions

  1. A sensor outputs 5 V logic. What do you do before connecting it to a Pi GPIO pin?

  2. Physical pin 11 is GPIO17. Which number does gpiozero use?

  3. Why does an old RPi.GPIO script fail on a Pi 5?

Common questions

What will I learn in "Raspberry Pi GPIO with Python"?

Blink an LED and read a button from Python with gpiozero, without damaging the Pi's 3.3 V pins.

How do I pick the LED resistor for a 3.3 V GPIO pin?

A red LED drops about 2.0 V, so the resistor sees 3.3 − 2.0 = 1.3 V. Aim for about 4 mA to stay well inside the pin's limit: R = 1.3 ÷ 0.004 = 325 Ω. The nearest standard value is 330 Ω. Check: 1.3 ÷ 330 ≈ 3.9 mA. 330 Ω gives a clearly visible LED at about 4 mA, which is gentle on the GPIO pin.

A sensor outputs 5 V logic. What do you do before connecting it to a Pi GPIO pin?

Use a level shifter or a voltage divider down to 3.3 V. Pi GPIO pins are 3.3 V only; 5 V can damage the processor.

Physical pin 11 is GPIO17. Which number does gpiozero use?

17. gpiozero uses GPIO (BCM) numbers by default.

Why does an old RPi.GPIO script fail on a Pi 5?

Its GPIO is on the RP1 chip, which RPi.GPIO does not support. Use gpiozero or lgpio, which support the Pi 5.