Skip to content

Explainer / Raspberry Pi

Introduction to Raspberry Pi GPIO

The GPIO (General Purpose Input/Output) header is your gateway to physical computing with Raspberry Pi. Learn about pin types, how they're numbered, and how to start using them safely.

BeginnerGPIO · HardwareBy Vilros

What is GPIO?

GPIO stands for General Purpose Input/Output. The GPIO header on your Raspberry Pi is the double row of pins along one edge of the board. These pins let your Pi interact with the physical world by connecting to electronic components like LEDs, buttons, sensors, motors, and more.

With GPIO, you can:

  • Output signals: Turn LEDs on and off, control motors, trigger relays.
  • Read inputs: Detect button presses, read sensor data, monitor switches.
  • Communicate with devices: Use protocols like I²C, SPI, and UART to talk to displays, sensors, and other electronics.

The modern Raspberry Pi (models 1 B+, 2, 3, 4, 5, Zero, and 400) has a 40-pin GPIO header. Earlier models (original Pi 1 A and B) had 26 pins, but the pinout for those first 26 pins remains the same across all models for compatibility.

Types of GPIO pins

Not all pins on the GPIO header are the same. The 40-pin header includes several types of pins:

Power pins

  • 5V power (pins 2 and 4): Provide 5 volts directly from the Pi's power supply. Useful for powering 5V devices. Current draw is limited by your power supply.
  • 3.3V power (pins 1 and 17): Provide 3.3 volts, with a maximum total current of about 50mA across all 3.3V outputs. Use this for low-power 3.3V devices.
  • Ground (GND) pins: Pins 6, 9, 14, 20, 25, 30, 34, and 39 are all ground. Ground completes the electrical circuit for your components.

GPIO pins (general-purpose)

Most of the remaining pins are general-purpose GPIO pins. These can be configured in software as either inputs or outputs. They operate at 3.3V logic levels—this is very important for safety.

  • When set as an output, the pin can be set HIGH (3.3V) or LOW (0V) to control devices.
  • When set as an input, the pin can read HIGH or LOW signals from sensors, buttons, or other devices.
  • Most GPIO pins can source or sink up to about 16mA of current. Do not draw more than this without using a transistor or other driver circuit.

Special-function pins

Many GPIO pins have alternate functions for communication protocols:

  • I²C (pins 3 and 5): GPIO 2 (SDA) and GPIO 3 (SCL) for the I²C bus. Used for communicating with sensors, displays, and other I²C devices. These pins have built-in pull-up resistors.
  • SPI (pins 19, 21, 23, 24, 26): Used for high-speed serial communication with displays, SD cards, and other SPI devices.
  • UART (pins 8 and 10): GPIO 14 (TX) and GPIO 15 (RX) for serial communication. Used for GPS modules, Bluetooth modules, and serial debugging.
  • PWM (various pins): Pulse Width Modulation for controlling motor speed, LED brightness, and servo positions. Hardware PWM is available on GPIO 12, 13, 18, and 19.

You can use these pins as regular GPIO if you're not using their special functions, but be aware that some software (like I²C or SPI drivers) may claim them automatically.

Pin numbering

There are two ways to refer to GPIO pins, and this can be confusing at first:

Physical pin numbering

Physical pin numbers (also called board numbering) count the pins from 1 to 40 based on their physical position on the header. Pin 1 is the pin closest to the SD card slot (the 3.3V pin), and pin 40 is at the opposite end (GPIO 21).

BCM (GPIO) numbering

BCM numbering (Broadcom chip numbering) uses the GPIO number assigned by the Broadcom processor. For example, physical pin 11 is GPIO 17 in BCM numbering.

Which system you use depends on your software library:

  • Many tutorials and examples use BCM numbering.
  • Some libraries default to physical pin numbering.
  • Always check your library's documentation to know which system it uses.
Find the pinout

To see a complete pinout diagram on your Pi, open Terminal and type pinout. This displays a visual diagram with both physical pin numbers and BCM GPIO numbers, plus special functions.

Safety precautions

The GPIO pins are connected directly to the Raspberry Pi's processor. Misusing them can damage your Pi permanently. Follow these safety rules:

  • Never connect 5V signals to GPIO pins. GPIO pins operate at 3.3V. Connecting a 5V signal (from an Arduino, for example) directly to a GPIO pin can destroy the pin or the entire Pi. Use a level shifter for 5V devices.
  • Do not short power pins to ground. Shorting 5V or 3.3V pins to ground can damage the Pi's power circuitry.
  • Limit current draw. Each GPIO pin can handle about 16mA. Use a transistor or relay to control high-current devices like motors or bright LEDs.
  • Disconnect power before wiring. Always shut down your Pi and disconnect power before adding or removing wires from the GPIO header.
  • Watch polarity. LEDs, motors, and some sensors have polarity. Connecting them backward may damage the component or the Pi.
  • Be careful with GPIO 3 (pin 5). This pin is used for I²C and has a special function: pulling it to ground can wake a sleeping Pi. Avoid using it for buttons or switches unless you understand this behavior.

Getting started with GPIO

Ready to try GPIO? Here are some beginner-friendly starting points:

Software libraries

To control GPIO pins from Python (the most common language for Pi GPIO projects), use one of these libraries:

  • gpiozero — Beginner-friendly library with simple, high-level commands. Pre-installed on Raspberry Pi OS. Recommended for beginners.
  • RPi.GPIO — Classic GPIO library with more low-level control. Pre-installed on Raspberry Pi OS.

Example: Blink an LED connected to GPIO 17 (physical pin 11) using gpiozero:

from gpiozero import LED
from time import sleep

led = LED(17)

while True:
    led.on()
    sleep(1)
    led.off()
    sleep(1)

Beginner projects

  • Blink an LED: The "Hello, World!" of hardware. Connect an LED with a 220Ω resistor to a GPIO pin and ground, then control it from Python.
  • Read a button: Connect a button between a GPIO pin and ground, then detect button presses in your code.
  • Control a servo: Use PWM to control the position of a servo motor.
  • Read a sensor: Use I²C or SPI to read data from a temperature sensor, distance sensor, or other device.

Resources

Need more help?

GPIO is a vast topic, and there's much more to explore beyond this introduction. If you're working on a specific project or running into issues, the Raspberry Pi community forums and project tutorials are great resources.

Have a GPIO question?

Tell us what you're trying to build, which pins you're using, and what's not working as expected.

Contact Vilros Support ↗