Operation Manual
Although additional buses are present in the Raspberry Pi’s BCM2835 SoC processor, they are not brought out to the GPIO
port and are thus unavailable for use.
Using the GPIO Port in Python
With the theory out of the way, it’s time to get practical. In this section, you’ll learn how to install a library to allow easy access
to the general-purpose pins on the Raspberry Pi’s GPIO port in Python. You’ll also be shown two simple electronic circuits
which demonstrate how to use the GPIO port for input and output.
As you saw in Chapter 11, “Python Basics”, Python is a friendly yet powerful programming language. It’s not, however, the
perfect choice for every scenario. Although it works fine for the simple circuits you’ll be creating in this chapter, it does not offer
what is known as deterministic real-time operation. For the majority of users, this doesn’t matter; if you’re planning on using the
Pi at the heart of a nuclear reactor or a complex robotics platform, however, you may want to investigate a lower-level language
such as C++ or even assembler running on a dedicated real-time microcontroller.
If true real-time operation is required for your project, the Pi may be a bad choice. Instead, consider using a microcontroller
platform such as the popular open-source Arduino, or one of the MSP430 family of microcontrollers from Texas Instruments.
Both of these devices can interface with the Pi either through the GPIO header or over USB, and provide a specialised real-time
environment for control and sensing.
Installing the GPIO Python Library
Since the launch of the Pi, numerous developers have created software modules known as libraries for making full use of its
various functions. In particular, programmers have addressed the Pi users’ need to access the GPIO port without having to know
low-level programming.
These libraries are designed to extend the functionality of the base Python language, much like the pygame software described in
Chapter 11, “Python Basics”. Installing one of these libraries gives Python the ability to easily address the Pi’s GPIO port,
although it means that anyone planning to use the software you create will also have to download and install the library before it
will work.
There are several GPIO Python libraries available, but for the purpose of this section, we recommend that you use the
raspberry-gpio-python library, which was at version 0.2.0 at the time of writing. You can access this library from the
Google Code website at the following address: http://code.google.com/p/raspberry-gpio-python/
Although it’s possible to download the Python library through a web browser, it’s significantly quicker to do so through the
terminal as part of the installation process. Just follow these steps:
1. Open a terminal window on your Raspberry Pi from the Accessories menu, or use the console if you haven’t loaded a
desktop environment.
2. Type wget http://raspberry-gpio-python.googlecode.com/files/RPi.GPIO-0.2.0.tar.gz to download
the library to your home directory. If a newer version has been released, replace the version number—0.2.0—with the
current version.
3. Type tar xvzf RPi.GPIO-0.2.0.tar.gz to extract the contents of the file. This command is case-sensitive, so make
sure to type the capital letters.
4. Type cd RPi.GPIO-0.2.0 to change to the newly created directory. Again, if you downloaded a newer version of the
library, replace the version number with that of the downloaded version.
5. Type sudo python setup.py install to install the library into Python.
Although the GPIO library is now installed in Python, it won’t be loaded by default. Like pygame, the library needs to be
explicitly imported into your program. To use the library, start your program with import RPi.GPIO as GPIO at the top.
You’ll learn more about this in the following examples.
The Raspberry Pi’s GPIO port does not provide any protection against voltage spikes or electrical shorts. Always make sure you’ve checked that
your circuit is sound before connecting it to the Pi. If possible, use an isolation board such as the Gertboard (discussed in Chapter 13, “Add-on
Boards”) to provide protection.
Calculating Limiting Resistor Values
An LED needs a current limiting resistor to protect it from burning out. Without a resistor, an LED will likely only work for a short time before failing and