Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (2023)

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 sometests, 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.

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:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (1)

The goal of the GPIO Pins is to add someextensions 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.

GPIO Pinout

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.

I have a complete guide on the GPIO pinout here that you can check for more details.
Here is an illustration to move quickly:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (2)

The website pinout.xyz is useful for this.
It gives you the exact layout and role of each pin.
I also have a dedicated article to the Raspberry Pi Pico pinout if you are using this model.

If you don’t have it with your breadboard or GPIO kit, I recommend printing this image to use it later.

Claim Your Python Syntax Reference Sheet!
Keep Python coding syntax at your fingertips with this handy cheat sheet!

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 (it should be pre-installed on most versions)
    sudo apt install python3-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 “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).

Are you a bit lost in the Linux command line?Check this article first for the most important commands to remember and a free downloadable cheat sheet so you can have the commands at your fingertips.

Required Hardware

Here is the recommended hardware you need to have to follow the rest of this tutorial (Amazon links):

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 understanding how it works.

Breadboard Installation

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 should look like this:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (3)

Don’t plug in 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 port 0, it will not work near port 50.

Claim Your Python Syntax Reference Sheet!
Keep Python coding syntax at your fingertips with this handy cheat sheet!

Other Ports

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.

Schema

It will be clearer with this picture:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (4)

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 seethe green square how they are connectedtogether.
It’s the same for each column, for both side of the middle line.

If needed, here is a complete schema:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (5)

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 toplug 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)
Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (6)

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:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (7)

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:

Getting Started with GPIO Pins on Raspberry Pi (Beginners guide) – RaspberryTips (8)

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.

🖋 Love Raspberry Pi & writing?
Combine your passions and get paid. Write for RaspberryTips!

Python script

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:
    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 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 touse the BCM number from pinout.xyz.

  • Save and Exit (CTRL+X)
  • Run the script with:
    python3 led_on.py

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.

By the way, if you are new to Python scripts, you must probably check this article first. I also have a complete e-book about Python on Raspberry Pi, that explain everything from scratch to mastery. I highly recommend using it if you are serious about learning Python and Raspberry Pi.

Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!

Reminder: Remember that all the members of my community get access to this website without ads, exclusive courses and much more. You can become part of this community for as little as $5 per month & get all the benefits immediately.

You may also like:

  • 25 awesome Raspberry Pi project ideas at home
  • 15 best operating systems for Raspberry Pi (with pictures)
  • My book: Master your Raspberry Pi in 30 days

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 🙂

You may also like: The 11 Best Raspberry Pi Robots Kits for Beginners.

Get My Commands Cheat Sheet!
Grab your free PDF file with all the commands you need to know on Raspberry Pi!

Additional Resources

Not sure where to start?
Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Watch the Raspberry Pi Bootcamp course now.

Master your Raspberry Pi in 30 days
Don’t want the basic stuff only? If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.
Download the e-book.

VIP Community
If you just want to hang out with me and other Raspberry Pi fans, you can also join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
More details here.
Need help building something with Python?
Create, understand, and improve any Python script for your Raspberry Pi.
Learn the essentials step-by-step without losing time understanding useless concepts.
Get the e-book now.

You can also find all my recommendations for tools and hardware on this page.


This tutorial doesn't work anymore? Report the issue here, so that I can update it!


FAQs

What is the basic of GPIO? ›

A GPIO (general-purpose input/output) port handles both incoming and outgoing digital signals. As an input port, it can be used to communicate to the CPU the ON/OFF signals received from switches, or the digital readings received from sensors.

What is the first thing you should do before connecting electronics to the GPIO? ›

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. ...
  • Install the rpi.gpio package (it should be pre-installed on most versions) sudo apt install python3-rpi.gpio.
  • Enable I2C and SPI in raspi-config.

How do I activate my GPIO? ›

Activate GPIO Via Terminal Commands
  1. raspi-gpio get prints the state of all GPIO pins.
  2. raspi-gpio get X prints the state of GPIO pin X.
  3. raspi-gpio set X op sets GPIO pin X as an output.
  4. raspi-gpio set X dh sets GPIO pin X to drive high.
  5. raspi-gpio set X dl sets GPIO pin X to drive low.
Feb 17, 2022

How do I set up GPIO? ›

Configuring GPIO
  1. sudo apt-get update sudo apt-get upgrade.
  2. sudo apt-get install rpi.gpio.
  3. sudo raspi-config.
  4. lsmod | grep i2c_
  5. sudo raspi-config.
  6. lsmod | grep spi_
  7. sudo nano led_blink.py.
  8. #import the GPIO and time package import RPi. GPIO as GPIO import time GPIO. setmode(GPIO. BOARD) GPIO. setup(7, GPIO.
Oct 27, 2022

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated: 28/12/2023

Views: 5295

Rating: 4.4 / 5 (75 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.