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

(singke) #1

Hour 24. Working with Advanced Pi/Python Projects


What You’ll Learn in This Hour:
Working with the GPIO interface
Exploring the Python RPi.GPIO module
Using the GPIO for output
Using the GPIO for input

One of the exciting features of the Raspberry Pi is the GPIO interface, which allows you to connect
your Raspberry Pi to electronic circuits and then interact with the outside world. In this hour, you’ll
learn about the GPIO interface and how to use it to both accept input and send output to electronic
circuits. This hour you’ll use two popular Raspberry Pi electronic interface devices for the projects:
the Pi Cobbler and the Gertboard.


Exploring the GPIO Interface


One of the features included in the Raspberry Pi that you don’t often see in consumer computers is the
General Purpose Input/Output interface (called the GPIO interface for short). The GPIO interface is
the key to getting your Raspberry Pi to interact with the outside world. You can use it to control all
sorts of electronics, from temperature gauges to robots. In the sections that follow, you’ll take a look
at the Raspberry Pi’s digital interface and what you need to interact with it.


What Is the GPIO Interface?


The GPIO interface provides direct access to the Broadcom chip on the Raspberry Pi, which includes
several built-in digital interface features:


17 digital input/output (I/O) pins
A pulse-width modulation (PWM) output
An Inter-Integrated Circuit (I2C) interface
A Serial Peripheral Interface (SPI) connection
A Universal Asynchronous Receiver/Transmitter (UART)

The 17 digital I/O pins allow you to read high or low digital signals from up to 17 separate devices
or send up to 17 high or low digital signals to external devices—or some combination of the two.
These signals can be used for controlling relays to turn circuits on or off or send signals to trigger
devices (such as turn on your coffeemaker).


The PWM output is used to control the speed of electric motors. You can control the PWM signal to
make a motor stop, start, speed up, or slow down.


The I2C and SPI interfaces provide a digital communications protocol for interfacing with integrated
circuits. This protocol allows you to connect advanced microcontrollers, such as the Atmel ATmega
microcontroller chip, made popular in the Arduino hobbyist unit.


Finally, the GPIO interface provides access to the UART pins on the Broadcom chip. The UART pins
allow you to connect a serial device (such as a terminal or modem) to your Raspberry Pi.

Free download pdf