Skip to content

Instantly share code, notes, and snippets.

@r3a1d3a1
Created July 4, 2021 12:12
Show Gist options
  • Save r3a1d3a1/869fa94f1fecbb91d448ac8c2957bc2e to your computer and use it in GitHub Desktop.
Save r3a1d3a1/869fa94f1fecbb91d448ac8c2957bc2e to your computer and use it in GitHub Desktop.
Script for shutting down Pi with a push button (GPIO 6 in this case)
# !/bin/python
# Simple script for shutting down the Raspberry Pi at the press of a button.
import RPi.GPIO as GPIO
import time
import os
# Use the Broadcom SOC Pin numbers
# Setup the pin with internal pullups enabled and pin in reading mode.
GPIO.setmode(GPIO.BCM)
GPIO.setup(6, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# Our function on what to do when the button is pressed
def Shutdown(channel):
print("Falling!")
time.sleep(1)
if (GPIO.input(6) == False):
print("Shutting Down")
os.system("sudo shutdown -h now")
# Add our function to execute when the button pressed event happens
GPIO.add_event_detect(6, GPIO.FALLING, callback=Shutdown, bouncetime=2000)
# Now wait!
while 1:
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment