Getting started with GPIO Pins on Raspberry Pi

gpio basics on raspberry pi

GPIO Pins on Raspberry Pi are a big feature you need to know
As I’m a Linux administrator, I mainly used it 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

What is GPIO Pins and how to use them?
GPIO pins allow us to add extensions to your Raspberry Pi, whether with HATs or with your circuits
The easiest way to use them is to create Python scripts, but you can also use Scratch or other software

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

GPIO Basics

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

raspberry pi gpio

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 also create your electronic circuit by using these GPIO pins with cables, LED and other accessories. We’ll see that later

GPIO Pinout

As you may have guessed, each pin has a specific role and you can use it only for this
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:

raspberry pi pinout

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 🙂
    sudo raspi-config

    Go into “Interfacing 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)

Required hardware

Here is the recommend hardware you need to have to follow the end of this tutorial:

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

Breadboard basics

Let’s start with the breadboard
If it’s your first time with it, you may have a hard time to understand how it works

Breadboard installation

If you took the breadboard kit I recommended before, 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:

raspberry pi breadboard installation

Don’t plug the Raspberry Pi power cable for the moment

Breadboard schema

Before going further, you need to understand how a breadboard works
Each hole is a pin you can use to plug something

Power input

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 the port 0, it will not work near the port 50

Other ports

The other ports are usable 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

Schema

It will be clearer with this picture

breadboard schema

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:

breadboard 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 🙂

Breadboard configuration

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)

gpio pins

You now have two cables, connected 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 to one column of the breadboard (anywhere)

It should look like this

breadboard wires

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:

breadboard led 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

Python script

The first basic script we can code is to light on the LED to check that everything works
To do this, Raspbian already includes any libraries you need

  • In a terminal, or with your favorite code editor, create a new python file:
    nano led_on.py
  • Paste these lines inside:
    #import libraries
    import RPi.GPIO as GPIO
    import time
    
    #GPIO Basic initialization
    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)
    
    #Use a variable for the  Pin to use
    #If you followed my pictures, it's port 7 => BCM 4
    led = 4 
    
    #Initialize your pin
    GPIO.setup(led,GPIO.OUT)
    
    #Turn on the LED
    print "LED on"
    GPIO.output(led,1)
    
    #Wait 5s
    time.sleep(5)
    
    #Turn off the LED
    print "LED off"
    GPIO.output(led,0)

    I commented 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:
    python led_on.py

The LED will turn on for 5 seconds and then turn off
I 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

Conclusion

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.

2 comments

  1. ED Reply

    Patrick,

    I used your excellent tips on how to install a print server on a the pi, and it works great.
    Thank you.
    I like coming back to you website, for more tips and things to try, It’s just great!!!

Leave a Reply

Your email address will not be published. Required fields are marked *