All about Raspberry Pi GPIOs

GPIO Raspberry Pi

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)

GPIO Layout Model B Rev1

Model A/B (Rev 2.0)

GPIO Layout Model A and B Rev2

Model A+, B+, 2B, 3B, 3B+, Zero and Zero W

GPIO Layout Model A+, B+ and 2

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

HAT for Raspberry Pi

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.

Raspberry Pi with HAT

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

Connecting the LED to the Raspberry Pi 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:

cd /sys/class/gpio

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:

source gpio
gpio mode 17 out
while true; do
gpio write 17 1
sleep 1.3
gpio write 17 0
sleep 1.3

Then save the text in a file called, for example, 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 file. You can do this from the graphical environment or with chmod (chmod u+rwx And the result will be an LED that blinks every 1.3 seconds. Remember that the limit is your imagination.

Advanced GPIO programming

Programming languages

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://

cd wiringPi

git pull origin

cd wiringPi


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//

#include <stdio.h>
#include <wiringPi.h>

int main (void)
printf ("LED flashing");
if (wiringPiSetup () == -1)
return 1;
pinMode (0, OUTPUT); //0 is the chip corresponding to pin 17
for (;;)
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....
return 0;

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:

sudo ./LED


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):

#!/usr/bin/env python

import time
import pigment
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:

sudo pigpiod

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 -O in
sudo bash

For example, if we have ScratchGPIO, we can create the following sketch to flash the LED:

Scratch sketch for LED flickering


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):

cd /tmp
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:

//Titilar LED

PinMode (17, 1) //Pin 17 as output
DigitalWrite (17, 1) //Pin ON
WAIT (1.3) //Wait 1.3 seconds
DigtialWRite (17, 0) //Pin OFF
WAIT (1.3)

I hope this has been helpful. Any questions or opinions, leave your comments!

64 thoughts on “Todo sobre los GPIO Raspberry Pi”

  1. Great information! Thank you very much for this detailed explanation.

    I am just getting interested in getting started with Rasperry PI. I have previously made some transient inventions but without sufficient knowledge of electronics and I would like to complement them with this system. I don't have much technical knowledge of electronics (I'm interested in going deeper) and having discovered this I can imagine how many uses I could give to this system and in all the places where I could apply it. For this reason I would like you to recommend me if there are any manuals or advanced tutorials on the use of these functionalities to be able to understand it in depth and apply it in future projects.

    On the other hand, could an error in these settings cause the system to start malfunctioning? I ask this because I have this system as a server but I would like to experiment with it.

    Can I have two separate configurations using different MicroSD so that when I want to do one or the other I turn on the rasperry with the corresponding MicroSD? One MicroSD to create new projects and another to use it as a Server.

    I hope you can answer these questions.
    Greetings, I'll be reading it.

    1. Alberto Navarro

      Good morning Mariano!

      Well, apart from what we have published on the web about the subject

      I can recommend you to visit the official website of the Raspberry Pi Foundation where you will find a lot of official info about this wonderful board.

      Yes, to avoid problems I would recommend you to do installations in different uSDs, it can be done without problems.


  2. Hello.
    I am using a Raspberry 3 as a media center. The OS on it allows me to "turn it off" via software, but the board stays powered, as a red led stays on right next to the micro-USB.
    My question is: when I turn off the Raspberry in this way, do the 5V GPIO pins still have voltage on them?


    Hello, since I am a layman and this is the only place I have found where I can ask this question and if it is not the right place I apologise and thank you also for the help you offer.
    I have been using a rasp 3b with osmc as a multimedia centre for some time but the audio delivered through the HDMI output does not seem to me to be of very good quality, in fact even my old desktop computer with its integrated card on the board has better sound than the rasp 3.
    I would like to change this but although I have seen a few cards (usb or directly through the gpio port or a connection that I think also goes through this that they call 'I2S' but without using more than 3 pins (within these some with amplification) but I do not know if the change will be noticeable when putting one of these cards, nor what type to put in case it is noticeable and if it is necessary to have programming knowledge to be able to put one.
    Best regards and thank you.

Leave a Comment

Your email address will not be published.

Scroll to Top