In this Raspberry Pi GPIO tutorial, we are going to take a look at all the basics of the GPIO pins or also known as the general purpose input and output pins.
These pins are to communicate with other circuitry such as extension boards, custom circuits, and much more.
You can make some pretty cool stuff by using these pins. We will look into a lot more projects in the future that make use of these pins.
There is quite a bit to know about these pins, and I highly recommend that you do follow this guide to get a better understanding before you start playing around with them.
Also, be sure to check out our video on the Raspberry Pi GPIO under the equipment list. It will take you through all the basics you need to know to get started.
Warning: Experimenting with the GPIO is risky and has a chance of bricking your Raspberry Pi. Knowing the power limitations of the GPIO pins and also understanding what you’re plugging in is a must.
The equipment that I use in this Raspberry Pi GPIO tutorial is listed below.
A Raspberry Pi Case with access to the GPIO pins
Our video will take you through all the topics explained in this guide. If you learn better by listening and seeing, then I highly recommend that you check out the video.
If you do like the video, then please subscribe to us or follow us on social media, so you stay up to date with all the coolest Raspberry Pi projects, guides and much more.
Can’t see the video? Support us by disabling your adblocker.
Raspberry Pi GPIO Tutorial
Now let’s get started on this Raspberry Pi GPIO tutorial, we will be taking a look at the different types of pins that available, how to enable the modules, what a breakout kit is and finally how to build and program a simple circuit.
If you’re an owner of a Raspberry Pi B+, 2, Zero or 3, then you will have 40 pins in total. The earlier models, such as the Raspberry Pi B have just 26 pins.
See below for the Raspberry Pi GPIO pinout diagram. We have included all three iterations of the Pi for the pinout diagram.
If you want a PDF version of the pinout diagram, you can find it for download here.
- GPIO is your standard pins that can be used to turn devices on and off. For example, a LED.
- I2C (Inter-Integrated Circuit) pins allow you to connect and talk to hardware modules that support this protocol (I2C Protocol). This protocol will typically take up two pins.
- SPI (Serial Peripheral Interface Bus) pins can be used to connect and talk to SPI devices. Pretty much the same as I2C but makes use of a different protocol.
- UART (Universal asynchronous receiver/transmitter) is the serial pins used to communicate with other devices.
- DNC stands for do not connect, this is pretty self-explanatory.
- The power pins pull power directly from the Raspberry Pi.
- GNDare the pins you use to ground your devices. It doesn’t matter which pin you use as they are all connected to the same line.
All this might be daunting at first, but it’s pretty easy once you get going. A lot of the abbreviations and technical jargon is easy to turn people off straight away.
Configuring and using the Pins
In this section, we will briefly touch on how to set up the pins so that you can use them on the Raspberry Pi. We will go lots more into actually programming and using the pins in future Raspberry Pi projects.
In this example and in future projects, we will be using Raspbian. If you haven’t got it installed, then you can find out how to install the latest version by following my Raspbian installation guide.
If you’re on the latest version Raspbian, then you can start programming and using the GPIO pins without needing to do any extra configuration.
However, I recommend that you update your Pi to the latest packages anyway. If you haven’t done this, then you can do it by running the following commands.
sudo apt-get update sudo apt-get upgrade
If you don’t have the GPIO package installed you simply install it by running the following command.
sudo apt-get install rpi.gpio
Configuring Raspberry Pi I2C
Setting up the I2C pins on the Raspberry Pi is super easy and will only take a couple of minutes to do.
Firstly, go to the Raspi-Config tool by entering the following command.
In here go to advanced options and then to I2c, enable I2c by pressing yes.
The Pi should now alert you that I2C will be enabled after reboot. It will then ask if you want it to be loaded by default. Select yes if you plan on using I2c every time the Raspberry Pi boots up.
Now we want to make sure it has successfully enabled the necessary modules. To do this, enter the following command.
lsmod | grep i2c_
This command will return any modules that are running, starting with i2c. It should return something like this i2c_BCM2708.
Configuring Raspberry Pi SPI
Configuring the Raspberry Pi SPI is much like the I2c and is super easy, it will only take a couple of minutes to do.
Firstly, go to the Raspi-Config tool by entering the following command.
Once the config tool has loaded, go to advanced options, and then to SPI.
Enable SPI, and it will tell you that it will be enabled after reboot. After that, it will ask if you want it to be loaded by default. Only select yes if you plan on using it every time the Pi boots up.
To check that SPI is successfully up and running use the following command.
lsmod | grep spi_
This command will return any modules that are running, starting with SPI. It should return something like spi_bcm2835. Make sure you have restarted your Pi before checking to see if the module has been loaded.
Using a Breakout Kit
A breakout kit allows you to take all the pins via a ribbon cable and connect them to a breadboard or a different device. This kit makes it a lot easier and safer than working in and around the Raspberry Pi.
There are a few different types of breakout kits that you’re able to buy for the Raspberry Pi GPIO pins.
Personally, I prefer the T type as they are easy to read and use. If you’re looking for a breakout kit to buy, then be sure to check out the equipment list above.
When connecting the ribbon cable, you need to make sure it is connected, so it is facing away from the board. You can see an example of a correct setup ribbon cable with a T type breakout board right below.
Programming with the Raspberry Pi GPIO Pins
Programming with the GPIO pins on the Raspberry Pi is typically done using the programming language Python. This particular circuit and code is super easy to get going and won’t take you long at all to complete.
If coding sounds like it may be a little too involved for you and would prefer a simple solution, then something like Raspberry Pi Cayenne might interest you. The software package allows you to add sensors, control GPIO pins, and lots more with a super easy user interface.
GPIO BOARD vs GPIO BCM
There is a massive difference between these two different modes, and it’s up to you which one you want to take advantage of for your project. Both have their own pros and cons.
GPIO board (GPIO.Board) references the physical numbering of the pins. For example, the top left pin is 1, and the top right pin 2. It continues to count upwards as you go from top to bottom until you run out of pins. In our diagram, you can see this numbering in the middle of each group.
GPIO BCM (GPIO.BCM) is the Broadcom Soc Channel numbering. In the diagram above, you can find the number after GPIO. For example on the Raspberry Pi 3, the pin below 3v3 is GPIO2, so the number for this pin is 2 in BCM mode.
A downside to BCM is they have changed between versions (Raspberry Pi Rev 1 vs Raspberry Pi Rev 2) and made do in the future. If you want to be safe, I recommend sticking to GPIO Board but as I said earlier, it’s up to you how you want to reference the pins.
The Code for a Simple LED Circuit
If you’re happy to code and learn lots about Python, then check out my example below.
Firstly, let’s set up our little circuit. I have a helpful easy diagram to follow below. If you have a breakout kit, the circuit will obviously be a bit different since your wires will come from the cobbler.
Alternatively, simply connect the positive end of a LED up to pin 7 (GPIO4) and the negative end to a resistor that connects to a ground pin.
Now let’s create a Python file so that we can type out our Python script.
sudo nano led_blink.py
Let’s write out a little script. It’s important to remember Python is whitespace sensitive.
For example, in the for loop, make sure you have four spaces for anything within the loop. To explain a little further, the GPIO.output(7, true) line will need to have four spaces before it.
#import the GPIO and time package import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) # loop through 50 times, on/off for 1 second for i in range(50): GPIO.output(7,True) time.sleep(1) GPIO.output(7,False) time.sleep(1) GPIO.cleanup()
Now exit and save by pressing ctrl+xand then y.
If you run our little script, the circuit should come to life.
You can run a Python script by doing the following command. It is also important that you run it as the superuser using sudo.
sudo python led_blink.py
I hope that this tutorial has helped introduce you to the basics of the GPIO pins.
As I mentioned earlier in this guide, I will be doing a lot of projects that use the Raspberry Pi GPIO pins in the future. This range of projects includes using motions sensors, temperature gauges, controlling motors, and much more.
If you have any questions, feedback or if I missed anything, then feel free to drop us a comment below.