Hardware Reference
In-Depth Information
As before, you will set a GPIO for output (GPIO 23). You also need to set a GPIO pin to
detect input . Use GPIO 24 for this purpose.
while True:
if GPIO.input(24):
GPIO.output(23, True)
else:
GPIO.output(23, False)
time.sleep(0.1)
In the previous project, you used a while True loop to repeatedly turn an LED on
and of with a one-second interval. In this project, you only want the LED to turn on
when the button is pushed; therefore, you need to introduce a condition. You use if
to set the condition: if the button (GPIO.input(24)) is pressed, then turn the LED (GPIO.
output(23)) on (or true). But this is only one part of the condition. You also need to set
the conditions for the button not being pushed; what should the LED do then? For
this part of the condition, you use else : else the LED should be of (or false) .
Save the ile as buttonLED.py in Documents .
Connecting the buttonLED
Components
As with the program for making an LED blink, before you run your button LED pro-
gram you need to assemble the electronic components and connect them to the
Raspberry Pi GPIO pins. If you use the component coniguration from the previous
exercise, you will only need to add a few extra parts. Figure 8-10 shows the Raspberry
Pi on the left and a breadboard on the right. Follow this diagram to help you connect
the right cables and components in the circuit.
1. Leaving the original circuit complete, take a small button switch and place it
across the ride in the centre of the breadboard, with two legs in holes on column
E and two legs in holes in column F on rows 21 and 23, as shown in Figure 8-10.
2. When the button is in place, take a 10k resistor and push one end into D21 next
to one of the button legs. Place the other end of the resistor into a hole in the
blue (negative) power column of the breadboard.
3. Next take a male-to-male jumper cable (green cable) and place one end into A23
on the same row as the other leg of the button on column E. Place the other end
into the red (positive) power column of the breadboard.
4. Connect a male-to-female jumper cable (yellow cable) from A21 on the bread-
board to GPIO pin 24 on the Raspberry Pi.
5. Finally, add a male-to-female jumper cable (black cable) from the red column
near the top of the breadboard to the top GPIO pin 3V3, which will power the
circuit.
Search WWH ::




Custom Search