Adafruits Raspberry Pi Lesson 4 Gpio Setup PDF
Adafruits Raspberry Pi Lesson 4 Gpio Setup PDF
GPIO Setup
Created by Simon Monk
Guide Contents 2
Overview 3
The GPIO Connector 5
Adafruit Pi Code 7
Configuring GPIO 11
Configuring I2C 13
Installing Kernel Support (with Raspi-Config) 13
Installing Kernel Support (Manually) 16
Testing I2C 18
Configuring SPI 20
Test and Configure 22
The GPIO connector actually has a number of different types of connection on them. There
are:
True GPIO (General Purpose Input Output) pins that you can use to turn LEDs on and
off etc.
I2C interface pins that allow you to connect hardware modules with just two control
pins
SPI interface with SPI devices, a similar concept to I2C but a different standard
In addition, some of the pins can be used for PWM (pulse Width Modulation) for power
control and another type of pulse generation for controlling servo motors called PPM (Pulse
Position Modulation).
In this tutorial, you are not actually build anything, but you will learn how to configure your
Raspberry Pi and install useful libraries ready to start attaching some external electronics to
it.
This tutorial is written for Raspbian & Raspbian-derived installations (like Occidentalis) only
As well as supplying power (GND, 3.3V and 5V) all the GPIO pins can be used as either
digital inputs or outputs. The pins labelled SCL and SDA can be used for I2C. The pins
labelled MOSI, MISO and SCKL can be used to connect to high speed SPI devices.
All the pins have 3.3V logic levels and are not 5V-safe so the output levels are 0-3.3V and
the inputs should not be higher than 3.3V. If you want to connect a 5V output to a Pi input,
use a level shifter (http://adafru.it/aM5)
A popular way to actually make the connections to the Raspberry Pi is to use a Pi Cobbler.
This uses a ribbon cable to connect the GPIO connector to solderless breadboard, where
you can add your own components.
To fetch this code, you need to use some software called 'git'. This comes pre-installed on
Occidentalis, but on Raspbian you must install it by entering the following commands into
LX Terminal.
The update may take a while, especially if this is the first time you have run it on your Pi.
Eventually it should give you another command prompt '$' and it will be ready for you to
type the next command which is:
sudo apt-get install git
Once git is installed (if its not already there) you can "check out" the Adafruit Pi Python
repository onto your Pi using the following commands
If there is any problem during any of the steps above, you will see an error message. The
most common reasons why something should fail to install are:
If you use a pin as a digital input, then you can connect switches and simple sensors to a
pin and then be able to check whether it is open or closed (that is, activated or not)
http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds (http://adafru.it/aJ5)
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-
pi (http://adafru.it/aTD)
http://learn.adafruit.com/basic-resistor-sensor-reading-on-raspberry-
pi (http://adafru.it/aTE)
The installation process for this is the same whether you are using Raspbian or
Occidentalis. In actual fact, some versions of Raspbian include this library, but these
instructions will also have the effect of updating to the latest version, which is worth doing.
To install RPi.GPIO, you first need to install the Python Development toolkit that RPi.GPIO
requires.
Thats all there is to it. You are ready to try some of the projects I mentioned at the top of
this section.
Here are some of the Adafruit projects that make use of I2C devices and modules:
http://learn.adafruit.com/mcp230xx-gpio-expander-on-the-raspberry-
pi (http://adafru.it/aTF)
http://learn.adafruit.com/adafruit-16x2-character-lcd-plus-keypad-for-raspberry-
pi (http://adafru.it/aTG)
http://learn.adafruit.com/adding-a-real-time-clock-to-raspberry-pi (http://adafru.it/aPm)
http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-
pi (http://adafru.it/aPj)
http://learn.adafruit.com/mcp4725-12-bit-dac-with-raspberry-pi (http://adafru.it/aPh)
http://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-
pi (http://adafru.it/aPi)
http://learn.adafruit.com/using-the-bmp085-with-raspberry-pi (http://adafru.it/aPg)
The I2C bus allows multiple devices to be connected to your Raspberry Pi, each with a
unique address, that can often be set by changing jumper settings on the module. It is very
useful to be able to see which devices are connected to your Pi as a way of making sure
everything is working.
To do this, it is worth running the following commands in the Terminal to install the i2c-tools
utility.
We also recommend going through the steps below to manually check everything
was added by raspi-config!
i2c-bcm2708
i2c-dev
like so:
If you do not have this file then there is nothing to do, however, if you do have this file, you
need to edit it and comment out the lines below:
.. then edit the file so that it appears as below, and then save and exit the file using CTRL-x
and Y.
If you are running a recent Raspberry Pi (3.18 kernel or higher) you will also need to update
the /boot/config.txt file. Edit it with sudo nano /boot/config.txt and add the text
dtparam=i2c1=on
dtparam=i2c_arm=on
sudo reboot
Testing I2C
Now when you log in you can type the following command to see all the connected devices
sudo i2cdetect -y 1
Note that if you are using one of the very first Raspberry Pis (a 256MB Raspberry Pi Model
B) then you will need to change the command to:
sudo i2cdetect -y 0
The Raspberry Pi designers swapped over I2C ports between board releases. Just
remember: 512M Pi's use i2c port 1, 256M ones use i2c port 0!
blacklist spi-bcm2708
Next time you log in you can check that you can see the devices with
ls -l /dev/spidev*
you should see two 'devices' one for each SPI bus
http://learn.adafruit.com/raspberry-pi-e-mail-notifier-using-leds (http://adafru.it/aJ5)
http://learn.adafruit.com/playing-sounds-and-using-buttons-with-raspberry-
pi (http://adafru.it/aTD)
http://learn.adafruit.com/matrix-7-segment-led-backpack-with-the-raspberry-
pi (http://adafru.it/aPj)