Hardware Reference
In-Depth Information
Creating the LEDblink Python Code
he irst part of the project is to write the code that will make your LED blink.
For a video that walks you through the LEDblink project, visit the companion
website at www.wiley.com/go/adventuresinrp . Click the Videos tab and
select the LEDblink ile.
1. Open Python IDLE 3 to program the GPIO pins, and select File New Window
to create a blank text editor window to write your Python code to control the
GPIO pins.
Type the following code into your text editor window:
import RPi.GPIO as GPIO
import time
hese two lines import the modules and their functions that you will need to
control the GPIO pins on the Raspberry Pi, and to create timed delays between
the LED turning on and of. (You used the time module in Adventure 5 to wait
for user input in the inventory program and the text-based adventure game.)
2. Underneath those lines, set the mode of pin numbering you are going to use,
either BCM or BOARD (see the “Digging into the Code” sidebar for more details).
You are then able to set up the individual pins on the Raspberry Pi.
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.OUT)
In this project, you are outputting to an LED. herefore you need to set up the
pin that the LED connects to on the Raspberry Pi as an output. To do this, use
the command GPIO.setup( the GPIO number , GPIO.OUT) .
3. Next, use a while True loop to set the output of GPIO 24 to True , which will
turn on the LED followed by a pause for one second. hen set the output of
GPIO 24 to False , followed by another one-second pause. When this is looped
over and over, the LED will repeatedly turn on (true) and of (false) with a one-
second delay between each.
while True:
GPIO.output(24, True)
time.sleep(1)
GPIO.output(24, False)
time.sleep(1)
4. Save the ile as LEDblink.py in Documents .
Search WWH ::




Custom Search