Hardware Reference
In-Depth Information
Using a Button to Turn on an
LED
So far, you have controlled an LED, which is an output device. In this exercise you will
add an input device in the form of a button that will start the light sequence when
pushed.
For this project, you need your Raspberry Pi plus the following items:
•
A breadboard
•
Six jumper cables
•
A simple push button
•
An LED
•
A 330 ohm resistor to protect the LED
•
A 10k ohm resistor for use with the push button
For a video that walks you through the LEDbutton project, select the buttonLED
Creating the buttonLED Python Code
In Python IDLE 3, amend your
LEDblink
program to include the following lines
(highlighted in bold):
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
import time
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.IN)
CHALLENGE
Why did you add
time.sleep(0.1
) to the
while
loop in the
buttonLED.py
program? What might happen if you remove it? Have a go to ind out!