1.

http://www.3cc.org/blog/2013/01/raspberry-pi-shutdown-switch-safely-turning-off-the-pi/

All that’s left is to monitor pin 17 for activity, for which I used the code on the Raspberry Pi forum.

Placed in /home/pi/bin/button/shutdown.py

import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
if(GPIO.input(17)):
os.system("sudo shutdown -h now")
break
time.sleep(1)

Placed in /etc/rc.local on the line before “exit 0″

python /home/pi/bin/button/shutdown.py

Then the Pi will shutdown when the button is pressed for about 1 second – only the Power light will remain on (red) when it’s shut down. It’s worth noting that this won’t work if the Pi has crashed but that’s not an issue we often see.

 

 

2. Reference circuit.

BatteryAdapterSwitchCircuit_zpsdae2a018.jpg