Blog

Raspberry Pi GPIO: controlling hardware from Python

Alessandro Panait·

GPIO stands for General Purpose Input/Output. These are the pins along the edge of the Raspberry Pi that let it interact with the physical world: light an LED, spin a motor through a driver, read a button.

Reading and writing pins

On modern Raspberry Pi OS, the recommended library is gpiozero:

from gpiozero import LED, Button
led = LED(17)
button = Button(2)
led.on()
if button.is_pressed:
    led.off()

Simple: create an object for a pin, then turn it on or read it.

The rule that saves your Pi

Raspberry Pi GPIO runs at 3.3V, not 5V. This matters:

  • Never feed 5V into a GPIO input pin. A 5V signal from an Arduino or a sensor can permanently damage the Pi. Use a level shifter, or connect boards over USB instead.
  • The Pi cannot drive a motor directly. GPIO pins deliver tiny currents. To move a motor you need a motor driver (an H-bridge) powered separately.

Common uses in a robot

  • Send simple on/off commands to a motor driver.
  • Read a start button.
  • Switch lights via a relay.
  • Send a few bits to another board as a signal.

Good habit: common ground

If the Pi controls another board, connect their grounds together. Without a shared ground, digital signals become unreliable even when everything looks wired correctly. It is the number one cause of "it works sometimes" bugs.

Comments (0)