Python Forum
gpiozero button turn off LED that is already on
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
gpiozero button turn off LED that is already on
#1
I have a Raspberry Pi on my desk. It is wired to an LED and a button. My phone sends an SSH to the Pi when it gets a text message. The SSH runs this script to turn the LED on:

from gpiozero import LED import time from time import sleep pink = LED(20) pink.on() sleep(900)
The "sleep(900)" keeps the LED on for 15 minutes. I also have a function on my phone that sends an SSH to the same Pi to run this script, effectively cancelling the LED.

from gpiozero import LED import time from time import sleep pink = LED(20) pink.off()
What I want is to press a button connected to GPIO17 that either turns the LED off or runs the script to cancel the LED. I have tried this, but it does not work:

from gpiozero import LED, Button from signal import pause led = LED(20) button = Button(17) button.when_pressed = led.off pause()
I have confirmed that the button is open when relaxed, closed when pressed. I have confirmed that GPIO17 does function as a button, the LED does turn on and off by other means. I just can't get the button to turn it off.
Reply
#2
You could use the toggle-method. One press => LED on, next press => LED off, next LED on

try following:
from gpiozero import LED, Button from signal import pause led = LED(20) button = Button(17) button.when_pressed = led.toggle pause()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
It works to toggle the LED when running that script. While it was still running, I ran the script to turn on the LED then went back to your solution. The button had no effect (stayed on). Then I ran the script to stop the LED and went back to your solution. Then yours did not toggle the LED - it stayed off.
Reply
#4
I was offered this solution in another forum. It replaces the script that turns on the LED and incorporates a button to turn it off. This works!

from gpiozero import LED, Button import time from time import sleep pink = LED(20) pink.on() button = Button(7) now = time.time() while time.time() < now + 900:	if button.is_pressed:	break	sleep(0.2) pink.off()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  gpiozero buttons stop working duckredbeard 7 1,724 Jul-18-2025, 03:10 PM
Last Post: deanhystad
  GPIOzero Newbe Question Rchrd 0 1,028 Nov-10-2024, 03:40 PM
Last Post: Rchrd
  Exceeding the value - turn on the relay. stsxbel 0 945 May-27-2024, 07:18 PM
Last Post: stsxbel
  How do i turn my program into a .exe julio2000 1 2,983 Feb-14-2020, 08:18 PM
Last Post: snippsat
  Turn py into exe tester21 4 4,939 Jul-22-2019, 04:31 PM
Last Post: nilamo
  Not sure how to turn this into a loop iamgonge 1 3,059 Dec-05-2018, 11:03 PM
Last Post: anandoracledba

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.