Everything You Need to Know About Raspberry Pi GPIO Pins
The Raspberry Pi is a cheap and tiny computer capable of a huge array of tasks, including retro gaming and being a home media center. The Pi also has a heavy focus on education, with both Scratch and Minecraft Pi edition geared towards helping young people learn to code, and the GPIO pins (General Purpose Input/Output) open up a whole world of DIY electronic tinkering and invention.
What Are Raspberry Pi GPIO Pins?
In this article, we will tell you everything you need to know about the Pi’s GPIO pins: what they can do, how to use them, and mistakes to avoid while using them.
A note before we begin: Different revisions of the Pi can vary with their pins! Before attaching anything to your board, make sure you are using the correct ones. A quick way to check is to type pinout into your Raspberry Pi’s terminal, which will bring up a diagram of your current setup.
The GPIO pins are integrated into the circuit board of the computer. Their behavior can be controlled by the user to allow them to read data from sensors, and control components like LEDs, motors, and displays. Older models of the Pi had 26 GPIO pins, while the newer models all have 40. This chart shows what each pin does:
In the labelled diagram above, you can see that there are different types of GPIO pins which serve different purposes. You can find an interactive version of this chart at pinout.xyz It also outlines one of the first confusing things you will have to contend with. Each pin has two numbers attached to it. Its BOARD number (the numbers in the circle) and its BCM (Broadcom SOC channel) number. You can choose which convention to use when you write your Python code:
# 1 - GPIO/BCM Numbering GPIO.setmode(GPIO.BCM) # 2 - Board Numbering GPIO.setmode(GPIO.BOARD)
You can only use one convention in each project, so choose one and stick to it. Neither convention is “right”, so go with whichever one makes most sense to you. It is worth noting however, that certain peripherals rely on GPIO/BCM numbering.
For this article, we will stick to BOARD numbering. So what do the pins actually do?
Let’s start with the power pins. The Raspberry Pi can provide both 5v (pins 2 and 4) and 3.3v (pins 1 and 17) power. It also provides a ground (GND) for circuits on pins 6, 9, 14, 20, 25, 30, 34, and 39.
Unfortunately, there is no single answer to how much current the 5v power pins can draw as it is reliant on what power supply you are using, and what other components you have attached to your Pi. The Raspberry Pi 3 will only draw 2.5A from its power supply, and requires around 750mA for boot up and normal headless operation. This means that if you are using a 2.5A power supply, the 5v pins can supply a total current of around 1.7A maximum. Annoyingly, this varies between models of Pi however, as this table shows:
For most users just starting out with the Pi, this will not be a problem, but it is something to bear in mind as you spend more time with the GPIO pins.
The 3.3v pins are somewhat simpler, with recent Raspberry Pi revisions (Model B+ onwards) providing up to 500mA total, and older models providing just 50mA. Note that this current is shared throughout all of the other GPIO pins too!
So these pins can provide power to your components, but that is all they do. The real fun stuff comes from the rest of the pins.
On the chart above, ignoring the power pins, you’ll see that some are marked in different colors. The green pins are standard GPIO pins, and these are what you will use for most beginner projects. These pins are capable of a 3.3v output, also referred to as setting the pin HIGH in code. When an output pin is LOW this means that it is simply providing 0v.
They are also capable of taking an input of up to 3.3v, which the pin reads as HIGH.
Do not provide the pins with greater than 3.3v: this is a quick way to fry your Pi!
For a great guide to getting started using the GPIO pins in a simple project, try out our Getting Started with Raspberry Pi GPIO project.
While we will cover some of the pins with special uses in this article, you can use any pins except the power pins, and pins 27 and 28 as regular GPIO pins.
PWM (Pulse Width Modulation) is used with components such as motors, servos and LEDs by sending short pulses to control how much power they recieve. We used it with an Arduino in our Ultimate Guide to LED Strips tutorial.
PWM is also possible on the Pi. Pin 12 (GPIO 18) and pin 35 (GPIO 35) are hardware PWM capable, though the Pi is also able to provide software PWM through libraries such as pigpio.
For an introduction to the code required for PWM, this simple LED brightness tutorial should help get you going.
Pins 8 and 10 (GPIO 14 and 15) are UART pins, designed for communicating with the Pi using the serial port. There are certain situations where you may want to do this, but for most beginners connecting to your Pi headlessly via SSH or using a VNC will probably be easier.
If you are interested in a detailed view of how the serial pins work, this is a great primer.
SPI (Serial Peripheral Interface bus) is a method of communicating with devices like the RFID reader we used in our DIY Smart Lock with Arduino and RFID project.
It allows devices to communicate with the Raspberry Pi synchronously, meaning much more data can pass between the master and slave devices. If you have ever used a small touch screen for your Pi, this is how they communicated.
There are various devices and extension HATs for the Raspberry Pi which use SPI, and it can open up your projects to much more hardware than the regular GPIO pins can sustain. It does however, require quite a lot of wiring to get it working. There is an in depth overview of SPI on the Raspberry Pi foundation website.
Pins 19, 21, 23, 24, 25 and 26 (GPIO 10, 9, 11, 8, GND, and GPIO 26) are used to connect to an SPI device, and they are all required for smooth operation. A good way to avoid all the spaghetti is to buy a premade extension such as the Sense HAT, which fits on top of your board and provides it with an LED matrix and a wide array on sensors. It’s been a favourite for several years now, and was even used on the International Space Station to do some experiments!
The SPI protocol is not enabled as standard on Raspbian, but it can be enabled in the raspi-config file, along with I2C.
I2C (Inter-Integrated Circuit) is similar to SPI, but is generally considered to be easier to set up and to use. It communicates asynchronously, and is capable of sustaining as many different devices as needed provided they each have unique address places on the I2C bus. Due to this addressing system, the Pi only needs two I2C pins—pin 3 (GPIO 2) and pin 5 (GPIO 3), making it much simpler to use than SPI.
The small footprint of I2C opens up a huge range of possibilities. With standard GPIO pins, setting up an LCD screen and some buttons would take up almost every pin, using an I2C device such as the Adafruit Negative LCD controller brings it down to just two pins!
Sparkfun have a full rundown of SPI and I2C along with examples to get you started.
Pins 27 and 28 (marked ID_SD and ID_SC) are also I2C. There are used by the Pi for internal functions, and also some HAT boards. As a general rule, don’t mess with them unless you really know what you are doing!
Raspberry Pi: A GPIO Pin for Everything!
The Raspberry Pi is the Swiss Army Knife of modern computing. Along with a huge amount of awesome day to day uses, it also opens anyone up to the possibility of making their own cool creations.
Many Raspberry Pi beginner projects use the protocols discussed in this article, and a hands on approach is the best way to learn. Keep tinkering, and have fun!
- A picture says a thousand words. Which is why i...
- So you forgot your Facebook login and password ...
- Maybe, you are in a regular job right now but d...
- Netflix is raising its prices again, and this t...
- Netflix is raising its prices again, and this t...
- Amazon’s Kindle is the best ebook reader ...
- A photo of an egg has become the most-liked Ins...
- 3D mouse technology lets you control computers ...
- While we went to CES last year, this was the fi...
- You could argue that one of the best features o...