The_Official_Raspberry_Pi_-_Beginner’s_Book_Vol1,_2018 (1)

(singke) #1

130 THE OFFICIAL RASPBERRY PI BEGINNER'S GUIDE


when clicked


forever


set gpio 25 to output high


wait^1 secs


set gpio 25 to output low


wait^1 secs


Click the green flag and watch your LED: it will turn on for a second, turn off for a second,
turn on for a second, and keep repeating that pattern until you click the red octagon to stop it.
See what happens when you click the octagon while the LED is in its on or off states.

LED control in Python
Load Thonny from the Programming section of the raspberry menu, then click the New button
to start a new project and Save to save it as Hello LED. To use the GPIO pins from Python,
you need a library called GPIO Zero. For this project, you only need the part of the library
for working with LEDs. Import just this section of the library by typing the following into the
Python shell area:

from gpiozero import LED

Next, you need to let GPIO Zero know which GPIO pin the LED is connected to.
Type the following:

led = LED( 25 )

CHALLENGE: CAN YOU ALTER IT?
How would you change the program to make the LED
stay on for longer? What about staying off for longer?
What’s the smallest delay you can use while still seeing
the LED switch on and off?
Free download pdf