ShutDown Button With Interrupt Method
  • Hi all,
    To improve the efficiency and minimize load to the CPU, is recommended to use interrupt method instead of While loop method in the Shutdown script.
    The code for the interrupt method is this one:
    #####################################################################################################
    # Import the modules to send commands to the system and access GPIO pins
    from subprocess import call
    import RPi.GPIO as gpio

    # Define a function to keep script running
    def loop():
    raw_input()

    # Define a function to run when an interrupt is called
    def shutdown(pin):
    call('halt', shell=False)

    gpio.setmode(gpio.BOARD) # Set pin numbering to board numbering
    gpio.setup(7, gpio.IN) # Set up pin 7 as an input
    gpio.add_event_detect(7, gpio.RISING, callback=shutdown, bouncetime=200) # Set up an interrupt to look for button presses

    loop() # Run the loop function to keep script running

    #####################################################################################################

    Here it uses the PIN 7, if you use another pin (Like 17) just change the PIN number in the Script.

    I found this code from tutorial to add an On/Off switch to my Raspberry and it's works great.
    With thid method, I added 3 Buttons to the Raspberry:
    1 - Soft Off
    1 - On
    1 - Hard Off
    With this 3 buttons. the power supply is more likely a ATX power Supply.

    If you are insterested, contact me with personal message.

    * Extracted from raspberry-pi-geek.com
  • cool, thanks for this!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!