Skip to main content

GPIO Header

Every board on Boards has the same 40-pin header: two rows of 20 pins along one edge. A CM4's is on its IO board. Most of the pins are the processor's input and output lines, which the pages in this group use. The rest are power and ground.

A plain Zero 2 W has only the holes, with no pins in them. Solder a header in, or buy the Zero 2 WH, which comes with one.

Finding the pins​

On a 3B+, a Pi 4 and a Zero 2 W, pin 1 is at the end of the header nearest the SD card, in the row away from the board's edge. The pins count from there: odd numbers along that inner row, even numbers along the edge, so pin 2 sits beside pin 1. On any other board, check the picture below against the board itself.

A pin's number and its GPIO number are different

Every pin has two numbers, and they don't match, because they come from different places. The GPIO number comes from the processor, which numbers its own input and output lines. The pin number comes from the Raspberry Pi board: it's where that line ends up on the header, counting from pin 1. Your code uses the GPIO number.

So 17 in your program means GPIO 17, which is pin 11. Pin 17 is 3.3 V power. Count from pin 1 before you connect anything: a wire on the wrong pin can put 5 volts on a pin that can't take it, and that can destroy the board.

SD card end13.3 V25 V3GPIO 2I²C SDA45 V5GPIO 3I²C SCL6Ground7GPIO 48GPIO 14UART TX9Ground10GPIO 15UART RX11GPIO 1712GPIO 18PWM013GPIO 2714Ground15GPIO 2216GPIO 23173.3 V18GPIO 2419GPIO 10SPI MOSI20Ground21GPIO 9SPI MISO22GPIO 2523GPIO 11SPI SCLK24GPIO 8SPI CE025Ground26GPIO 7SPI CE127GPIO 0HAT ID28GPIO 1HAT ID29GPIO 530Ground31GPIO 632GPIO 12PWM033GPIO 13PWM134Ground35GPIO 19PWM136GPIO 1637GPIO 2638GPIO 2039Ground40GPIO 21
  • Free to use
  • Free until PWM uses it
  • Refused: it belongs to a bus or the board
  • Power
  • Ground

Not every pin is yours to use. gpioOutput and gpioInput refuse the pins that I²C, SPI and UART use, and GPIO 0 and 1, which the board uses to identify a HAT. Thirteen pins are always free: GPIO 4, 5, 6, 16, 17, 20, 21, 22, 23, 24, 25, 26 and 27. GPIO 12, 13, 18 and 19 are free too, until PWM uses them. With a Build HAT on, GPIO 4 and 22 are its reset and boot pins.

Power​

  • 3.3 V, at pins 1 and 17, powers a small sensor or chip that works at 3.3 volts. It can't supply much, so a motor never goes here.
  • 5 V, at pins 2 and 4, comes from the board's power supply. It powers a part that needs 5 volts, such as a small servo, as PWM shows.

Power pins are on while the board has power. The 5 V pins stay on even after it shuts down, so unplug the board before you rewire. Nothing in your program switches them.

Ground​

The header has eight ground pins: 6, 9, 14, 20, 25, 30, 34 and 39. When a part needs ground, any ground pin will do, so pick the one nearest the part's other wires.

A part with a power supply of its own still needs its ground joined to one of the Pi's, or the two sides can't agree on what a signal means.

Voltages​

  • The GPIO pins work at 3.3 volts. An output pin drives 3.3 volts for high and 0 for low, and an input reads a voltage near 3.3 as high and near 0 as low.
  • Never put 5 volts on a GPIO pin. The pins can't take it, and it can permanently damage the board's processor. A part that signals at 5 volts needs a level shifter between it and the pin.
  • The pins supply very little current: keep each pin to 16 milliamps at most, and all of them together to 50. That lights an LED through a resistor, but a motor needs a driver board, such as the LEGO Build HAT.