The Raspberry Pi GPIO pins are the great forgotten ones, but knowing how to use them opens up a world of possibilities. That's what this article is about, to open the doors of this world and introduce you to programming and the possibilities of GPIOs. In future posts we will also show you how to connect Arduino and Raspberry Pi using these pins to achieve great projects combining the power of both platforms.
Introduction to GPIOs and schematics
GPIO (General Purpose Input/Output) is, as the name suggests, a general purpose I/O (Input/Output) system, i.e. a series of connections that can be used as inputs or outputs for multiple uses. These pins are included on all Raspberry Pi models, so you can make interesting projects just as you would with the Arduino.
GPIOs represent the interface between the Raspberry Pi and the outside world. And with them you can make a multitude of projects, from making LEDs flicker to much more sophisticated ones. But for that you need to know their characteristics and how they are programmed. The first thing will vary depending on the revision of the board you have or the model.
All pins are "unbuffered".The pins do not have protection buffers, so you will have to be careful with the magnitudes (voltages, current,...) when connecting components to them in order not to damage the board. As you can see in the pictures below, not all pins have the same function:
- Power pins: you can see 5v, 3v3 (limited to 50mA) and ground (GND or Ground) pins, which provide power at these voltages for your circuits. They can serve as a power supply, although you can also use other sources (batteries, external power supplies, etc). Remember that they are unbuffered and you must be careful not to damage the board.
- DNC (Do Not Connect): are pins that have no function at the moment, but in future implementations are used for other purposes. That's why you will only find them on earlier models of the Raspberry Pi. On current boards they are marked as GND.
- Normal GPIO: are configurable connections that you can programme for your projects, as we will show you later.
- Special GPIOs: Within these are some pins for a UART interface, with TXD and RXD connections that are used for serial communications, such as connecting to an Arduino board. We can also see others such as SDA, SCL, MOSI, MISO, SCLK, CE0, CE1, etc..., which we will explain their operation later.
GPIO diagrams depending on the model
Not all boards and revisions have the same GPIO schematics.. Here we offer you all the diagrams of the different boards that exist at the moment. In the following images we can see the different layouts of the existing models at the moment:
Model B (Rev 1.0)
Model A/B (Rev 2.0)
Model A+, B+, 2B, 3B, 3B+, Zero and Zero W
As you can see, there are three basic schemes. Those in revision 1.0 of the B model, those included in revision 2.0 of the A and B model, and the modern A+, B+, 2, 3B, 3B+ and Zero which share the same wiring diagram.
Introduction to HATs and Plates
Although we will dedicate another post to a mega-comparison of HATs (Hardware Attached on Top) and Plates for the Raspberry PiWith examples of their uses, we are going to explain what they are and what they have to do with GPIOs. If you are already familiar with Arduino, it is easy to get an idea of what these components are, as they are known as Shields or modules and Plates in the Duino world.
The Plates are punched plates for easy mounting of circuits.. Something like a breadboard that can be connected to the GPIOs of the Raspberry Pi. They are a good addition if you want to mount surface mount electronic devices. As you can see, their function is identical to their Arduino equivalent.
On the other hand, the HATs are similar to Arduino shields.. That is, expansion boards that can be connected to the GPIOs to add functionality to the board. If you look at the name, it matches the English word "hat" and that refers to the way they connect to the Raspi.
As a new complement, these are only compatible with Raspberry Pi models. that share the same wiring diagram. If you remember from the previous section, these are the A+, B+ and version 2 and later of the SBC board. But you can find different manufacturers on the market that supply these types of HATs for Raspberry Pi. One of them is AdaFruit, which sells expansion boards called HatADay and which contain TFT screens, GPS, servomechanisms, robotics add-ons, etc...
Introduction to the use of GPIOs
Like Arduino, it uses its Arduino IDE environment to create sketches or source code for programming projects, the Raspberry Pi also allows you to program its GPIOsbut with greater flexibility. It can be done from your distribution using a multitude of tools with different programming languages (Python, Java, C,...), or from the console using simple scripts and commands.
In general, the Raspberry Pi uses Linux distributions. As a good UNIX, the operating system will treat all elements, including hardware, as a file. You know that in Windows there are drives (C:, D:,...) or hardware devices. But on a *nix it's all filessuch as the hard disk (/dev/sda), DVD drive (/dev/dvd), SD cards (/dev/mmcblk0), etc...
Well, GPIO ports will also be treated as another file, even if they are not in the /dev directory, and therefore you will be able to use the basic commands console to manage files. For example, if we want to control an LED, we could enter the console (for our examples we have used Raspbian) and type the following:
echo 17 > /sys/class/gpio/export
With this line we would create a file with the corresponding GPIO structure so that it can be manipulated. Then should be configured as input or outputdepending on what you want. In our case as an output, since we want to power the LED to turn it on:
echo out > /sys/class/gpio/gpio17/direction
It is now possible to switch on or off. To do this we can give values to the output we have created. If we give it a value of 1 it will switch on and with a value of 0 it will switch off:
echo 1 > /sys/class/gpio/gpio17/value
echo 0 > /sys/class/gpio/gpio17/value
Once you finish the experiment, you can delete the entry to perform another exercise on the same or a different pin:
echo 17 > /sys/class/gpio/unexport
Simple, isn't it? Well, now you can use your imagination and use other commands you normally use with files and directories to manage GPIOs. For example, you can list the GPIOs you have active with:
You can also create Bash scripts (or whatever interpreter you use). For example, continuing with the LED example, you can create a script that you can run at any time without having to enter the commands one by one. To do this, open a text file and write the following content:
gpio mode 17 out
while true; do
gpio write 17 1
gpio write 17 0
Then save the text in a file called, for example, LED.sh and when you want to open it you only have to go to the directory where you have saved it and type this for its implementation:
If the above does not work, you may need to give it enforcement permissions to the LED.sh file. You can do this from the graphical environment or with chmod (chmod u+rwx LED.sh). And the result will be an LED that blinks every 1.3 seconds. Remember that the limit is your imagination.
Advanced GPIO programming
Well, it can get a bit more complicated for more sophisticated projects. Especially if we use programming languages to create our projects. Different tools and programming languages can be used. For example, the C programming language using libraries such as WiringPi, pigpio, sysfs, etc..., or compiling our own source codes with other languages such as C#, Ruby, Java, Perl, BASIC and Python.
Y if you don't know how to program, don't worryEven a child can program GPIOs using graphical languages such as those provided by Scratch, which is included in Raspbian by default, but you can install it on other distros without any problem and enjoy the "drag and drop" system with Scratch graphical blocks.
C and wiringPi
One of the best ways to create programmes in C programming language is by using the wiringPI library (if your distro doesn't have it installed, the first thing to do is to download and install it, as well as the gcc compiler or any other compiler you prefer). I will use gcc and the wiringPI library. To create our source code we must link the library.
If you don't have them installed, installs GCC:
sudo apt-get install gcc
And then do the same for la wiringPI library:
sudo apt-get install git-core
sudo apt-get update
sudo apt-get upgrade
git clone git://git.drogon.net/wiringPi
git pull origin
If, for example, we want to continue with our example of a LED that flickers, and we call it LED.c. In a text editor of your choice, such as gedit, you can write the source code and then save it with the name we have chosen. The content of our C program will contain the following lines:
/*Source code for flashing an LED from the GPIOs//
int main (void)
printf ("LED flashing");
if (wiringPiSetup () == -1)
pinMode (0, OUTPUT); //0 is the chip corresponding to pin 17
digitalWrite (0, 1); //Set to ON or turn it on
delay (1300); //Delay of 1.3 seconds
digitalWrite (0, 0); //Set to Off or power off
delay (1300); //Delay to turn on again....
For compile itFrom the directory where the saved source code is located, we can type the following:
gcc -o LED LED.c -lwiringPi
If you have chosen the pigpio library, use "-lpigpio". And for run the programme that we have compiled, will be done in a similar way to the script. But we will need privileges to start the execution of the binary called LED that we have generated:
Python is the preferred language for programming GPIOs.. The above libraries can also be used with Python. Raspbian already includes a module called RPI.GPIO to create Python scripts, but I recommend that you use and install pigpio, as these scripts can be run on Windows, Mac OS X and Linux. You just need the daemons running on the machine where you want to run the code.
Install it like this:
sudo apt-get install python-dev python-rpi.gpio
The source code to perform the typing of our Python LED is as follows (do the same as with the C code, you can edit it in a text editor):
pi = pigpio.pi()
pi.set_mode (17, pigpio.OUTPUT) #P Set pin 17 as output
pi.set_servo_pulsewidth (17, 1300) 1TP3We initiate pulses every 1.3 seconds to light the LED
pi.stop() #TWe end the program.
You owe it to save with the name LED and the extension ".py" and to run it, type in the terminal:
Scratch and GPIO
For using Scratch with GPIOs we can use different methods, as with the rest of programming languages. One is to use ScratchGPIO, RpiScratchIO, etc... But the first thing to do, if you already have Scratch and you will if you are using Raspbian, is to install the ScratchGPIO add-on:
sudo wget https://raw.githubusercontent.com/cymplecy/scratch_gpio/V5/install_scratchgpio5.sh -O in stall_scratchgpio5.sh
sudo bash install_scratchgpio5.sh
For example, if we have ScratchGPIO, we can create the following sketch to flash the LED:
If you are used to programming in BASIC or you are already a user of Parallax BASIC Stamp development boards (which use PBASIC to program the microcontrollers), you will find it easier to program the GPIO pins in BASIC language.
One of the possibilities is to use RTB (Return to Basic)to install it (in this case it is version 2.20):
sudo dpkg -i rtb-2.20-1.deb
sudo apt-get -f install
To run it, just type rtb in the terminal. And with it type the following source code renewed, but reminiscent of classic BASIC, and save it as LED.rtb:
PinMode (17, 1) //Pin 17 as output
DigitalWrite (17, 1) //Pin ON
WAIT (1.3) //Wait 1.3 seconds
DigtialWRite (17, 0) //Pin OFF
I hope this has been helpful. Any questions or opinions, leave your comments!