Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Raspberry Pi 40-Pin GPIO: Hardware Add-Ons Guide

· 6 min read · Home Tech

In this article
  1. Background: The 40-Pin Header
  2. How the Pins Work
  3. Types of Hardware Add-Ons
  4. How to Choose an Add-On
  5. Common Problems and Mistakes
  6. Costs and Safety
  7. Alternatives to the GPIO Header
  8. Frequently Asked Questions

The 40-pin GPIO header on a Raspberry Pi is the main way to connect hardware add-ons, from simple LEDs and buttons to HATs, sensors, and motor controllers. This guide explains what the pins do, how to choose compatible add-ons, and common pitfalls to avoid.

Background: The 40-Pin Header

All modern Raspberry Pi models (from the Pi 2 Model B onward, including the Pi 3, Pi 4, Pi Zero series, and Pi 5) share the same 40-pin GPIO (General Purpose Input/Output) header. The original 26-pin header on the Pi 1 Model B and A is now obsolete for new designs. The header provides a mix of power pins, ground pins, and general-purpose I/O pins that can be programmed for digital input/output, PWM, I2C, SPI, UART, and more.

The pinout is standardized, but the physical layout differs: the Pi Zero and Pi Zero 2 have unpopulated through-hole pads, while other models have a 2x20 male header. The pin numbering follows two conventions: physical pin numbers (1-40) and Broadcom GPIO numbers (e.g., GPIO17). Software like the RPi.GPIO library uses the Broadcom numbering, while the gpio command-line tool can use either.

How the Pins Work

Each GPIO pin can be configured as an input or output. When set as an output, it can drive a logic high (3.3V) or low (0V) signal. When set as an input, it reads the voltage level. Most pins also have alternate functions, such as:

  • I2C: Pins 3 (GPIO2) and 5 (GPIO3) for SDA and SCL.
  • SPI: Pins 19, 21, 23, 24, 26 for MOSI, MISO, SCLK, CE0, CE1.
  • UART: Pins 8 (TX) and 10 (RX).
  • PWM: Hardware PWM on pins 12, 32, 33, 35 (GPIO18, 12, 13, 19).

