Skip to content

Instantly share code, notes, and snippets.

@jonwitts
Last active August 15, 2017 20:26
Show Gist options
  • Save jonwitts/85ab3b4ef0f75b11b1e8b037b0693edd to your computer and use it in GitHub Desktop.
Save jonwitts/85ab3b4ef0f75b11b1e8b037b0693edd to your computer and use it in GitHub Desktop.
A Python program to shutdown a Pi when button is held for 3 seconds
#!/usr/bin/python3
from gpiozero import Button, LED
from signal import pause
from time import sleep
from os import system
button = Button(21, hold_time=3)
led = LED(20)
def shutdown_pi():
for i in range(3):
led.on()
sleep(0.5)
led.off()
sleep(0.5)
system("sudo shutdown now -hP")
button.when_held = shutdown_pi
pause()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment