The Raspberry header is the key to its ability to interface with the real world. The Pi either uses a 40-pin or 26-pin depending on the model and it is important to understand how those pins are arranged and labelled.
The GPIO header provides the following power and interface options :
- 3.3V (on 2 pins)
- 5V (on 2 pins)
- Ground (on 8 pins)
- General purpose input and output
- PWM (pulse width modulation)
- I2C
- I2S
- SPI
- Serial
These allow a massive range of sensors, motors, LEDs and accessories to be connected to the Pi.
Raspberry Pi 40-pin GPIO Header
The majority of Raspberry Pi models use the same 40-pin header :
If you are reading the Raspberry Pi copyright statement printed on the PCB then Pin 1 is in the bottom left and Pin 40 is in the top right.
The models that use this configuration are :
- Pi 4 Model B
- Pi 3 Model B+
- Pi 3 Model B
- Pi 3 Model A+
- Pi 2 Model B
- Pi Model B+
- Pi Model A+
- Pi Zero
- Pi Zero W
Pi 1 Model A/B (Revision 2.0)
Revision 2.0 saw some slight changes to Pin 3, Pin 5 and Pin 11. These changes carried over to the new 40-pin design so anything that worked on a Revision 2.0 model should work on the newer models.
Pi 1 Model B (Revision 1.0)
This is where it all began. The original 26-pin GPIO header :
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 :
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.
The white pins were previously “DNC” (Do Not Connect) as they were reserved for future use. It was eventually confirmed that their function would not be changed so the diagrams above showed their permanent assignment.
To find out what board revisions you have take a look at my Guide to finding out your PCB revision number.
Pin 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.
GPIO Header 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. Power management has been improved with each iteration of hardware.
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.
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 :
23 Comments
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.
Ah thanks for the clarification. It all seems much clearer now.
I’ve made some improvements to my diagram so it should be easier to follow now!
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 🙂
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.
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 🙂
Pingback: Raspberry PI with Erlang | Matthew (Phil)yaw
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
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.
…nearly there! You’ve now got both GPIO2 and GPIO3 labelled as SDA1 I2C 😉
Arrrggghhh. I’m sure that changed behind my back! Thanks for the feedback. Should be correct now.
Is there a difference between the black and white grounds or the red and white 5 volt rails?
No. The white pins were originally specified as “do not connect” but the Pi Foundation later stated they weren’t going to change so their use became fixed.
Hello,
Great article and diagrams. I am looking to set the TX GPIO14 as a regular GPIO input pin. For some background, I am using the Wolfson audio card which takes up most of the gpio pins. It has a header that passes the serial pins (GPIO14&15). The only other device I have to connect to the Pi is an Adafruit Ultimate GPS. I do not need to talk to the GPS so I only need the Rx (GPIO15) on the Pi. I do, however need to hook up a PPS timing input signal from the GPS. I think i might have struck a bit of luck here that the Wolfson audio card passed the two serial pins and I only need one (prevents me from having to solder and hack the pins up) if I can just repurpose the TX (GPIO14) as a GPIO input.
My question is, do you see a problem with my approach here and can I just follow your example Python script to reconfigure the pin?
Thanks for your help and your time
Adam
All the GPIO pins can be configured as inputs or outputs so you should be fine.
Thanks Matt,
I went ahead with this plan and it is working fine!
Adam
Pingback: What is the difference between BOARD and BCM for GPIO pin numbering? | DL-UAT
Hi! I was wondering if anyone else is having an issue getting their GPIOs to communicate? I wrote a small snippet of code, really simple that controls the GPIO on and off. I have been simply using a multimeter to measure the change in voltage. This code normally works, but after I enabled and have been playing with my I2C and SPI pins (which all work excellently) I am no longer able to communicate with the GPIO. I even tried re-blacklisting I2C and SPI to see if that could help fix the problem, but to no avail.
It really depends on what code you are using and what library you are using to access the GPIO pins. It’s probably best to post on the Official Raspberry Pi Forums as it will easier to work through the options.
this tutorial is simple amazing!!!
this saved my day 🙂
“P1” does not mean Pin 1 — its the connector designation: as you can see connectors “P2” and “P3” also exist on the board. Pin 1 is indicated by a squared pad (instead of a circle) or a white button-like marking, sometimes a white dot (as on S5).
You are right but on the original Pi the P1 label was next to Pin 1 so was a good way to explain to people which end to look at. It isn’t the case on the newer models so I will revise the text at some point.
Aw, this was a really nice post. Finding the time and actual effort
to produce a really good article… but what can I say… I
hesitate a whole lot and don’t seem to gget nearly anything
done.