Saturday, April 16, 2016

Adding a Shutdown Button to the Raspberry Pi 2

  1. Writing a Python Script
  2. #!/bin/python
    # Simple script for shutting down the raspberry Pi at the press of a button.
    # by Inderpreet Singh

    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(18, GPIO.IN, pull_up_down = GPIO.PUD_UP)

    # Our function on what to do when the button is pressed
    def Shutdown(channel):
        os.system("sudo shutdown -h now")

    # Add our function to execute when the button pressed event happens
    GPIO.add_event_detect(18, GPIO.FALLING, callback = Shutdown, bouncetime = 2000)

    # Now wait!
    while 1:
        time.sleep(1)
  3. Test it
  4. sudo python shutdown_pi.py
  5. Adding it to startup
  6. sudo nano /etc/rc.local
    sudo python /home/pi/Scripts/shutdown_pi.py &

Source
Filed Under :

0 comments for "Adding a Shutdown Button to the Raspberry Pi 2"

Post a Comment

background