GPIO pins on Raspberry Pi are a main feature you need to know.
As I’m a Linux administrator, I mainly used the Raspberry Pi for testing systems and software in the first months. But to go further and create your own electronic systems and programs, you need to learn how to use them.
GPIO pins allow adding extensions to your Raspberry Pi, whether with HATs or to create circuits. The easiest way to use them is to create Python scripts, but Scratch or other software are good options to consider.
In this beginner guide, I’ll really start with the basics, for people who never tried GPIO pins. If you already made some tests, you can use the content table below to get directly to the paragraph you want to read.
You’ll need a few items to complete this tutorial. If you don’t have anything yet, please check this great kit from Sunfounder. This might be a bit too much for this tutorial, but it’s worth it on the long run, as you won’t need anything else for your first few projects.
Let’s start with the basics about GPIO.
What are GPIO Pins?
GPIO stands for General Purpose Input/Output.
It’s the 40 pins you can see on the Raspberry Pi, near the edge:
The goal of the GPIO Pins is to add some extensions to your Raspberry Pi.
For example, most of the Raspberry Pi HATs use these pins to connect with the Raspberry Pi (you can find here my Raspberry Pi HATs recommendations if you want to try one).
You can also create your electronic circuit by using these GPIO pins with cables, LED, and other accessories. We’ll see that later.
As you may have guessed, each pin has a specific role, and you can use it only for that role.
Some of them are input/output, power (3.3V or 5V), or ground.
There are even more complex things we’ll see later.
So, it’s important to know what is what.
Here is an illustration:
The website pinout.xyz is useful for this.
It gives you the exact layout and role of each pin.
If you don’t have it with your breadboard or GPIO kit, I recommend printing this image to use it later.
Raspberry Pi Configuration
Before starting with the GPIO pins practice, we need to do a few steps on the Raspberry Pi to make sure that everything is ready:
- Start by updating your system
sudo apt update sudo apt upgrade
- Install the rpi.gpio package
sudo apt install rpi.gpio
- Enable I2C and SPI in raspi-config
You don’t need them in this tutorial, so you can skip this step.
But if you go further after this one it may save you time, as nobody explains this 🙂
Go into “Interface Options”
And enable I2C and SPI in each submenu.
I2C and SPI pins are specific GPIO pins.
You may need them with some hardware modules that need them (a screen like this one for example).
Here is the recommended hardware you need to have to follow the rest of this tutorial (Amazon links):
- A Raspberry Pi, I’m using the 3B+ for this post, but any model will be ok.
- A breadboard kit will be perfect, with everything you need inside. This way you’ll not ask dozens of questions to know which component you really need (not easy when you just start with GPIO circuits).
- Or at least:
- A breadboard
- Some male/female jumper wires, to plug the breadboard to the GPIO pins
- And also male/male jumper wires can be useful too (on the breadboard)
- LEDs and resistors (you can take this pack if you already have breadboard and wires)
- I don’t have one yet, but this kind of GPIO Expansion board is an excellent option to know which pin to use
These components will be useful for many projects later, it’s a long list of things you need to get started, but you’ll use them every time after that 🙂
That’s it, once you have everything ready, you can move to the next part.
Let’s start with the breadboard.
If it’s your first time with it, you may have a hard time understanding how it works.
If you are using the breadboard kit I recommended above, the first step is to install it in the blue plastic case.
Generally, you have to stick it in the large space and screw the Raspberry Pi to the corresponding location.
At this point your setup must look like this:
Don’t plug in the Raspberry Pi power cable for the moment.
Before going further, you need to understand how a breadboard works.
Each hole is a pin you can use to plug something.
On the edges of the board, there are two lines:
- The red line is for the power input
- The blue line is for the ground
Each port is connected with all the pins from the same line.
Attention, with some breadboards (like mine), there is a separation in the middle, you can see a gap in the red line for example.
If you plug near port 0, it will not work near port 50.
The other ports are used for everything else (LEDs, resistors, other modules).
A wire connects them in columns.
If you take one number on the board, it connects each port from the same column with the others.
It will be clearer with this picture:
I have squared each connected ports.
The red square corresponds to a power input line.
There are four lines of this type on the board.
If you input power in one of the squared ports, you can use it from any other highlighted ports.
For the ground ports it’s the same thing (blue square).
And for the other ports, you can see the green square how they are connected together.
It’s the same for each column, for both side of the middle line.
If needed, here is a complete schema:
Your First Circuit: Blink the LED!
Ok, that’s the end of the theory part, and the beginning of your first circuit tutorial.
Let’s practice 🙂
As always, start to plug the pins without a power source plugged.
To get started you need:
- 1x LED
- 2x male/female jumper wires
- 1x resistor
Take the two jumper wires and plug them like this:
- One from the ground line to a ground pin of the Raspberry Pi (for example, the third one from the second row, port 6)
- The other from an input/output port (for example, the fourth one of the first row, port 7)
You now have two cables, connected to only one side.
On the other side, you need to connect them to the breadboard.
Plug the ground jumper wire to the ground line of the breadboard (any port).
Plug the other cable into one column of the breadboard (anywhere).
It should look like this:
Finally, put the resistor between the ground line and the column near the other cable.
And put a light with one foot on each column, the shorter in the resistor/ground column.
Something like this:
Yes, I know my resistor seems to be having a hard time 🙂
You can now boot the Raspberry Pi and jump into SSH (or GUI if you prefer) to create the Python script.
As soon as the Raspberry Pi is on, avoid touching the circuit.
The first basic script we can code is to turn on the LED light to check that everything works.
To do this, Raspberry Pi OS already includes any libraries you need.
- In a terminal, or with your favorite code editor, create a new python file:
- Paste these lines inside:
import RPi.GPIO as GPIO
#GPIO Basic initialization
#Use a variable for the Pin to use
#If you followed my pictures, it's port 7 => BCM 4
led = 4
#Initialize your pin
#Turn on the LED
print "LED on"
#Turn off the LED
print "LED off"
I commented on everything so it should be clear.
The only trap is the pin number to use in the LED variable.
You may have a different numeration in your breadboard expansion board or sticker, but you have to use the BCM number from pinout.xyz.
- Save and Exit (CTRL+X)
- Run the script with:
The LED will turn on for 5 seconds and then turn off.
If not, double-check every previous paragraph to see what you have missed.
If your circuit seems good, check the LED direction, there is a + and – side.
That’s it, you now have learned the basics about GPIO pins and breadboard.
You’re able to build basic circuits and you’re ready for the next level 🙂
If you like this kind of tutorial, stay tuned because I’ll talk more about it in the weeks or months to come.