Cart 0

Ryanteck Traffic HAT - Blinking LED

Blinking an LED in python has become really easy due to the new GPIO Zero library. There's a few different ways.

Lets begin by just turning the amber LED on. Open up your favourite python editor and type in the following to turn it on.

from gpiozero import LED

 

amber = LED(23)

 

amber.on()

And then try "amber.off()" to turn it off.

We can make the LED now blink by adding the following code at the top

from time import sleep

And as the body

while True:

    amber.on()

    time.sleep(1)

    amber.off()

    time.sleep(1)

This will then turn the LED off and on with a 1 second interval.

Congratulations you're now blinking a LED!

  • But wait, there's more!*

Instead a fancy feature of GPIO Zero is auto blinking which can be done using the following command.

amber.blink(1,1,None,False)

This will make the LED blink forever with a 1 second interval and pause the program.

We can change the False to True (or remove the ",False") section to make it so the rest of the program would continue.

You can change "None" to be a number which is how many times it would blink, Replacing "None" with 10 would make it blink just 10 times.

Finally you can change the two beginning numbers to set the intervals, using "1,2" would make the LED turn on for one second and then off for 2.



Older Post Newer Post

Related Posts

Using GPIO Zero with the PumpkinPi
Using GPIO Zero with the PumpkinPi
    It's that time of year... the ghosts and ghouls are out in force, pumpkins are being carved and witches are prepa...
Read More
Using a UBEC with a Raspberry Pi
Using a UBEC with a Raspberry Pi
Hooking up your UBEC to your Raspberry Pi really couldn't be easier. Simply plug in the 3 pin socket to your Raspberr...
Read More
How to lock your Raspberry Pi Screen
How to lock your Raspberry Pi Screen
Many of you will be familiar with the Windows screensaver/lock screen.  If you are going to leave your PC alone for a...
Read More