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

(singke) #1

frequency in the PWM() method) until you press Ctrl+C.


Detecting GPIO Input


Using the GPIO pins to detect input signals is a little bit trickier than using them for output. The
following sections walk through a couple different ways to handle digital input signals on the GPIO
pins. First, you need to set up the hardware you need for this project.


Setting Up the Hardware for Detecting Input


In this project, you’ll simulate a house with two doorbells: one for the front door, and one for the
back door. When someone is ringing one of the doorbells, the project will tell you which one, and it
will give you the opportunity to do some cool things with that information.


The following sections describe how to set up the hardware for the Pi Cobbler and Gertboard
environments.


Setting Up the Pi Cobbler for Input


For the doorbells, you need two push-button switches. You can use any type of switch you can find,
as long as it conducts when pushed and breaks the connection when released. You can get specialty
miniature push buttons that plug directly in to a breadboard, or you can use larger buttons and connect
them to your breadboard using wires.


Try It Yourself: Connect the Pi Cobbler Circuit
For this project, you need to start with the circuit you created for controlling GPIO
output in the previous section. In addition to that setup, you need just four additional
pieces of hardware: two push-button switches and two 1,000-ohm resisters. When you
have all the hardware you need, follow these steps:


  1. Connect one side of each push-button switch to the ground signal, using a 1,000-ohm
    resistor.

  2. Connect the other side of one push button to the GPIO 24 pin on the Pi Cobbler
    (marked #24) using a piece of wire.

  3. Connect the other side of the other push button to the GPIO 25 pin on the Pi Cobbler
    (marked #25) using a piece of wire.
    Figure 24.4 shows a diagram of what your final circuit should look like.

Free download pdf