Skip to content

Instantly share code, notes, and snippets.

@oschettler
Created April 10, 2020 09:57
Show Gist options
  • Save oschettler/09f10cafc03d7712f419fc346675053c to your computer and use it in GitHub Desktop.
Save oschettler/09f10cafc03d7712f419fc346675053c to your computer and use it in GitHub Desktop.
Switch light on on the press of a button. Turn it off automatically after 5s
# Energy light
from gpiozero import LED, Button
from time import sleep, time
from gpiozero.pins.pigpio import PiGPIOFactory
factory = PiGPIOFactory(host='raspberrypi')
led = LED(17, pin_factory=factory)
btn = Button(4, pin_factory=factory)
# Time when the light will switch off again
offtime = 0
while True:
if btn.is_pressed:
print("You pressed me")
# Store time to switch light off
offtime = time() + 5
if time() < offtime:
led.on()
else:
led.off()
sleep(0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment