GPIO: all about Raspberry Pi 4 and 3 connections

Raspberry Pi 4 GPIO

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

That makes the board more than just a cheap computer. It will allow you to connect a lot of electronic elements that you can use with Arduino, but that can also be controlled from the Pi. In this guide I will try to give you as much information as possible about these GPIO pins so that you can start taking advantage of them…

What is GPIO?

GPIO

It will be the runtime user who can configure these GPIO pins to do what he wants. This can be done in different ways, like with certain code or scripts from the console or with the Python program, which is one of the easiest and most preferred ways because of the amount of options you have at your disposal.

This way, the Raspberry Pi not only has a number of ports and interfaces to connect various standard devices, but adds these GPIO pins so you can add other electronic devices or project makers that you have created yourself. The same way you would do it with Arduino and its I/O pins for control.

And is not exclusive to Arduino or the Raspberry Pi, so do other similar SBC plates and embedded products.

GPIO functions

And among its most outstanding features:

  • They can be configured both as input and output. They have that duality as it happens to the Arduino’s.
  • GPIO pins can also be enabled and disabled 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, such as ones and zeros, i.e. voltage signal or no voltage signal at all.
  • Read and write output values.
  • The input values can be configured in some cases as events to generate some kind of action on the board or system. Some embedded systems use them as IRQ. Another case is to configure that when one or more pins are active by certain sensors perform some action .
  • As for the voltage and intensity, you must know the maximum acceptable capacities of the board, in this case the Raspberry Pi 4 or 3. You must not pass them to avoid damaging it.

 

By the way, when a group of GPIO pins is grouped together, as is the case with the Raspberry Pi, the group is known as ‘Strong’.

Raspberry Pi GPIO pins

GPIO Raspberry Pi
Scheme valid for version 4, 3, Zero

The new Raspberry Pi 4 plates and the 3 version are equipped with a large number of GPIO pins. Not all versions offer the same amount, nor are they numbered in the same way, so you have to be careful with this to know well how you should make the connection according to the model and revision you have.

But what is more generic is the types of GPIO you can find in the port of the Raspberry Pi plates. And that will be the first thing I’d like to make clear, because that way you will know ‘strong’ the types of pins you can count on for your projects

  • Power: these 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 provide 5v and 3v3 voltages (3.3v limited to 50mA load). In addition, you will also find the ground pins (GND or Ground). If you don’t use external power sources such as batteries or adapters, these pins can be a great help to power your circuit.
  • DNC (Do Not Connect): these pins are available in some versions and have no function, but in the new boards they have been given another purpose. You will find these only in more primitive models of the Pi. In the new 3 and 4 will be marked as GND generally, being able to integrate in the previous group.
  • Configurable pins: they are the normal GPIO, and 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. Among them we can highlight:
    • PWM, that can regulate the width of the pulse as we already saw in a previous article. In the Raspberry Pi 3 and 4 are the GPIO12, GPIO13, GPIO18 and GPIO19.
    • SPI is another communication interface that I also analyzed in another article. And the case of the new 40-pin boards, are the pins (with different communication channels as you 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 composed by the data signal (GPIO2) and the clock (GPIO3). Besides 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 in the Arduino UNO board.

 

Remember that GPIOs are the interface between the Raspberry Pi and the outside world, but they have limitations, especially electrical ones. One thing you must be very careful not to damage the board is to remember that these GPIO pins are usually unbuffered, that is, without a buffer. This means that they do not have protection, so you must watch the magnitudes of voltage and current applied not to end up with a useless board …

Differences between GPIO versions

old Raspberry Pi GPIO pins

As I said, not in all models are the same pins, I leave you here some diagrams so you can see the differences between models and so I can focus on the Raspberry Pi 4 and 3, which are the newest ones and the one you probably have in your possession. There are differences between them (all of them share the same pins):

  • Raspberry Pi 1 Model B Rev 1.0, with 26-pin slightly different than 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 the 4 models. All of them with a 40-pin GPIO head.

What can I connect on the GPIO?

Raspberry Pi hat

Not only will you be able to connect electronic devices such as transistors, but also moisture/temperature sensors, transistors, moisture/temperature sensors, termistors, step motors, LEDs, etc. You can also connect components or modules created specifically for the Raspberry Pi that extend the capabilities of the board beyond what it includes as a base.

I’m referring to the famous hats and plates you can find in the market. There are many types of them, from those used to control engines with drivers, to others to create a computation cluster, with LED panel controllable, to add DVB, LCD screen, etc.

These hats are mounted on the Raspberry Pi plate, matching the GPIO required for it to work. Therefore, its assembly is quite simple and fast. But be sure to check the version of the board compatible with each hat, because the GPIO port is different as you have seen…

I’m saying this in case you have an older board, since hats are only compatible with newer ones. Like the Raspberry Pi Model A+, B+, 2, 3, and 4.

Introduction to the use of GPIO on the Raspberry Pi

Output of pinout command
Source Raspberry Pi

To start, in Raspbian, you can open the console and type the command pinout, what will return is an image in the terminal with the GPIO pins available on your board and what each one is for. Something very practical to have always present at the time of work so you don’t get confused.

First project: make a LED blink with the GPIO pins

GPIO with LED on Raspberry Pi

The most basic way to make a kind of “hello world” with the GPIO is to use a simple LED connected to the pins of the Raspberry Pi so you can see how they work. In this case, I have connected it to GND and the other to pin 17, although you can choose another of the normal pins…

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


echo 17 > /sys/class/gpio/export

Then you can configure as an input (in) or as an output (out) that pin 17 chosen for our example. You can do it very easily with:


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

In this case as an output, since we want to send an electric pulse to the LED to turn it on, but if it is a sensor, etc., you could use in. 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 move to another project and ‘delete’ the created entry, you can do it this way:


echo 17 > /sys/class/gpio/unexport

By the way, you can also collect all the necessary commands for your project, like all the previous ones, save them in a file like script de Bash and then execute them in package at once, instead of typing them one by one. This is practical when you repeat the same exercise many times, so that you don’t have to rewrite. Just run 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 are done, you save and then you can give it the proper execution permissions and run the script so that the LED lights up, waits 1.3 seconds and then turns off in a loop…

chmod +x led.sh
./led.sh

Advanced programming

código fuente lenguaje de programación

Obviously this is useful for small electronic projects with few components, but if you want to create something more advanced, instead of the commands, what you can use are ‘string’ programming languages to make different scripts or source code that automate the operation.

Different tools can be used for programming, with very different languages. The libraries developed by the community make things much easier for you, like WiringPi, sysfs, pigpio, etc. The programs can be very varied, from Python, which is the preferred option for many, to Ruby, Java, Perl, BASIC, and even C#.

Officially, the Raspberry Pi offers you many facilities to program your GPIO, like:

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

Choose with flexibility the one you like or simple you find.

Leave a Comment

*

*