GPIO: all about the Raspberry Pi 4 and 3 connections

Raspberry Pi 4 GPIO

The GPIO pins of the Raspberry Pi 4, 3 , and its predecessors, give the SBC board capabilities similar to those of Arduino, since with them you can create very interesting electronic projects controlled from the operating system using code in different languages, such as Python.

This makes the board more than just a cheap computer. It allows you to connect a multitude of electronic components that you can use with Arduino, but which can also be controlled from the Pi. In this guide, I'll try to give you all the information possible about these GPIO pins so you can start using them…

What is GPIO?

GPIO

GPIO stands for General Purpose Input/Output. It can be found in various electronic products, such as chips or certain PCBs, like this Raspberry Pi. As the name suggests, these are pins that can be configured to perform different functions; hence, they are general-purpose and not for a specific use.

The user will be able to configure these GPIO pins at runtime to perform their desired functions. This can be done in various ways, such as using specific commands or scripts from the console, or with the Python program, which is one of the simplest and most popular methods due to the wide range of options available.

In this way, the Raspberry Pi not only has a series of ports and interfaces for connecting various standard devices, but it also adds these GPIO pins so you can add other electronic devices or maker projects that you've created yourself. Just like you would with Arduino and its I/O pins for control.

And it's not exclusive to Arduino or Raspberry Pi ; other similar SBC boards and embedded products also have it.

GPIO Features

And among its most outstanding features :

  • They can be configured so much as input as output. They have that duality as it happens to those of Arduino.
  • GPIO pins too can be activated and deactivated by code. That is, they can be set to 1 (high voltage level) or 0 (low voltage level).
  • Of course they can read binary data, as the ones and zeros, that is, voltage signal or absence of it.
  • Output values ​​of Reading and writing.
  • The input values ​​can be configured in some cases as events so that they generate some type of action on the board or system. Some embedded systems use them as IRQs. Another case is to configure that when one or more pins are active by certain sensors, perform some action ...
  • Regarding the voltage and intensity, you must know well the maximum capacities acceptable for the board, in this case the Raspberry Pi 4 or 3. You should not pass them to avoid damaging it.

Incidentally, when a group of GPIO pins is grouped together, as is the case with the Raspberry Pi, the set is known as a GPIO port.

The GPIO pins of the Raspberry Pi

Raspberry Pi GPIO

Scheme valid for version 4, 3, Zero

The new Raspberry Pi 4 and 3 boards are equipped with a large number of GPIO pins. Not all versions offer the same number, nor are they numbered in the same way, so you need to be careful to know how to make the correct connections depending on your specific model and revision.

But what is more generic are the types of GPIO pins you can find on the Raspberry Pi board's ports. And that's the first thing I'd like to clarify, so you'll know what types of pins you can use for your projects:

  • FoodThese pins are used to connect the power lines or wiring for your electronic projects. They correspond to pins similar to those on the Arduino board, and that provide voltages of 5v and 3v3 (3.3v limited to 50mA load). In addition, you will also find the ground ones (GND or Ground). If you do not use external power sources such as batteries, or adapters, these pins can be of great help to power your circuit.
  • DNC (Do Not Connect): they are pins that are in some versions and that have no function, but that in the new boards they have been given another purpose. You will only find these in more primitive models of the Pi. In the new 3 and 4 they will be marked as GND in general, being able to integrate in the previous group.
  • Configurable pins: They are the normal GPIOs, and they can be programmed by codes as I will explain later to do what you need.
  • Special pins: these are some connections that are intended for special connections or interfaces like UART, TXD and RXD serial connections, etc., as it happens with Arduino. You will even find some like SDA, SCL, MOSI, MISO, SCLK, CE0, CE1, etc. They stand out among them:
    • PWM, which can regulate the pulse width as we saw in a previous article. On the Raspberry Pi 3 and 4 they are the GPIO12, GPIO13, GPIO18 and GPIO19.
    • SPI is another communication interface that I also discussed in another article. In the case of the new 40-pin boards, they are the pins (with different communication channels as you can see):
      • SPI0: MOSI (GPIO10), MISO (GPIO9), SCLK (GPIO11), CE0 (GPIO8), CE1 (GPIO7)
      • SPI1: MOSI (GPIO20); MISO (GPIO19); SCLK(GPIO21); CE0 (GPIO18); CE1 (GPIO17); CE2 (GPIO16)
    • I2C is another connection that I have also explained in this blog. This bus is made up of the data signal (GPIO2) and the clock (GPIO3). In addition to EEPROM Data (GPIO0) and EEPROM Clock (GPIO1).
    • Serial, another very practical communication with TX (GPIO14) and RX (GPIO15) pins like the ones you can find on the board Arduino UNO.

Remember that GPIO pins are the interface between the Raspberry Pi and the outside world, but they have their limitations , especially electrical ones. Something you must keep in mind to avoid damaging the board is that these GPIO pins are usually unbuffered. This means they have no protection, so you must carefully monitor the voltage and current applied to avoid ending up with a useless board.

