Hardware Reference
In-Depth Information
Save the ile as gpiooutput.py . If you're using a Python development environment such
as SPE, don't try to run the program from within the editor. Most Raspberry Pi Linux distri-
butions restrict the use of the GPIO port to the root user, so the program will need to be run
using the command sudo python gpiooutput.py at the terminal to get it started. If all
has gone well, you should see the LED begin to blink on and of at regular intervals—and
you've created your irst home-made output device for the Pi.
If things don't work, don't panic. First, check all your connections. he holes in a breadboard
are quite small, and it's easy to think you've inserted a component into one row only to ind
it's actually in another. Next, check that you've connected the circuit to the right pins on the
GPIO port—with no labelling on the Pi itself, mistakes are unfortunately easy to make.
Finally, double-check your components—if the forward voltage of your LED is higher than
3.3 V or if your current limiting resistor is too large, the LED won't light up.
Although this example is basic, it's a good demonstration of some fundamental concepts. To
extend its functionality, the LED could be replaced with a buzzer to make an audible alert, or
a servo or motor as part of a robotics platform. he code used to activate and deactivate the
GPIO pin can be integrated into other programs, causing an LED to come on when new email
arrives or a lag to be raised when a friend has joined an IRC channel.
GPIO Input: Reading a Button
Being able to use the GPIO as an output is undeniably useful, but it becomes signiicantly more so
when you can combine that with one or more inputs. In the following example, you'll see how to
connect a push-button switch to another pin on the GPIO port and read its status in Python.
As with the earlier LED output example, this input example makes use of the Python GPIO
library. Assuming that you have this library installed, you can begin to build the circuit. (If
you haven't installed the Python GPIO library yet, skip back a few pages and follow the instal-
lation instructions.)
If you've already built the GPIO output example, you can either disconnect that from your Pi
or leave it connected—this example uses diferent pins, so both can co-exist quite happily. If
you do leave the previous example connected, make sure to use diferent rows on the bread-
board for the new components or you'll ind things don't work quite as planned.
Build the circuit as follows:
Insert the push-button switch into the breadboard. Most switches have either two or
four legs. You only need worry about two of the legs in the circuit. If the button has
four legs, they'll be set up in in pairs: check the push-button's data sheet to ind out
which legs are paired together.
Search WWH ::




Custom Search