You are on page 1of 4

Raspberry Pi GPIO

These pins are to communicate with other circuitry such as extension boards,
custom circuits and much more. You can make some pretty cool stuff by using
these pins. We will look into a lot more projects in the future that make use of
these pins.

There a few things that you should know before you start playing around with
pins, I cover all of these later on.

Warning: Experimenting with the GPIO is risky and has a chance of bricking
your Raspberry Pi. Knowing the power limitations of the GPIO pins and also
understanding what you’re plugging in is a must.


Equipment:

The equipment I use in this Raspberry Pi GPIO tutorial is listed below.

Raspberry Pi
SD Card or Micro SD Card if you’re using a Raspberry Pi 2, 3 or B+ (8 GB+
Recommended)
Ethernet Cord or Wifi dongle (Only if you’re controlling remotely)
A Raspberry Pi Case with access to the GPIO pins
100-ohm resistor
Red LED
GPIO Breakout Kit
Breadboard
Breadboard Wire


Raspberry Pi GPIO Tutorial:

Now let’s get started on this Raspberry Pi GPIO tutorial, we will be taking a look
at the different types of pins that available, how to enable the modules, what a
breakout kit is and finally build a very simple circuit.
If you’re an owner of a Raspberry Pi B+, 2, zero or 3, then you will have 40 pins
in total. The earlier models such as the Raspberry Pi B and similar all have 26
pins.
See below for the Raspberry Pi GPIO pinout diagram. We have included all three
iterations of the Pi for the pinout diagram. If you want a PDF version of the
pinout diagram, you can find it for downloaded from web.

As you can see there are more than just your standard pins there are some that
reference I2C, SPI, and UART.

GPIO is your standard pins that simply be used to turn devices on and off. For
example, a LED.
I2C (Inter-Integrated Circuit) pins allow you to connect and talk to hardware
modules that support this protocol (I2C Protocol). This protocol will typically
take up two pins.

SPI (Serial Peripheral Interface Bus) pins can be used to connect and talk to SPI
devices. Pretty much the same as I2C but makes use of a different protocol.

UART (Universal asynchronous receiver/transmitter) is the serial pins used to
communicate with other devices.

DNC stands for do not connect, this is pretty self-explanatory.

The power pins pull power directly from the Raspberry Pi.

GND are the pins you use to ground your devices. It doesn’t matter which pin you
use as they are all connected to the same line.

All this might be daunting at first, but it’s pretty easy once you get going. A lot of
the abbreviations and technical jargon is easy to turn people off straight away.


Configuring and using the Pins:

In this section, we will briefly touch on how to set up the pins so you can use
them on the Raspberry Pi. We will go lots more into actually programming and
using the pins in future Raspberry Pi projects.
In this example and future projects, we will be using Raspbian. If you haven’t got
it installed, then you can find out how to install the latest version of my Raspbian
installation guide.


Configuring GPIO:

If you’re on the latest version Raspbian, then you can start programming and
using the GPIO pins without needing to do any extra configuration. However, I
recommend that you update your Pi to the latest packages anyway. If you haven’t
done this, then you can do it by running the following commands:

sudo apt-get update sudo apt-get upgrade

If you don’t have it installed you simply run the following command:
sudo apt-get install rpi.gpio


Configuring Raspberry Pi I2C:

Setting up the I2C pins on the Raspberry Pi is super easy and will only take a
couple of minutes to do.
Firstly, go to the Raspi-Config tool by entering the following command.
sudo raspi-config

In here go to advanced options and then to I2c, enable I2c by pressing yes.
The Pi should now alert you that I2C will be enabled after reboot. It will then ask
if you want it to be loaded by default. Select yes if you plan on using I2c every
time the Raspberry Pi boots up.

Now we want to make sure it has successfully enabled the necessary modules. To
do this enter the following command:

lsmod | grep i2c_

This command will return any modules that are running starting with i2c. It
should return something like this: i2c_BCM2708.


Configuring Raspberry Pi SPI:

Configuring the Raspberry Pi SPI is much like the I2c and is super easy, it will
only take a couple of minutes to do.

Firstly, go to the Raspi-Config tool by entering the following command.

sudo raspi-config

Once the config tool has loaded, go to advanced options and then to SPI.
Enable SPI, and it will tell you that it will be enabled after reboot. After that, it
will ask if you want it to be loaded by default. Only select yes if you plan on using
it every time the Pi boots up.

To check that SPI is successfully up and running use the following command:
lsmod | grep spi_

This command will return any modules that are running starting with SPI. It
should return something like spi_bcm2835. Make sure you have restarted your
Pi before checking to see if the module has been loaded.


Using a Breakout Kit:

A breakout kit allows you take all the pins via a ribbon cable and connect them to
a breadboard or a different device. This kit makes it a lot easier and safer than
working in and around the Raspberry Pi.
There are a few different types of breakout kits you’re able to buy for the
Raspberry Pi GPIO pins. Personally, I prefer the T type as they are easy to read
and use. If you’re looking for a breakout kit to buy, then be sure to check out the
equipment list above.
When connecting the ribbon cable, you need to make sure it is connected, so it is
facing away from the board. You can see an example of a correct setup ribbon
cable with a T type breakout board right below.


Programming with the Raspberry Pi GPIO Pins:

Programming with the GPIO pins on the Raspberry Pi is typically done using the
programming language Python. This particular circuit and code is super easy to
get going and won’t take you long at all to complete.
If coding sounds like it may be a little too involved for you and would prefer a
simple solution, then something like Raspberry Pi Cayenne might interest you.
The software package allows you to add sensors, control GPIO pins and lots more
with a super easy user interface.
If you’re happy to code and learn lots about Python, then check out my example
below.

Firstly, let’s set up our little circuit, I have a nice easy diagram to follow below. If
you have a breakout kit, the circuit will obviously be a bit different since your
wires will come from the cobbler. Alternatively, simply connect the positive end
of a LED up to pin 7 and the negative end to a resistor that connects to a ground
pin.

Now let’s create a python file so we can type out our python script.

sudo nano led_blink.py

Now let’s write out a little program. It’s important to remember python is
whitespace sensitive. For example, in the for loop make sure you have four
spaces for anything within the loop. To explain a little further, the GPIO.output(7,
true) line will need to have four spaces before it.

#import the GPIO and time package import RPi.GPIO as GPIO import time
GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) # loop through 50 times,
on/off for 1 second for i in
range(50): GPIO.output(7,True) time.sleep(1) GPIO.output(7,False) time.
sleep(1) GPIO.cleanup()

Now exit and save by pressing ctrl+x and then y.

Now if you run our little script, the circuit should come to life. You can run a
python script by doing the following command. It is also important that you run
it as the superuser using sudo.

sudo python led_blink.py

I hope this tutorial has helped introduce you to the basics of GPIO pins.

You might also like