Simple Guide to the RPi GPIO Header and Pins

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)

Raspberry Pi GPIO Layout - Revision 1

Raspberry Pi GPIO Layout – Revision 1

Model A/B (Revision 2.0)

Raspberry Pi GPIO Layout - Revision 2

Raspberry Pi GPIO Layout – Revision 2

Model B+

Raspberry Pi GPIO Layout - Model B+

Raspberry Pi GPIO Layout – Model B+

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.

Power 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.

 Basic GPIO

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

Pin Protection

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.

Circuits

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 :

Controlling an LED using a GPIO pin

I will post links to other circuits as I test them.

Alternative Diagram

Here is an alternative header diagram for the Revision 1.0 board created by scottchiefbaker on the Raspberry Pi Forums :

Scottchiefbaker's GPIO Header Diagram

This entry was posted in Hardware, Python, Tutorials & Help and tagged , , , , , . Bookmark the permalink.



11 Responses to Simple Guide to the RPi GPIO Header and Pins

  1. Mike Cook says:

    This is not true:-
    However only 8 of these are considered GPIO pins out of the box. The other 9 can perform more complex roles but I’m only interested in the basics.

    All the pins except GPIO 14 & 15 come up initialised as inputs. If you want to use them as inputs there is nothing further you have to do to them.

    GPIO 14 & 15 come up initialised as serial data lines TX & RX so you can connect a serial terminal to allow you to log on.

  2. Matt says:

    I’ve made some improvements to my diagram so it should be easier to follow now!

  3. Simon Walters says:

    Out of all the info on the net – I found yours was the closest that I found I needed to getting this working.

    If you try this on the current default RaspberryPi Debian download then the code doesn’t work as there is a permission restriction that prevents the standard user from using GPIO (It works up to the point when you issue GPIO.setup(11, GPIO.IN) and then bombs :( )

    I don’t know if this is just peculiar to that distro but since its likely to be the first one that people use it would be great if you could add that in order to use the code that sometimes you have to run Python using sudo python first.

    I think your article would be nigh on perfect then :)

    • Matt says:

      As you’ve noticed Python scripts that use the GPIO library need to be run as sudo. I’ve updated the article to mention it.

  4. Kris says:

    Nice diagrams. I started from the docs of RPi.GPIO library but those where abit sparse about how to map these numbers to the actual board pins. I also have a book ‘Raspberry Pi Projects for the Evil Genius’ which has some diagrams and tables. But yours are the best / clearest by far. Especially pins 3 and 5 which are different between revision 1 and 2. Those had me stumped before your diagrams made it clear to me why my program wasn’t driving those pins… the diagrams I had been looking at are for Rev 1 and my Pi is Rev 2. The ‘Evil Genius’ book completely missed pointing that out and only has Rev 1 diagrams.

    Thanks for sharing your diagrams with us :-)

  5. Pingback: Raspberry PI with Erlang | Matthew (Phil)yaw

  6. AndrewS says:

    Slight error/inconsistency in your diagrams above:
    For Rev 1.0, GPIO1 ought to be labelled as I2C0_SCL

    For Rev 2.0, GPIO2 ought to be labelled I2C1_SDA and GPIO3 ought to be labelled as I2C1_SCL

    • Matt says:

      Thanks for noticing. They were a bit messy. I’ve made them all consistent but gone for a slightly different label to align them slightly more with the style used on the B+ schematics.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>