GPIO differences between versions

Old Raspberry Pi GPIO pins

As I mentioned, the pinout isn't the same on all models . Here are some diagrams so you can see the differences between models, allowing me to focus on the Raspberry Pi 4 and 3, which are the newest and the one you probably already own. The pinout differs between (all models in each group share the same pins):

  • Raspberry Pi 1 Model B Rev 1.0, with 26-pin slightly different from Rev2.
  • Raspberry Pi 1 Model A and B Rev 2.0, both models with 26-pin.
  • Rapsberry Pi Model A +, B +, 2B, 3B, 3B +, Zero and Zero W, and also 4 models. All of them with a 40-pin GPIO header.

What can I plug into the GPIOs?

raspberry pi hat

Not only can you connect electronic devices such as transistors , humidity/temperature sensors , thermistors , stepper motors , LEDs , etc., but you can also connect components or modules created specifically for the Raspberry Pi that extend the board's capabilities beyond its basic features.

I'm referring to the famous HATs and plates you can find on the market. There are many types, from those used to control motors with drivers, to others for creating a computing cluster , with controllable components , for adding , etc.

These hats are mounted on the Raspberry Pi board, aligning with the necessary GPIO pins for it to function. Therefore, their installation is quite simple and quick. However, make sure you select the correct board version for each hat, as the GPIO pins vary, as you've seen.

I mention this in case you have an older board, as the HATs are only compatible with newer models , such as the Raspberry Pi Model A+, B+, 2, 3, and 4.

Introduction to using GPIO on the Raspberry Pi

Pinout command output

Source: Raspberry Pi

To begin, in Raspbian you can open the terminal and type the command `pinout` . This will return an image in the terminal showing the GPIO pins available on your board and what each one is used for. This is very useful to have on hand while working so you don't get confused.

First project: flashing an LED with the GPIOs

GPIO with LED on Raspberry Pi

The most basic way to create a sort of "hello world" display using GPIO pins is to use a simple LED connected to the Raspberry Pi's pins so you can see how they work. In this case, I've connected one to GND and the other to pin 17, although you can choose any of the standard pins…

Once connected, you can control them from Raspbian using the terminal. In Linux, specific files are used, such as those in the /sys/class/gpio/ directory. For example, to create a file with the necessary structure to begin working:

echo 17 > /sys/class/gpio/export

You can then configure pin 17, chosen for our example, as either an input (in) or an output (out) . You can do this very easily with:

echo out > /sys/class/gpio/gpio17/direction

In this case, we're using the output because we want to send an electrical pulse to the LED to turn it on, but if it were a sensor, etc., you could use input. Now, to turn the LED on (1) or off (0), you can use:

echo 1 > /sys/class/gpio/gpio17/value
echo 0 > /sys/class/gpio/gpio17/value

If you want to switch to another project and delete the entry you created, you can do it this way:

echo 17 > /sys/class/gpio/unexport

By the way, you can also gather all the necessary commands for your project, just like the ones mentioned above, save them in a Bash script file , and then run them all at once instead of typing them one by one. This is practical when you repeat the same exercise many times, so you don't have to rewrite the code. Just run it and you're done. For example:

nano led.sh

#!/bin/bash
source gpio 
gpio mode 17 out
while true; do 
gpio write 17 1 
sleep 1.3 
gpio write 17 0 
sleep 1.3 done

Once you're finished, save it, and then you can give it the appropriate execution permissions and run the script so that the LED turns on, waits 1.3 seconds, and turns off, repeating this cycle…

chmod +x led.sh
./led.sh

Programation advance

programming language source code

Obviously, the above is useful for small electronic projects with few components, but if you want to create something more advanced, instead of commands, you can use programming languages ​​to make different scripts or source codes that automate the operation.

You can use different tools for programming, with a wide variety of languages. Libraries developed by the community make things much easier, such as WiringPi, sysfs, pigpio, etc. The programs themselves can be incredibly diverse, ranging from Python, which is the preferred choice for many, to Ruby, Java, Perl, BASIC, and even C#.

Officially, the Raspberry Pi offers many features for programming its GPIO pins, such as:

  • Scratch, for those who do not know how to program and want to use the puzzle blocks of this project with which Arduino can also be programmed, etc. Programming with graphic blocks is quite intuitive and very practical for the field of education.
  • Python: This simple interpreted programming language allows you to create simple and powerful codes, with a multitude of libraries at your disposal to do almost everything you imagine.
  • C / C ++ / C #: are more powerful programming languages ​​to create binaries with which to interact with the GPIOs. You can do it in several ways, using the standard form or kernel interface via the librarylibgpiod, but also through a third-party library such as pygium.
  • processing3, similar to the Arduino.

Choose with flexibility whichever one you like best or find easiest.


Add as preferred source in Google