This page aims to give a general introduction to the General Purpose Input Output pins on the Raspberry Pi (Model B). The GPIO pins are available on the PCB via a header and allow you to interface the Pi to the real world.
I was mainly interested in the standard GPIO pins for reading switches and controlling LEDs and Motors. I found a great deal of confusion surrounding the numbering and naming of these pins so decided to create my own diagram. The idea was to create a page that I could print out and stick in my box of wires and cables.
Reading information from a webpage is useful but when you are up to your neck in soldering irons, wires and tools it is sometimes easier to have a piece of paper to hand. The other advantage is that you can scribble notes on it.
So here is the Pi with its 26 way header in the top left of the board :
It consists of two rows of thirteen pins. Pin 1 is clearly marked on the board as “P1″. It is vital you are looking at the header the correct way round. Locate “P1″ and compare the header to the diagram below :
Model B (Revision 1.0)
Model A/B (Revision 2.0)
The pin-out of the header on the Model B+ is described in more detail on my Model B+ GPIO page.
To find out what board revisions you have take a look at my Guide to finding out your PCB revision number.
The white pins were previously “DNC” (Do Not Connect) as they were reserved for future use. It has now been confirmed their function will not be changed so the diagrams above show their permanent assignment.
The Pin marked “P1″ is Pin 1 and provides 3.3V (50mA max). You can work out all the other pins from there. Note how the numbering works. Even numbers on the top row and odd numbers on the bottom row.
Labels – Clear as Mud
The labels above are the names of the pins on the Broadcom system chip to which the pin is physically connected. Much of the confusion around the GPIO is due to these labels, their relationship to the Broadcom labels and how they are referred to in your programs. To confuse things even more the GPIO pins are sometimes renamed with another set of numbers. In order to avoid damaging your Pi you need to be sure what pins you are connecting to other hardware and that your program is referring to the correct pins.
The header provides 5V on Pin 2 and 3.3V on Pin 1. The 3.3V supply is limited to 50mA. The 5V supply draws current directly from your microUSB supply so can use whatever is left over after the board has taken its share. A 1A power supply could supply up to 300mA once the board has drawn 700mA.
The header provides 17 Pins that can be configured as inputs and outputs. By default they are all configured as inputs except GPIO 14 & 15.
In order to use these pins you must tell the system whether they are inputs or outputs. This can be achieved a number of ways and it depends on how you intend to control them. I intend on using Python.
GPIO in Python
The easiest way to control the GPIO pins is using the RPi.GPIO Python library. Installing the library is easy if you follow my RPi.GPIO Installation Guide. Once installed using the pins is as easy as :
import RPi.GPIO as GPIO # Use GPIO numbers not pin numbers GPIO.setmode(GPIO.BCM) # set up the GPIO channels - one input and one output GPIO.setup(7, GPIO.IN) GPIO.setup(8, GPIO.OUT) # input from GPIO7 input_value = GPIO.input(7) # output to GPIO8 GPIO.output(8, True)
In this example we use GPIO7 (pin 26) and GPIO8 (pin 24). Python scripts that use the GPIO library must be run using sudo. i.e.
sudo python yourscript.py
Most of the pins in the header go directly to the Broadcom chip. It is important to carefully design the components you attach to them as there is a risk you will permanently damage your Pi. Short circuits and wiring mistakes could also ruin your day so double check everything. A multimeter is probably going to help a lot here as you can double check wiring before you connect to the Pi.
Luckily there are some basic circuits that you can use to protect the pins and the cost of implementing them is minimal. Here are some links to circuits I have built :
I will post links to other circuits as I test them.
Here is an alternative header diagram for the Revision 1.0 board created by scottchiefbaker on the Raspberry Pi Forums :