If you click our links and make a purchase, we may earn an affiliate commission. Learn more
The GPIO pins, placed on each Raspberry Pi model, are one of the best features to expand the device’s capabilities. But, it’s not easy to get started because the pin numbers seem to have been placed randomly, and it isn’t even the same numbers to use in your code.
Don’t worry, that’s why I created this article. I’ll give you the diagram with all the pin numbers, and the equivalent IDs you should use in your Python scripts. By the end of this article, you’ll be able to use this as a foundation for any electronic project you have in mind.
If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!
Want the best experience? Become a premium member for ad-free browsing, access exclusive content, and ask questions in our private forums. Your membership helps support the site!
The Raspberry Pi Pinout Diagram
Introduction
All Raspberry Pi models come with 40 GPIO pins on the main board (except for the Pico, which I have a separate guide for it here). The Raspberry Pi Zero (and Zero 2) exist in two versions (with or without the pins pre-soldered), but you can use them too.
The good news is that all models use the same numbering and the same pinout.
The bad news is that this numbering isn’t intuitive at all. Not all pins can be used the same way, and the numbers you’ll use in your code are not necessarily the ones you see (or can guess) on the board.
That’s why you most likely need the diagram below (or that kind of adapter on Amazon), to make sure you are using the correct IDs in your project. Feel free to print this diagram, or part of this page, to have it with you when needed.
Simplified diagram
When you take the Raspberry Pi board in front of you with the USB ports at the bottom, the GPIO pin 1 is in the top-left corner. Number 2 is in the top-right corner (not below), and from there it goes from left to right, and the next line, as if reading a book.
Also: The 5 fastest web browsers for Raspberry Pi — tested and ranked!
There are two lines of 20 pins, so 40 pins in total. The last one in the bottom-right corner is number 40.
Well, the issue is that you won’t use these numbers in your scripts. Instead, you’ll use their name “equivalents”, which are mentioned on the side in this diagram:
The red ports on this diagram are used for power input. There are two options: 3V or 5V. Make sure to use something compatible with your electronic circuit.
Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
The black ports are for grounding. That’s explicit enough I think, you’ll typically plug one on the blue line of your breadboard if you use one (as explained here).
Then all the other ports are programmable. They can be used for input (sensor) or output (LED, servo).
Their names do not follow the pin IDs, for example, GPIO 1 is port 28 while GPIO 2 is port 3. This is why you’ll need this diagram all the time.
In the example I give you later, I will light a LED on pin 37, but use GPIO 26 in my code. So, you’ll have to do this mental exercise each time.
If you use the Argon NEO for your Raspberry Pi case (read my review here), the Argon ONE (review), or something similar, you may have some guidance on it, which is really useful. Here is what it looks like on the Argon NEO, for example:
Full diagram
I only gave you the simplified version of the GPIO pinout diagram because I think it’s what you’ll need most of the time. But if you play with advanced accessories, especially using SPI or I2C, you may need more information, as each port can have several names.
Also: 25 project ideas you can try at home with Raspberry Pi
You’ll find all the defaults in the official datasheet (here, page 10), or you can check the graphic version on this website.
You might also like: Enabling UART on Raspberry Pi: A Step-by-Step Guide
Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
How to use the GPIO pinout with Python
Each GPIO pin has a number and a name. In Python, the number is mentioned in the name. For example, port 37 is GPIO26, so we’ll use “26” in the Python script.
I created a simple circuit as an example, with just a LED and a resistor, using 3 wires to plug 3 GPIO pins:
- Port 1: power input (3V)
- Port 6: ground
- Port 37: GPIO26, to control the LED
If you want to try this simple circuit on your own, read my GPIO guide for beginners, as I think I use something very similar in it (maybe not the same pins, but it’s a good exercise anyway).
In Python, the full script will look like this:
import RPi.GPIO as GPIOimport time#GPIO Basic initialization GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) #LED configurationled = 26GPIO.setup(led, GPIO.OUT)#Turn on the LED print("LED on") GPIO.output(led,1)#Wait 5stime.sleep(5) #Turn off the LED print("LED off") GPIO.output(led,0)
Here are a few takeaways:
Also: No screen? No problem! Here's how to setup a Pi without one.
- I use RPi.GPIO (a pre-installed Python library).
Other solutions are possible, but let’s use this one as an example. - I start with two initialization steps, especially setting GPIO.BCM, which means we’ll use the port “name” instead of the pin “numbers” (GPIO.BOARD).
- Then, I define the LED name (26 in my example), and initialize it as an output with:
GPIO.setup(<name>, GPIO.OUT)
- From there, I can set it on or off with:
GPIO.output(<name>, <0|1>)
Remember, if reading this Python script feels like deciphering hieroglyphs for you, you will benefit from reading my book “Master Python on Raspberry Pi“. I explain everything you need (and only that), to use it for any project. In a few weeks, you’ll be ready, and this example will be easy to read for you.
Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
Anyway, this was just a basic example to get you started and show you how to use the previous diagram. I hope this will be useful.
If you want to go further, SunFounder has the perfect kit for your Raspberry Pi, including all the components you might need (sensors, wires, etc.). You can find it here on Amazon, or directly on their website. It’s more than 150 projects you can do with it, all documented, in Python, C++, Java and more. Don’t miss out!
Download Your Essential Linux Commands Guide!
It's a free PDF guide containing every Raspberry Pi Linux command you should know!
Download now
🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!
If you are looking for exclusive tutorials, I post a new course each month, available for premium members only. Join the community to get access to all of them right now!
You may also like:
- Managing Python libraries the right way
- Using GPIO pins with Python doesn't have to be complicated
- Programming your Raspberry Pi camera with Python
- Your Python script can have a GUI, here's how
- Master Python on Raspberry Pi (ebook)
Related questions
What does GPIO mean?
GPIO stands for “General Purpose Input/Output”. It’s an interface used to plug other electronic devices into the Raspberry Pi.
What are GPIO pins used for?
GPIO pins are used to connect additional devices, sensors, and wires to the Raspberry Pi, and use it as the brain of any electronic circuit. For example, a weather station, a robot, or an expansion card (named “HAT”).
You can find some concrete examples on this website:
- The 11 Best Raspberry Pi Robots Kits for Beginners
- Top 13 Raspberry Pi HATs you need to try
- All The Best Raspberry Pi Project Ideas (with links)
Which GPIO pin should I use?
Each pin has a different role. Some of them provide power, others are ground, and others can be used in programming. Each pin can be used as input (for a thermometer, for example) or output (for a LED).
Also: Pi5 vs. Pi4: I tested them, here's the result
Use the diagram provided above to know which pin to use. But basically, any green pins on my diagram are free to use. Follow the script given previously as a template to make sure the port is properly initialized before use.
Whenever you’re ready, here are other ways I can help you:
The RaspberryTips Community: If you want to hang out with me and other Raspberry Pi fans, you can join the community. I share exclusive tutorials and behind-the-scenes content there. Premium members can also visit the website without ads.
Master your Raspberry Pi in 30 days: 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.
The Raspberry Pi Bootcamp: Understand everything about the Raspberry Pi, stop searching for help all the time, and finally enjoy completing your projects.
Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.
You can also find all my recommendations for tools and hardware on this page.
How would you rate this article?
Click on a star to rate it!
Average rating / 5. Vote count:
No votes so far! Be the first to rate this post.
Also: Tired of Raspberry Pi OS? Level up with these top-rated systems.
As you found this post useful...
Spread the word!
We are sorry that this post was not useful for you!
Let us improve this post!
Tell us how we can improve this post?
Also: 25 project ideas you can try at home with Raspberry Pi