We will jump right into the code:
- Create a new file in Thonny, and call it
Hello LED.py
or something similar. - Type in the following code and run it:
from gpiozero import LED led = LED(18) led.blink(1,1,10)
If we wired up our circuit and typed in our code correctly, we should see our LED blink for 10 seconds in 1 second intervals. The blink function in the gpiozero LED
object allows us to set on_time
(the length of time in seconds that the LED stays on), off_time
(the length of time in seconds that the LED is turned off for), n
or the number of times the LED blinks, and background
(set to True
to allow other code to run while the LED is flashing).
The blink
function call with its default parameters looks like this:
blink(on_time=1, off_time=1, n=none, background=True)
Without parameters passed into the function, the LED will blink non-stop at 1 second intervals. Notice how we do not need to import the time
library like we did when we used the RPi.GPIO
package for...