The GPIO pins of the Raspberry Pi 4 board, the 3, and also its predecessors, provide the SBC board with capabilities similar to those that Arduino can have, since with them you can create very interesting electronic projects controlled from the operating system by means of code in different languages, such as Python.
That makes the board more than just a cheap computer. It will allow you to connect a multitude 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 ...
Table of Contents
What is GPIO?
GPIO is the acronym for General Purpose Input / Output, that is, General Purpose Input / Output. Different electronic products can have it, such as the chips themselves or certain PCB boards such as this Raspberry Pi. As their name suggests, they are pins that can be configured to perform different functions, hence they are general purpose and not for a specific use.
It will be the user at runtime who can configure these GPIO pins so that they do what he wants. It can be done in different ways, such as with certain codes or scripts from the console or with the Python program, which is one of the simplest and most preferred ways due to the amount of options you have at your disposal.
In this way, the Raspberry Pi not only has a series of ports and interfaces to connect multiple standard devices, but add these GPIO pins so you can add other electronic devices or maker projects that you have created yourself. In the same way that you would with Arduino and its I / O pins for control.
Y not exclusive to Arduino or Raspberry Pi, so do other similar SBC boards and embedded products.
And between her CARACTERISTICS most outstanding:
- 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.
By the way, when a group of GPIO pins is grouped, as is the case with the Raspberry Pi, the group is known as GPIO port.
The GPIO pins of the Raspberry Pi
New Raspberry Pi 4 boards and version 3 They 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 that you can find in the port of Raspberry Pi boards. And that will be the first thing I would like to make clear, since that is how you will know the types of pins you can count on 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 GPIOs are the interface between the Raspberry Pi and the outside world, but they have its limitations, especially electrical. Something that you must take into account so as not to spoil the board is to remember that these GPIO pins are usually unbuffered, that is, without buffer. This means that they do not have protection, so you must monitor the magnitudes of voltage and intensity applied so as not to end up with a useless plate ...
GPIO differences between versions
As I said, not all models are the same pinsHere are some diagrams so you can see the differences between models and thus be able to focus on the Raspberry Pi 4 and 3, which are the newest and the one you probably have in your possession. It differs between (all of 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?
You can not only connect electronic devices as transistors, humidity / temperature sensors, thermistors, stepper motors, LEDs, etc. You can also connect components or modules created specifically for the Raspberry Pi and that extend the capabilities of the board beyond what is included in the base.
I am referring to the famous hats or hats and plates that you can find in the market. There are many types, from those used to control motors with drivers, to others to create a computing cluster, with LED panel controllable, to add DVB TV capability, LCD screen, etc.
These hats or hats They are mounted on the Raspberry Pi board, matching the GPIOs needed for it to work. Therefore, its assembly is quite simple and fast. Of course, make sure of the plate version compatible with each hat, since the GPIO port is different as you have seen ...
I say this in case you have an older plate, since hats are compatible only with the newest. As are the Raspberry Pi Model A +, B +, 2, 3, and 4 models.
Introduction to using GPIO on the Raspberry Pi
To get started, on Raspbian, you can open the console and type the command PinoutWhat it will return to you is an image in the terminal with the GPIO pins available on your board and what each one is for. Something very practical to always have it present at the moment of work so that you do not get confused.
First project: flashing an LED with the GPIOs
The most basic way to make a kind of "Hello world" with GPIOs 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 making use of the terminal. In Linux, specific files are used like the ones 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 electrical pulse to the LED to turn it on, but if it was a sensor, etc., you could use in. Now for turn on (1) or turn off (0) the LED you can use:
echo 1 > /sys/class/gpio/gpio17/value echo 0 > /sys/class/gpio/gpio17/value
If you want to move on to another project and delete entry 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, like all the previous ones, save them in a file type bash script and then run them in bundles all at once, instead of typing them one by one. This is handy when you repeat the same exercise many times, so you don't have to rewrite. Just run and go. For example:
#!/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 finish, you save and then you can give it the appropriate execute and execute permissions the script for the LED to turn on, wait 1.3 seconds and turn off like this in a loop ...
chmod +x led.sh ./led.sh
Obviously the above works 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 programming languages to make different scripts or source codes that automate the operation.
They can be used Different tools to program, with very different languages. The libraries that the community has developed make things much easier for you, such as WiringPi, sysfs, pigpio, etc. The programs can be very varied, from Python, which is the preferred option of many, through Ruby, Java, Perl, BASIC, and even C #.
Officially, the Raspberry Pi offers you many facilities to program your GPIOs, 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 flexibly the one you like the most or you think is simple.