Skip to content

Instantly share code, notes, and snippets.

@alaudet
Last active February 6, 2018 16:25
Show Gist options
  • Save alaudet/9e280d190bff83830dc7 to your computer and use it in GitHub Desktop.
Save alaudet/9e280d190bff83830dc7 to your computer and use it in GitHub Desktop.
Raspberry Pi Graceful Shutdown with gpiozero
#!/usr/bin/python
'''Having a little fun with Raspberry Pi and gpiozero. Small script that uses
a button and leds to indicate a graceful Linux shutdown.
Module at https://github.com/RPi-Distro/python-gpiozero
Wiring your button
https://github.com/RPi-Distro/python-gpiozero/blob/master/docs/inputs.md
Wiring LED's
https://github.com/RPi-Distro/python-gpiozero/blob/master/docs/outputs.md
'''
import time
import os
from gpiozero import Button
from gpiozero import LED
button = Button(2)
redled = LED(17)
greenled = LED(27)
while True:
time.sleep(0.02) # prevents killing your cpu
# Hold button down for 5 seconds. Light turns red and Linux shuts down gracefully
if button.is_pressed:
time.sleep(5)
if button.is_pressed:
redled.on()
cmd = "shutdown -h now"
os.system(cmd)
else:
# On boot light is green.
greenled.on()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment