duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 2:27 am

I am using an LED to indicate that my security system program is running. I would like the LED to have a short blink (a bit longer than a flash) then a pause for 2-3 seconds. All I can find is

Code: Select all

from gpiozero import LED
from signal import pause

red = LED(17)

red.blink()

pause()
Where the "red.blink(1)" would blink the LED one second on, one second off. How do I change each half of the cycle?
I have also tinkered with this:

Code: Select all

while True:
    red.on()
    sleep(1)
    red.off()
    sleep(1)
and changed the values inside the () but I get lots of errors.

I just want a short on, long off cycle. Is there another library I should consider?
You can't know your limits until you exceed them.

pcmanbob
Posts: 6256
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 6:13 am

There are several ways to do it using the code examples you posted , for a constantly flashing LED at say 0.5 second intervals then you could use

Code: Select all

 from gpiozero import LED
from signal import pause

red = LED(17)

red.blink(0.5)

pause()
this gives 0.5 second flash constantly

for a 0.5 second flash once every 2 seconds then

Code: Select all

from gpiozero import LED
from time import sleep

red = LED(17)

while True:
    red.on()
    sleep(0.5)
    red.off()
    sleep(2)
  
  

you could also do this using RPi.GPIO

Code: Select all

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT)

while True:
	GPIO.output(17, GPIO.HIGH)
	time.sleep(0.5)
	GPIO.output(17, GPIO.LOW)
	time.sleep(2)
	
	

All code tested using python 3
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 9:31 am

I thought I had tried something like the

Code: Select all

from time import sleep

red = LED(17)

while True:
    red.on()
    sleep(0.5)
    red.off()
    sleep(2)
but I guess I didn't. Maybe I did the times in the red.on and red.off steps. I will try this again. Thanks!
You can't know your limits until you exceed them.

User avatar
B.Goode
Posts: 7876
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 9:59 am

Reading the documentation for gpiozero shows that the ON and OFF times for LED.blink() can be specified separately: https://gpiozero.readthedocs.io/en/stab ... t.html#led
blink(on_time=1, off_time=1, n=None, background=True)

Make the device turn on and off repeatedly.
Parameters:

on_time (float) – Number of seconds on. Defaults to 1 second.
off_time (float) – Number of seconds off. Defaults to 1 second.
n (int or None) – Number of times to blink; None (the default) means forever.
background (bool) – If True (the default), start a background thread to continue blinking and return immediately. If False, only return when the blink is finished (warning: the default value of n will result in this method never returning).

User avatar
davidcoton
Posts: 3811
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 3:47 pm

pcmanbob wrote: for a 0.5 second flash once every 2 seconds then

Code: Select all

from gpiozero import LED
from time import sleep

red = LED(17)

while True:
    red.on()
    sleep(0.5)
    red.off()
    sleep(2)
[pedant]
That will give a 0.5s flash every 2.5s (0.5 on, 2.0 off)
For 0.5s flash every 2.0s use 0.5 on, 1.5 off
[/pedant]
But better to use B.Goode's suggestion:

Code: Select all

from gpiozero import LED

red = LED(17)
red.blink(0.5, 1.5)
which will blink "forever" in a background thread, 0.5s on in every 2.0s
EDIT: proper attribution of quote
Last edited by davidcoton on Tue Jun 04, 2019 3:58 pm, edited 1 time in total.
Signature retired

User avatar
B.Goode
Posts: 7876
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 3:55 pm

{Original content overtaken by events, removed.}
Last edited by B.Goode on Tue Jun 04, 2019 4:08 pm, edited 1 time in total.

User avatar
davidcoton
Posts: 3811
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 4:02 pm

Deleted 'cos no longer relevant.
Last edited by davidcoton on Tue Jun 04, 2019 8:07 pm, edited 1 time in total.
Signature retired

User avatar
B.Goode
Posts: 7876
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 4:07 pm

{Original content overtaken by events, removed.}
Last edited by B.Goode on Wed Jun 05, 2019 5:51 am, edited 1 time in total.

duckredbeard
Posts: 46
Joined: Mon Apr 29, 2019 1:53 am

Re: Short blink/long pause LED with gpio zero

Tue Jun 04, 2019 11:52 pm

Thanks everyone for their replies. I decided to go with

Code: Select all

red = LED(17)
red.blink(0.08, 3)
and seems a good balance. I even had the on cycle shorter, but it doesn't bright up if you make the duration too short.
You can't know your limits until you exceed them.

Return to “Beginners”