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. Hi, I'm new to this, and I was starting with the led example, with C mode and wiringPi. Once I open the terminal to write the last code and run the program, I get the message that it can't find the file or directory.

    What can I do?

    Thank you

  2. Americo Da Rosa

    Hi. I want to use a Raspberry Pi, to display messages on a LCD monitor or TV through the HMI. I can't find where to start. Like the ones in the reception of hospitals. Please, thank you to guide me on what application or applications I should look at.

    1. Alberto Navarro

      Hello Americo!

      Research through the Kodi system, as far as multimedia is concerned, it is state of the art.


  3. Hi Isaac, I wanted to ask you if it was possible to connect a hard disk through the gpio to avoid using a USB port on the raspberry pi 3b.

    1. Very good,

      There are hubs to expand the amount of USBs that has the Raspberry Pi, I for example have one of 4 USBs to connect keyboard, mouse and external hard drive. But if what you want is to get down to work and do something more sophisticated from the GPIOs, I'm sorry to say no, since the USB transfer requires high impedance configurations, and a controller for the frequency at which they work, etc., in addition to having to stick to the standard voltages...

      Greetings and I hope I have helped you.

      1. Thank you very much for your help and a very clear answer, I will have to look for a hub.

  4. Hello!
    I've been trying to get to grips with the raspberry for a few days to control a stepper motor with it. I have a raspberry pi 3 and a nema 17 motor. I'm trying to control the motor through a drv8833 drive but no matter how hard I try I don't know why I can't get it to move xd.
    If someone could help me I would be very grateful.
    Best regards!

    1. Hello Alvaro,

      Well, we would like to be able to help you, and for that we need more information. A priori, with what you tell me I could suggest that you check if the connections are correctly made (and on the right pins) and if the code for the manipulation is correct. I imagine that the motor and the driver are working properly, I don't know if you have an Arduino board to test them, but it would also be important to make sure that they are ok.

      If you can provide more information, we may be able to help you more...

      Best regards, and thank you for following us.

  5. Jorge Piovano

    Good I have the raspbraspberry Pi3-B 1.2gb. The query dque I have is how to connect a button to start it. I have currently I can the push button and the pin 5y6 and walks but generates me problems in recalbox that to start many times it shuts down.
    The idea is to test in some position that I just start but do not switch off.
    How can I do
    Thank you

    1. Hello Jorge,

      I assume that you have done all the connection correctly for your button and you have created a script that allows you to shut it down when you click on it (e.g. with the sudo shutdown command) and you have tried some option for powering it from the GPIOs.
      Well, but I have to tell you that both the Raspberry Pi 2 and the Raspberry Pi 3 have special connections for that. In the case of the PI 3 there are two little holes right between the GPIO and USB connectors on the board. There you can solder or connect the cables with a push button. You will see that they are two small holes with an inscription on the board that says RUN.
      It's super simple, just put the push button there that bridges both connectors and the computer starts up, just like a PC does... Also, if you press the button during power-up you can also trigger a reset to reboot.

      Best regards and I hope I have helped you.

Leave a Comment

Your email address will not be published.

Scroll to Top