Home / Tutorials / Raspberry Pi Tutorial / Raspberry Pi GPIO Tutorial
GPIO LED output example

Raspberry Pi GPIO Tutorial

The Raspberry Pi is a Linux computer. But unlike most desktop and laptop Linux computers, users have access to a row of pins which can be used as inputs or outputs. These 40 pins are called GPIO (General Purpose Input Output) pins. This Raspberry Pi GPIO tutorial aims to help you program these pins for your purpose.

Raspberry Pi GPIO Pinout

The GPIO pins for the newer Raspberry Pi models (starting with B+) are laid out as follows:

Image result for why are raspberry pi pins out of order

Here we see two ways to identify a pin: by board name or by its Broadcom chip name (a.k.a. BCM). The numerically arranged names are the board names while the GPIO# are the BCM names. Unfortunately, the BCM names can never be arranged like the board names because of how the Raspberry Pi PCB is designed. From here on, I will refer to the board names by numbers only and the BCM pins by GPIO numbers.

The pins can be grouped into four main groups: Power, Ground, GPIO and EEPROM. The Raspberry Pi can provide 3.3V and 5V voltage sources on pins 1, 2 and 4. The ground pins are spread out to pins 6, 9, 14, 20, 25, 34 and 39.

The GPIO pins can be further grouped to normal, UART, SPI, and I2C. They are colored green, purple, blue and pink as per the diagram above. Note that all GPIO pins operate at 3.3V so stay away from devices that go beyond this voltage.

We will look closer on UART, SPI and I2C in separate tutorials. You might want to read my primers on UART, SPI and I2C as an introduction to these communication methods.

Finally, the EEPROM pins are used by HATS (hardware attached on top) for identification. Each HAT has their own EEPROM ID which is detected by the Raspberry Pi when attached. The RPi, in turn, uses the ID to automatically load the required driver.

You can view the GPIO pin names on the Raspberry Pi terminal using the command pinout. Here’s what it looks like:

Using pinout to view GPIO pins

Programming the GPIO Pins

Now that we are introduced to the Raspberry Pi GPIO pins, it’s time to make them useful!

There are several ways to program the Raspberry Pi GPIO pins but I found Python to be the easiest way. Specifically, we’ll be using the RPi.GPIO Python module which already comes with the latest Raspbian distribution.

We’ll connect a LED to GPIO17 of the Raspberry Pi like this:

GPIO LED output example

Next, we will open the Python terminal:

In order to use the GPIO module I mentioned, we need to import it like this:

Next, we need to specify if we will be referring to the pins by their board name or by their BCM name. This is done through:

Then we setup the pin as either output or input. Since we connected a LED to GPIO17, this would be the right call

Now to make GPIO17 high which will light up the LED, we use this command:

This is how we turn off the LED:

Once we’re done, we need to tell the Raspberry Pi that clear up the pin we just used:

If we don’t use the .cleanup() function, a warning is issued if ever we used the same GPIO pin again even if we have already exited the Python terminal:

You can disregard using GPIO.cleanup() and just use GPIO.setwarnings(False) to get rid of the warning message above. But using cleanup() is a good practice because it resets the pin to its default state. This would prevent damaging situations like a previously high pin connected to ground.

Python Script for Blinking a LED

Of course, you would want a single page program and not just commands typed into the terminal. You can write the commands above (with some add-ons) on a file:

This will bring up the Nano file editor.

Raspberry Pi Nano Editor

Then we type this Python script:

Press CTRL+X, press Y to save, then press enter to use the same file name.

To run the script, invoke Python:

The LED should now be blinking!

GPIO Blinking LED

Now we added some things on the script not used in the Python terminal. I’ll start with this:

This imports the time module which will be used to insert a delay between turning on and turning off the LED. The delay command for Raspberry Pi in Python is:

Where the number 1 is the delay in seconds.

Also, we need to create a loop to keep the LED blinking using

To stop the blinking, just use CTRL+C (Keyboard Interrupt)

Using GPIO Pin as Input

I think you now have an idea to make a Raspberry Pi GPIO pin as input. To make a pin as input, we will now use this:

To read an input pin, we use the GPIO.input(22) function which returns 1 if the input is high and 0 otherwise.

We will now attach a button to GPIO22 for this next example:

Raspberry Pi GPIO button example

This script turns on the LED when the button is pressed:

Similar to the previous script, we can exit this program using CTRL+C.

On the next part of my article, I will show how to control a servo motor using the Raspberry Pi. Thanks for reading!

Check Also

Raspberry Pi Zero Bluetooth

Setting Up Raspberry Pi Zero Bluetooth

The Raspberry Pi Zero W comes with both WiFi and Bluetooth connectivity. I have already …

Leave a Reply

Your email address will not be published. Required fields are marked *