Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
TABLE 24.2 The Gertboard Pin Block Layout

The three-pin J7 block is crucial. You must place a jumper between the middle pin and the upper pin,
labeled 3V3, to power on the Gertboard. Without it, none of your projects will work.


Also inside the circuit are two sets of 12 pins labeled B1 through B12. One set is for using the
buffered inputs, and the other set is for using the buffered outputs. Consult the Gertboard manual for a
complete description of how to use the buffered input and output pins.


Using the RPi.GPIO Module


To interface your Python programs with the GPIO signals, you have to use the RPi.GPIO module.
The RPi.GPIO module uses direct memory access to provide an interface to control the GPIO
signals. The following sections walk through the basics of the RPi.GPIO module.


Installing RPi.GPIO


At this writing, the Raspberry Pi installs the Python v2 version of the RPi.GPIO module by default
(called python-rpi.gpio). To use Python 3 programs, you have to install the Python v3 version
of the module from the software repository, like this:


Click here to view code image


sudo apt-get update
sudo apt-get install python3-rpi.gpio

After you install the module, you can test to make sure it’s installed, as in the following example:


Click here to view code image


pi@raspberrypi ~ $ python3
Python 3.2.3 (default, Mar 1 2013, 11:53:50)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import RPi.GPIO as GPIO
>>>

Because of the long module name, it has become somewhat of a default standard to use the GPIO alias
when importing the RPi.GPIO module. This hour uses that convention.


When you have the RPi.GPIO module installed for Python v3, you’re ready to start experimenting!


Startup Methods


You need to know only a handful of basic methods in order to access the GPIO pins. Before you can
start interacting with the interface, you have to use the setmode() method to set how the library
will reference the GPIO pins:


GPIO.setmode(option)
Free download pdf