The 40 Pin Header

All recent Raspberry Pis (including Zero models) have a 40 pin header for connecting external components. Using 2.54mm jumper wires (aka dupont wires) many project can be implemented without soldering.

The table below describes the functions of the 40 pins for model 3B+. Hold the Pi so that the pins are on the right side and pointing toward you. Older models may differ in minor ways - see end of this page.

Description BCM Pin Pin BCM Description
3.3VCC 01 02 5VCC
I2C1 SDA 02 03 04 5VCC
I2C1 SCL 03 05 06 GND
GPCLK0 04 07 08 14 UART TX
GND 09 10 15 UART RX
SPI1 CS1 17 11 12 18 SPI1 CS0 / PWM / I2S BCLK
27 13 14 GND
22 15 16 23
3.3VCC 17 18 24
SPI0 MOSI 10 19 20 GND
SPI0 MISO 09 21 22 25
SPI0 CLK 11 23 24 08 SPI0 CS0
GND 25 26 07 SPI0 CS1
I2C0 SDA 00 27 28 01 I2C0 SCL
GPCLK1 05 29 30 GND
GPCLK2 06 31 32 12 PWM
PWM 13 33 34 GND
SPI1 MISO / PWM/ I2S LRCLK 19 35 36 16 SPI1 CS2
26 37 38 20 SPI1 MOSI / I2S DIN
GND 39 40 21 SPI1 CLK / I2S DOUT

A lot of confusion is caused by having two numbering schemes for the pins:

We recommend using the BOARD scheme exclusively as it is more systematic and makes it easy to locate the desired pins by counting.

All pins except for VCC and GND can be used for general purpose input/output (GPIO) but some pins have additional capabilities as shown in the table. Among those the higher level interfaces SPI, I2C and I2S are the most useful and are described in more details in their own section, including information about how to activate the interfaces - just follow the links.

All pins are digital. The Pi does not have built-in analog-to-digital converters (ADCs) or digital-to-analog converters (DACs).

Permissions

Access to the GPIO pins is usually only permitted for root (aka superuser). However, access can be granted to other users via Linux groups. Run the groups command to determine which groups the current user belongs to. If the output is similar to the example below and shows gpio, i2c and spi membership you are all set.

4 groups
pi adm dialout cdrom sudo audio video plugdev games users input netdev gpio i2c spi

Otherwise use the addgroup command to add group membership one at a time.

$ sudo addgroup pi i2c

Command Line Access

A handy command line tool for exploring the gpio pins is gpio1.

The following command show the current configuration of the various pins:

$ gpio readall

Other useful commands are:

# configure pin 20 (BCM numbering) for output
$ gpio -g mode 20 out
# set pin 20 (BCM numbering) to 1 (high)
$ gpio -g write 20  1  

Python Access

In Python you get access to the pins using the GPIO library2

import RPi.GPIO as GPIO

To select between BOARD or BCM numbering use either

GPIO.setmode(GPIO.BOARD)

or

GPIO.setmode(GPIO.BCM)

Note: You should call setmode only once in the entire program which can be an issue if you program is spread over multiple files.

GPIO.setup(20, GPIO.OUT)     # configure pin 20 as output
GPIO.output(20, GPIO.HIGH)   # set pin 20 to high

Differences between Models

Model Difference
TODO TODO

  1. sudo apt install wiringpi↩︎

  2. sudo apt install python3-rpi.gpio↩︎