All GPIO pins operate at 3.3V logic levels. They are not 5V tolerant, so connecting a 5V signal directly can damage the Pi. The 5V pins (2 and 4) are for powering add-ons, not for logic signals. The 3.3V pins (1 and 17) provide a regulated 3.3V supply, but the total current draw from these pins should not exceed about 50mA (check the specific model's documentation).

Types of Hardware Add-Ons

Add-ons fall into several categories:

  • HATs (Hardware Attached on Top): These are boards that follow a specific standard, including an EEPROM that identifies the board to the Pi. They stack directly onto the header and are designed to fit the Pi's form factor. Examples include audio DACs, motor drivers, and relay boards.
  • Breakout boards: These expose the GPIO pins in a more convenient layout, often with additional connectors for sensors or servos.
  • Individual components: LEDs, resistors, buttons, sensors (temperature, humidity, motion), and actuators can be connected directly via jumper wires.
  • Expansion boards: These add specific functionality, such as analog-to-digital converters (ADCs), since the Pi's GPIO pins are digital only.

When choosing an add-on, check its voltage requirements. Many sensors are 3.3V compatible, but some (like certain ultrasonic distance sensors) require 5V power and output 5V logic, which needs a level shifter.

How to Choose an Add-On

Consider these factors:

  1. Compatibility: Does it support your Pi model? Most HATs work with the 40-pin header, but some older ones may only be for 26-pin models.
  2. Power requirements: Add-ons that draw more than a few hundred milliamps should have their own power supply, not rely on the Pi's 5V pins.
  3. Software support: Check if the manufacturer provides a Python library or if it works with standard Linux drivers.
  4. Physical fit: HATs are designed to fit the Pi's mounting holes, but some may obstruct the camera or display connectors.
  5. Level shifting: If the add-on uses 5V logic, you'll need a level shifter board to avoid damaging the Pi.

For beginners, a starter kit with a breadboard, jumper wires, LEDs, resistors, and a few sensors is a good way to learn. For specific projects, search for add-ons that match your needs and read reviews from other users.

Common Problems and Mistakes

Here are frequent issues and how to avoid them:

  • Damaging the GPIO pins: Bending or breaking pins is common when inserting or removing HATs. Always align the header carefully and apply even pressure.
  • Using 5V logic without a level shifter: This can permanently damage the Pi's GPIO controller. Use a level shifter or choose 3.3V-compatible components.
  • Overloading the 3.3V rail: Drawing too much current from the 3.3V pin can cause the Pi to reboot or crash. Check the current draw of your add-ons.
  • Incorrect wiring: Double-check pin connections against the pinout diagram. A common mistake is swapping SDA and SCL on I2C devices.
  • Software conflicts: Some add-ons require disabling the default UART or I2C settings in config.txt. Follow the manufacturer's instructions.

If an add-on isn't working, first verify power and ground connections, then check the software configuration, and finally test with a multimeter if you have one.

Costs and Safety

Add-on prices vary widely: a basic LED and resistor cost less than a dollar, while a full HAT with an EEPROM can cost $20-$50 or more. Starter kits with multiple components are often the most cost-effective way to begin.

Safety is important: never connect the GPIO pins directly to mains voltage or to high-current circuits without proper isolation. Always use a resistor in series with LEDs to limit current (typically 330Ω for 3.3V). When working with motors or relays, use a separate power supply and a driver board to protect the Pi.

If you're unsure about a connection, power off the Pi before making changes. Hot-swapping components can cause voltage spikes that may damage the board.

Alternatives to the GPIO Header

If you need more I/O than the 40-pin header provides, you can:

  • Use an I2C multiplexer to connect multiple devices with the same address.
  • Add an ADC (like the MCP3008) to read analog sensors.
  • Use a USB interface for devices like Arduino or a USB-to-GPIO adapter.
  • Consider a microcontroller (e.g., Arduino, Pico) for real-time or analog tasks, and connect it to the Pi via USB or I2C.

These options expand your capabilities without overloading the Pi's GPIO.

Frequently Asked Questions

What is the difference between physical pin numbers and GPIO numbers?

Physical pin numbers are the actual positions on the header (1-40). GPIO numbers are the Broadcom chip's internal numbering (e.g., GPIO17). Software libraries like RPi.GPIO can use either, but you must be consistent. The pinout command on Raspberry Pi OS shows both.

Can I use 5V sensors with the GPIO?

You can power a 5V sensor from the 5V pin, but the sensor's output signal will be 5V, which is too high for the Pi's 3.3V GPIO input. Use a level shifter or a sensor with a 3.3V logic level. Some sensors have a VCC pin that can be connected to 3.3V, but check the datasheet.

How many add-ons can I connect at once?

There is no fixed limit, but each add-on consumes pins and power. I2C allows up to 127 devices on the same bus (with different addresses), but practical limits are lower due to capacitance and address conflicts. For power, the Pi's 5V pin can supply about 1A (depending on the model and power supply), but it's safer to power high-current add-ons separately.

Do all Raspberry Pi models have the same 40-pin header?

Yes, all models with a 40-pin header use the same pinout. The Pi Zero and Pi Zero 2 have the same header but as unpopulated pads, so you need to solder a header or use a GPIO hammer header. The Pi 5 has the same 40-pin header, but some pins have different capabilities (e.g., three UARTs, more PWM channels).

What is a HAT and do I need one?

A HAT (Hardware Attached on Top) is a board that follows the Raspberry Pi HAT specification, including an EEPROM that the Pi reads to identify the board and automatically configure the pins. HATs are convenient because they stack neatly and are often plug-and-play. You don't need a HAT; you can use breadboards and jumper wires, but HATs are more reliable for complex projects.