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.

Ryan McClure
  • 225
  • 2
  • 10

3 Answers3

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.

Gerben
  • 2,420
  • 16
  • 17
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.

hiro345
  • 21
  • 1
0

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

John Shearing
  • 271
  • 3
  • 12