Facebook Twitter Instagram Pinterest YouTube
    Trending
    • Pi Pico W Launched
    • Add Kodi to RetroPie Menu
    • Disable Auto-login in Raspberry Pi OS
    • Raspberry Pi Cloud Storage with MEGA
    • RetroPie Temperature Monitor from Menu
    • Pi Pico Pinout and Power Pins
    • Install Arduino IDE on Raspberry Pi
    • Raspberry Pi 400 SSD Upgrade
    Facebook Twitter Instagram Pinterest YouTube RSS
    Raspberry Pi SpyRaspberry Pi Spy
    • Home
    • Categories
      • General
      • Hardware
      • Programming
      • Python
      • Software
      • Tutorials & Help
    • BerryClip
      • BerryClip Instructions
      • BerryClip Plus Instructions
      • Videos & Reviews
    • Buy
      • Buy Pi
      • Buy Pi Accessories
      • Buy Books
    • Tools
      • Ultimate Raspberry Pi Alexa Skill
      • Pi Power Estimator App
      • Pi-Lite 14×9 LED Matrix Sprite Editor
      • RPiREF Pin-out Reference App
      • Simple Ohm’s Law Calculator
      • Web Sites & Links
    • Tutorials & Help
        Featured
        November 9, 20200

        Raspberry Pi Temperature Monitoring

        Recent
        May 6, 2022

        Add Kodi to RetroPie Menu

        February 26, 2022

        Disable Auto-login in Raspberry Pi OS

        February 2, 2022

        Raspberry Pi Cloud Storage with MEGA

      1. Contact Us
      2. Site Map
      Raspberry Pi SpyRaspberry Pi Spy
      You are at:Home»Hardware»Simple Guide to the Raspberry Pi GPIO Header
      Raspberry Pi GPIO Header with Photo

      Simple Guide to the Raspberry Pi GPIO Header

      23
      By Matt on June 9, 2012 Hardware, Tutorials & Help

      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 :

      Raspberry Pi 40-pin GPIO Layout
      Raspberry Pi 40-pin GPIO Layout

      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.

      Raspberry Pi 40-pin GPIO Header alignment

      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.

      Raspberry Pi GPIO Layout - Revision 2
      a Raspberry Pi GPIO Layout – Pi 1 Model A/B Revision 2

      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 :

      Raspberry Pi GPIO Layout - Revision 1
      Raspberry Pi GPIO Layout – Pi 1 Model B Revision 1

      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 :

      Controlling an LED using a GPIO pin

      Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
      Previous ArticleMy Raspberry Pi LEGO Case
      Next Article Control LED Using GPIO Output Pin

      Related Posts

      Pi Pico W Launched

      Add Kodi to RetroPie Menu

      Disable Auto-login in Raspberry Pi OS

      23 Comments

      1. Mike Cook on June 14, 2012 4:31 pm

        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.

        Reply
        • Matt on June 14, 2012 11:15 pm

          Ah thanks for the clarification. It all seems much clearer now.

          Reply
      2. Matt on June 14, 2012 11:14 pm

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

        Reply
      3. Simon Walters on July 8, 2012 7:01 pm

        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 🙂

        Reply
        • Matt on July 8, 2012 7:46 pm

          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.

          Reply
      4. Kris on May 23, 2014 1:52 am

        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 🙂

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

      6. AndrewS on August 1, 2014 3:10 pm

        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

        Reply
        • Matt on August 1, 2014 7:45 pm

          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.

          Reply
          • AndrewS on August 4, 2014 4:56 pm

            …nearly there! You’ve now got both GPIO2 and GPIO3 labelled as SDA1 I2C 😉

            Reply
            • Matt on August 4, 2014 7:42 pm

              Arrrggghhh. I’m sure that changed behind my back! Thanks for the feedback. Should be correct now.

              Reply
      7. Christopher Wilkes on September 13, 2014 4:24 am

        Is there a difference between the black and white grounds or the red and white 5 volt rails?

        Reply
        • Matt on September 13, 2014 9:42 am

          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.

          Reply
      8. Adam on November 11, 2014 9:48 pm

        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

        Reply
        • Matt on November 12, 2014 4:17 pm

          All the GPIO pins can be configured as inputs or outputs so you should be fine.

          Reply
          • Adam on November 22, 2014 8:27 pm

            Thanks Matt,

            I went ahead with this plan and it is working fine!

            Adam

            Reply
      9. Pingback: What is the difference between BOARD and BCM for GPIO pin numbering? | DL-UAT

      10. Tyler Maydew on April 14, 2015 3:04 pm

        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.

        Reply
        • Matt on April 15, 2015 7:04 pm

          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.

          Reply
      11. giorgi on October 30, 2015 6:19 pm

        this tutorial is simple amazing!!!
        this saved my day 🙂

        Reply
      12. Sméagol on August 7, 2016 2:57 pm

        “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).

        Reply
        • Matt on August 17, 2016 8:26 pm

          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.

          Reply
      13. DNC Shoes on February 24, 2020 10:17 pm

        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.

        Reply

      Leave A Reply Cancel Reply

      This site uses Akismet to reduce spam. Learn how your comment data is processed.

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      February 2, 2022

      Raspberry Pi Cloud Storage with MEGA

      January 7, 2022

      RetroPie Temperature Monitor from Menu

      Categories
      • 1-wire
      • 3D Printing
      • Add-ons
      • BBC Micro:bit
      • BerryClip
      • Books
      • Camera Module
      • Cases
      • Events
      • General
      • Hardware
      • I2C
      • Infographics
      • Interfaces
      • Minecraft
      • Model A+
      • Model B+
      • News
      • Pi Models
      • Pi Pico
      • Pi Zero
      • Power
      • Programming
      • Python
      • Raspberry Pi OS
      • Raspbian
      • RetroGaming
      • Robotics
      • Sensors
      • Software
      • SPI
      • Tutorials & Help
      Tags
      3D Printing Arduino audio battery berryclip Birthday bluetooth cambridge camera CamJam DigiMakers display games GPIO I2C interface Kickstarter LCD LED Linux media Minecraft Model A Model B motionEyeOS PCB photography photos Pi-Lite portable power python Raspberry Jam Raspberry Pi Bootcamp raspbian Retrogaming retroPie screen SD card security sensor SPI temperature ultrasonic video
      Raspberry PI Related
      • Adafruit Blog
      • Average Maker
      • Official RaspBerry Pi Site
      • Raspberry Pi Pod
      • RasPi.tv
      • RaspTut
      • Stuff About Code
      Tech Resources
      • MattsBits – Pi Resources
      • Microbit Spy
      • Technology Spy
      Archives
      About

      Unofficial site devoted to the Raspberry Pi credit card sized computer offering tutorials, guides, resources,scripts and downloads. We hope to help everyone get the most out of their Pi by providing clear, simple articles on configuring, programming and operating it.

      Popular Posts
      September 19, 2014

      Top 5 Reasons The Raspberry Pi Sucks

      July 27, 2012

      16×2 LCD Module Control Using Python

      October 20, 2013

      Analogue Sensors On The Raspberry Pi Using An MCP3008

      Recent Posts
      June 30, 2022

      Pi Pico W Launched

      May 6, 2022

      Add Kodi to RetroPie Menu

      February 26, 2022

      Disable Auto-login in Raspberry Pi OS

      Facebook Twitter Instagram Pinterest YouTube RSS

      Entries RSS | Comments RSS

      This site is not associated with the official Raspberrypi.org site or the Raspberry Pi Foundation. Raspberry Pi is a trademark of the Raspberry Pi Foundation.

      Copyright © 2022 - All Rights Reserved - Matt Hawkins

      Type above and press Enter to search. Press Esc to cancel.