32: print('End of program')
The script2405.py code registers two events—one for each button. In this project, the code goes
into a loop and does nothing while it waits for a button to be pressed (lines 27 through 31). You can
easily incorporate other features in the loop, such as checking the temperature. (See Hour 20 for a
refresher on using the urllib module to read temperatures from a webpage.)
By-the-Way: Reducing Switch Bounce
You may have noticed when testing the input project that sometimes using push-button
switches can be a bit touchy (such as triggering two separate contacts with one button
push). This is commonly called switch bounce. You can reduce switch bounce by
adding a capacitor across the switch inputs. You can also control switch bounce by
using software: The add_event_detect() method has a bouncetime
parameter that you can add to set a timeout feature that helps with the switch bounce
problem.
Now that you know the basics of working with input and output from the GPIO interface, you can
create many applications. You can mix and match which pins you use for input and output, creating
complex projects that detect input and send output based on the inputs.
Summary
This hour explores the GPIO interface on the Raspberry Pi. You worked on a project that outputs a
digital signal to a GPIO pin, as well as a project that outputs a PWM signal you can use to control
motors. You also worked a project to read input values from the GPIO pins, which allows you to
detect switch presses. You can use these concepts to control any type of electronic circuit, from
reading temperatures to running robots!
Q&A
Q. How do you control the analog-to-digital (A/D) and digital-to-analog (D/A) converters on
the Gertboard?
A. You can connect the A/D and D/A converters directly to GPIO pins and then either send
outputs to the GPIO pins to generate an analog voltage in the D/A converter or read inputs from
the GPIO pins to detect the A/D voltage.
Q. Can you use the ATmega microcontroller on the Gertboard from the Raspberry Pi?
A. Yes. The popular Arduino Integrated Development Environment (IDE) package has been
ported to the Raspberry Pi, so you can run Arduino programs directly from your Raspberry Pi.
Workshop
Quiz
1. What RPi.GPIO method should you use to set a GPIO signal to use for output?
a. setmode(GPIO.BCM)