There have been several attempts published here and on the web to provide a simple method to halt the Pi with a button to get it ready for power removal.
Similarly, there have been several solutions suggested for restarting the Pi again from a halted state.
I have not seen any simple ones that combine the two. One button to put the Pi in a halt state, and the same button to re-start it again.
Here is such a solution. The schematic is simple. The P6 connector is used to wake the Pi up, and a small Python script is used to put it to sleep. The Python script listens to a GPIO input pin to start the shutdown to halt sequence.
Update:
Dom has provided an alternative to the P6 solution, have a look here:
viewtopic.php?f=29&t=24682&p=1139735#p1139735
The trick is to use one button to do both. This is where R2 and C1 come into play. The P6-1 pin needs to be high for the Pi to run. Bringing it low (<0.5V) without any precautions will send the Pi to sleep - immediately. Warning! Without a proper shutdown! The circuit will prevent that from happening accidentally.
When you power-up the Pi, it will start to boot normally and at the end of the boot sequence, the Python script is activated by cron. To install the script, do the following:
Code: Select all
crontab -e
Code: Select all
@reboot sudo /usr/bin/python /home/pi/halt-and-reset.py
A quick pulse from the button (< 0.5 second) is already enough to trigger the halt sequence, but not enough to drain C1 to get the run pin below .5V. The script shuts the Pi down and puts it in the Halt state. It still consumes power though. My Model(1) B still pulls about 70mA.
You can now safely remove the power from the Pi.
If you want to restart it from the halt state instead, a press of about 2 Sec. duration or more is enough to drain C2 and to re-start the Pi as soon as you release the button. The Pi will now go through an abbreviated power-up sequence.
The following Python script can be used to execute the halt state request and properly "Halt" the RPi so it can be restarted again with a next button press.
Code: Select all
nano halt-and-reset.py
Code: Select all
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Name: halt-and-reset.py
# Purpose: Together with a pushbutton and a little circuit attached to J6,
# it is possible to use one button to halt the Pi, and restart it.
#
# Author: paulv
#
# Created: 20-03-2016
# Copyright:
# Licence: <your licence>
#-------------------------------------------------------------------------------
import RPi.GPIO as GPIO
import subprocess
DEBUG = False
GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)
BUTTON = 28 # GPIO-28 is P5-3 or any other standard pin
GPIO.setup(BUTTON, GPIO.IN, pull_up_down=GPIO.PUD_UP) # pulled-up just in case the circuit is not connected.
def main():
while True:
# set an interrupt on a falling edge and wait for it to happen
GPIO.wait_for_edge(BUTTON, GPIO.FALLING)
if DEBUG:
print "Button press detected", GPIO.input(BUTTON)
else:
print "Stop requested, Halting the RPi now!"
subprocess.call(['halt'], shell=True, \
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if __name__ == '__main__':
main()
It should be fairly simple to mount a push button to the housing of a Pi box.
Adding the two resistors, and the capacitor should not be too difficult as well. The required connections to 3V3(P5-2), GND(P5-7) and the GPIO pin(P5-3) can all be made to the P5 connector. The only other connection must be made to J6-1, which is the pin (or rather the hole) closest to the edge of the PCB.
You now have a permanent way to halt and restart the Pi, especially handy when it is running headless.
Enjoy!