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.

- 225
- 2
- 9
3 Answers
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.

- 2,420
- 14
- 17
-
1Brilliant! I didn't realize that I could do it all on just 5 and 6. Thank you! :) – Ryan McClure Jan 26 '14 at 04:37
-
So, we both learned something today (-: – Gerben Jan 26 '14 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 '14 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 '14 at 05:58
-
@athanassis It only runs 10 times a second. 100ms is a very long time, CPU land. That's like 80000000 clock cycles. I guesstimate it uses around 0.1% CPU or less. – Gerben Oct 30 '14 at 16:31
-
1Indeed, 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 '14 at 18:36
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.
RPi Low-level peripherals - eLinux.org http://elinux.org/RPi_Low-level_peripherals#P6_header
Making a Reset Switch for your Rev 2 Raspberry Pi » RasPi.TV http://raspi.tv/2012/making-a-reset-switch-for-your-rev-2-raspberry-pi

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

- 271
- 3
- 12