10

I have rigged up a wake-from-halt button using pins 5 and 6. These two pins, when connected, will reset power and reboot from halt. I want to use this same button, if I can, to run a command that, when pressed, will shut down the computer using sudo halt. Would this be possible? For the sudo halt button, I would need to connect from GPIO 1 to the button, then split to GPIO 6 (ground) and a general GPIO pin. I don't know if it is possible/safe to connect GPIO 5 to this either.

3 Answers 3

8

No need to add other GPIO pins. You could just use the same pins for your halt-button.

Here is some python code that will poll pin 5. When the button is presses pin 5 is pulled to ground (pin 6), and the code will read a LOW. In that case is will run the halt command

#!/usr/bin/python
import RPi.GPIO as GPIO
import time
import subprocess

GPIO.setmode(GPIO.BOARD)
# set pin 5 to input, and enable the internal pull-up resistor
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)

oldButtonState1 = True
while True:
    buttonState1 = GPIO.input(5)

    if buttonState1 != oldButtonState1 and buttonState1 == False :
        # print "Button 1 pressed"
        subprocess.call("halt", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    oldButtonState1 = buttonState1

    time.sleep(.1)

PS. I didn't know about the Wake from Halt function. Thanks to you I know now! So thanks.

6
  • 1
    Brilliant! I didn't realize that I could do it all on just 5 and 6. Thank you! :) Jan 26, 2014 at 4:37
  • So, we both learned something today (-:
    – Gerben
    Jan 26, 2014 at 15:28
  • More of a note to myself. You could even modify the code to have a press do a reboot, and a press-and-hold to do a shutdown (or vice versa).
    – Gerben
    Jan 26, 2014 at 20:11
  • In what way is your script running continuously, i.e., daemonized? Have you noticed any decrease in performance when watching movies?
    – athanassis
    Oct 30, 2014 at 5:58
  • 1
    Indeed, it's very long time, but not when CPU hits 100% for a few secs when, e.g., decoding a full HD movie. I'll give it a try, testing never hurts. Thanks for the feedback!
    – athanassis
    Oct 30, 2014 at 18:36
2

A reset button can be attached to the P6 header, with which the Pi can be reset. Momentarily shorting the two pins of P6 together will cause a soft reset of the CPU (which can also 'wake' the Pi from halt/shutdown state)

c.f.

0

Using overlays worked great for me. http://www.stderr.nl/Blog/Hardware/RaspberryPi/PowerButton.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.