The Raspberry Pi 4 board has a GPIO header with 40 pins. This GPIO header is also the same for Raspberry Pi 3 boards, so this guide applies to both versions.
GPIOs allow you to easily use hardware features and communication, directly from a computer – the Raspberry Pi microprocessor. It brings the Raspberry Pi 4 much closer to hardware applications, making it perfect for being embedded in a hardware application or product: a robot, a retro-gaming application, etc.
In this complete Raspberry Pi 4 pins guide I’ll break down all the pins of the GPIO header, and you’ll learn what you can/can’t do with them.
Along the way I’ll make some comparisons with the pins of the Arduino boards. You’ll find out that the pins can be quite similar for some functionalities. At the end I’ll give you more details about the differences between Arduino and Raspberry Pi pins. If you’re interested about Arduino pins, check out this tutorial: Arduino Uno Pinout Guide.
>> Watch this video as an additional resource to this article:
You are learning how to use Raspberry Pi to build your own projects?
Check out Raspberry Pi For Beginners and learn step by step.
After watching the video, subscribe to the Robotics Back-End Youtube channel so you don’t miss the next tutorials!
Table of Contents
Raspberry Pi 4 pins overview
Here’s a complete overview with all the GPIOs and their primary function.
Now, let’s break down each pin or group of pins, and see what they can do.
A word of caution
Before you plug anything to a Raspberry Pi 4 pin, you have to know that you can easily damage the board if you do something wrong. There are no real hardware safety when it comes to the Raspberry Pi hardware pins.
If you connect a ground (GND) pin to a 3.3V pin directly, well… You might destroy your Raspberry Pi board the second those pins are connected together.
So, be really careful when you plug something or when you create a test circuit. If you have any doubt, double, triple check, and ask someone for help before you burn your board.
But if you follow some basic rules and common sense, you’ll have nothing to worry about!.
The ground is very useful for making a common reference between all components in your circuit. Always remember to connect all components to the ground.
If you connect 2 circuits together, add a wire between both grounds to make it common. If you add a new sensor/actuator to an existing circuit, connect the ground of the component to the ground of the circuit.
That’s very important. Without that, you may burn some parts of the circuit, you may have components that do not function correctly, give wrong values, etc.
8 out of the 40 GPIOs are connected to the ground. You can find them with the 3 letters GND.
One additional warning: don’t ever connect the ground directly to a power supply pin (3.3V or 5V)! This creates a short circuit and can definitively burn your Raspberry Pi 4 board.
You can find 2 pins bringing 3.3V and 2 pins bringing 5V.
Those pins can be used to power components such as sensors or small actuators. Note that they are certainly not powerful enough to actuate motors such as servo or stepper motors. For that you’ll need an external power source.
The power pins are used as a source to power external components, not to power the Raspberry Pi itself from an external source. (Well there is a way to power the Raspberry Pi from the GPIO header, but you have a high probability of burning it, so just use the micro-USB port)
And just another word of caution: as previously said in the Ground pins section, don’t ever connect one of the power pin directly to one of the GND of the Raspberry Pi 4!
The pins 27 and 28 are reserved pins. They are usually used for I2C communication with an EEPROM.
If you just begin with Raspberry Pi 4 pins, just don’t connect anything to those pins. There are many other available pins for you to use.
Well, that’s 14 slots already taken for GND, power supply and reserved pins. Now let’s see how the other 26 GPIOs are used for communication.
Raspberry Pi 4 GPIOs
GPIO means General Purpose Input/Output. Basically that’s one pin you can use to write data to external components (output), or read data from external components (input).
If you embed your Raspberry Pi board with some hardware components, the GPIO header will become quite useful.
GPIOs will allow you to read some basic sensors (ex: infrared), control some actuators (those which are working with a ON/OFF mode), and communicate with other hardware boards, such as Raspberry Pi, Arduino, Beaglebone, Jetson Nano, etc.
GPIOs are digital pins
The Raspberry Pi 4 GPIOs are quite similar to what we call “digital pins” on an Arduino board.
First you need to choose whether you want to use them as input or output. If you configure a GPIO as input, you’ll be able to read a value from it: HIGH or LOW (1 or 0). And if you configure a GPIO as output, you’ll be able to write a value to it, also HIGH or LOW.
A digital pin has only two states. LOW usually means 0V, and HIGH means 3.3V (with some tolerances). That’s very simple, it’s like a switch that you turn on and off.
Note: when you set the mode for a GPIO as output, after booting the Pi, you can expect a different default state for different GPIOs.
All GPIOs work at 3.3V. It’s important for you to know that, in case you need to plug in a component with a different voltage.
Sometimes, you’ll find sensors that are powered with 5V, but all the communication pins are running with 3.3V. In this case, no problem: you can use the 5V power pin from the Raspberry Pi to power the component, and then use any 3.3V GPIO for the communication. If you don’t mix the 5V signal with the 3.3V signals, everything should be alright.
Now if you need to make your Raspberry Pi 4 GPIOs communicate with 5V pins directly (ex: Arduino Uno or Mega), you’ll need to use a 3.3V to 5V level shifter. You can either buy one or build one yourself.
If you use 3.3V Arduino boards such as Due, Zero, or M0, you won’t need to add a 3.3V to 5V level shifter, and you can plug the Arduino pins directly to the Raspberry Pi 4 GPIOs.
How to use GPIOs
Here’s the GPIO header again:
To use a GPIO, first you need to know its number.
As you can see, the pin numbers and GPIO numbers are different. Pin numbers are in grey, and GPIO numbers in orange. Depending on the library you use to manipulate GPIOs, you’ll either have to use the number of the pin or the GPIO number. For example, pin 29 corresponds to GPIO 5. Any time you have a doubt, just check the pinout again and you’ll know!
So, to use any of those GPIO, first you need to configure it as input or output, and after that you can write to it, or read from it.
Now, you might wonder: how can you configure and use the GPIOs from your code? Do you need to dive into complex hardware stuff to do that?
Well, good news for you. There are at least 2 libraries that will allow you to easily use those pins. For Python, you can use RPi.GPIO, and for Cpp you can use WiringPi.
Those libraries were developed so you can use the Raspberry Pi pins just like you would use Arduino pins, which means that all the complex stuff is hidden and you can use them with just a few lines of code.
For example, to set GPIO 17 (pin 11) as output/high:
// With WiringPi (Cpp) pinMode (17, OUTPUT); digitalWrite(17, HIGH);
# with RPi.GPIO (Python) GPIO.setmode(GPIO.BCM) # Choose BCM to use GPIO numbers instead of pin numbers GPIO.setup(17, GPIO.OUT) GPIO.output(17, GPIO.HIGH)
To make 2 boards communicating with each other, it’s quite simple: you’ll configure a GPIO as an input on one side, and as an output on the other side. You can then use more GPIOs to transfer more pieces of information.
Communication protocols through Raspberry Pi 4 pins
You can use some hardware communication protocols directly with the Raspberry Pi 4 GPIOs. Those communication protocols are in fact the same ones that you can natively use on many Arduino boards.
With those protocols you’ll be able to transfer far more information than with just a bunch of GPIOs configured as digital pins.
On the Raspberry Pi 4 pinout schematics, you can see a column for alternate functions. Well, the communication protocols are all there!
In fact, saying that a GPIO is a digital pin is an overly exaggerated simplification. It’s much more than that. For each GPIO you have at least one alternate function, and sometimes many more.
But let’s keep things simple here. You don’t need to know all the alternate functions to get started and develop cool applications. If you’re interested though, check out page 102 of the bmc2835 datasheet (this is the datasheet for the whole GPIO header), where you’ll see a complete table with all alternate functions for all GPIOs.
UART is multi master communication protocol. This protocol is quite easy to use and very convenient for communicating between several boards: Raspberry Pi to Raspberry Pi, or Raspberry Pi to Arduino, etc.
For using UART you need 3 pins:
- GND that you’ll connect to the global GND of your circuit.
- RX for Reception. You’ll connect this pin to the TX pin of the other component.
- TX for Transmission. You’ll connect this pin to the RX of the other component.
If the component you’re communicating with is not already powered, you’ll also have to use a power pin (3.3V or 5V) to power on that component.
By using a UART to USB converter, you can communicate between your laptop and Raspberry Pi with UART.
Now, to use UART in your code, you can use the Serial library in Python, and WiringPi in Cpp.
If you’re interested in communicating between a Raspberry Pi board and an Arduino board via Serial, check out this Raspberry Pi Arduino Serial tutorial.
I2C is a master-slave bus protocol (well it can have multiple masters but you’ll mostly use it with one master and multiple slaves). The most common use of I2C is to read data from sensors and actuate some components.
The master is the Raspberry Pi, and the slaves are all connected to the same bus. Each slave has a unique ID, so the Raspberry Pi knows which component it should talk to.
For using I2C you’ll need 3 pins:
- GND: I guess you start to get used to that!
- SCL: clock of the I2C. Connect all the slaves SCL to the SCL bus.
- SDA: exchanged data. Connect all the slaves SDA to the SDA bus.
And as most of the time you’ll need to power on the component, you’ll also need a power pin (3.3V or 5V), linked to the Vcc pin of the component. Make sure you know which voltage is accepted by the component before you plug anything. But don’t worry too much though: usually, hobby components will accept 3.3V and/or 5V.
Note that the SDA and SCL pins on the Raspberry Pi are alternate functions for GPIO 2 and 3. When you use a library (Python, Cpp, etc) for I2C, those two GPIOs will be configured so they can use their alternate function.
Some of the best and easy-to-use libraries for I2C are SMBus for Python and WiringPi for Cpp.
SPI is yet another hardware communication protocol. It is a master-slave bus protocol. It requires more wires than I2C, but can be configured to run faster.
So, when to use I2C vs SPI on your Raspberry Pi 4? Well, the answer is quite simple. Sometimes you’ll find a sensor that is only I2C or SPI compatible. And sometimes, you’ll just want to have a balance between those protocols, so for example you’ll choose to use I2C if you already have many components using the SPI. As you progress you’ll start to know the differences better, and be able to make a better choice between those two protocols. But for now, let’s keep things simple.
As you can see, you get 2 SPIs by default: SPI0 and SPI1. It means you can use the Raspberry Pi as a SPI master on two different SPI buses at the same time.
And, as for I2C, SPI uses the alternate functions of GPIOs.
For using SPI you’ll need 5 pins:
- GND: what a surprise! Make sure you connect all GND from all your slave components and the Raspberry Pi together.
- SCLK: clock of the SPI. Connect all SCLK pins together.
- MOSI: means Master Out Slave In. This is the pin to send data from the master to a slave.
- MISO: means Master In Slave Out. This is the pin to receive data from a slave to the master.
- CS: means Chip Select. Pay attention here: you’ll need one CS per slave on your circuit. By default you have two CS pins (CS0 – GPIO 8 and CS1 – GPIO 7). You can configure more CS pins from the other available GPIOs.
In your code, you can use the spidev library for Python, and WiringPi for Cpp.
The difference between Raspberry Pi 4 pins and Arduino pins
We often compare the Raspberry Pi 4 pins with the pins from the Arduino boards. To the point where many libraries use the same function name to actuate those pins! For example, WiringPi uses the
void digitalWrite(int pin, int value); to set the state of a GPIO, which is the exact same function in Arduino to set the state of a digital pin.
But it’s not quite the same.
First of all, Raspberry Pi has a microprocessor, often running a Linux system (for example Raspbian), while Arduino has a microcontroller. This makes a huge difference, especially when considering real time constraints.
Also, and that’s something you can’t see on the board directly, many hardware functionalities from Arduino are not present in a Raspberry Pi board.
You don’t have analog pins on a Raspberry Pi board. If you want to use an analog sensor, you’ll have to use an external ADC (Analog to Digital Converter), and maybe get the value using I2C or SPI protocol.
Also there are no native PWM on Raspberry Pi. PWM are quite useful to control components with a non-binary command. You can fake the PWM from software (ex with WiringPi), but it’s clearly not recommended as it will take a lot of CPU and won’t be really fast.
I won’t make the complete list here, but you see the point. Arduino is much closer to hardware than Raspberry Pi, and thus there are many native-hardware functionalities that you can’t get on a Raspberry Pi board.
So, before you choose between those 2 kinds of boards for your project, make sure you know what you need: more computation power, the need to use high level languages (Raspberry Pi), or something more close to hardware, with limited resources (Arduino)?
Get started with Raspberry Pi 4 pins
Well, there are many things you can do with Raspberry Pi 4 pins.
I’ll repeat it here: you can never be too cautious when manipulating the pins! A mistake can destroy your board in less than a second. But if you pay attention and double check everything, there is no reason you’ll burn anything.
Now, if you feel lost with so much information and don’t know where to start, here’s a list of steps you can take from there:
- Get some simple examples and do them, like powering on a LED, read the value from a button, etc. For that you’ll need to create a small circuit on a breadboard, and you’ll use the pins with their primary function (GPIO).
- Once you’re familiar with how basic circuits work (GND, Vcc, and communication pins), try to get a more complex sensor, for example an I2C accelerometer, so you can measure whether your board is on a flat surface or not.
- After you know how to communicate with one sensor, try to communicate between your Raspberry Pi board and another Raspberry Pi/Arduino/Computer, using all 3 protocols: UART, I2C, SPI. You’ll learn a lot by doing that.
And then, there is no secret! Find yourself a personal project, and you’ll learn more stuff along the way.