New rev b version of the shutdown button has been released. Much smaller and improved get it HERE
The one bug bare about running a ‘headless’ raspberry pi is shutting it down when something goes wrong. I started looking into what other people had done, I quite liked the look of the turnoffeepi but hated how it covered valuable I2C pins.
This is why I decided to design my own shutdown button. I knew I needed VCC, GND and 2 other pins for the button and the LED. Lucky for me I found the right pins middle of the GPIO that weren’t near any other valuable pins
I knew I would need a VCC and a GND to make this work so I had a look at the pinouts, luckily they supply you with a few VCC and GND pins along the GPIO. About half way down I saw a VCC and GND quite close together an not near any important pins so that’s what made up my mind to use these group of pins. I also choose these pins because I wouldn’t be blocking any other pin like I2C or SPI or UART
As you can see from the schematic below I’ve used a pull up resistor for the button attached to pin 23 and a led paired with a 220 Ohm resistor attached to pin 22. A very simple circuit.
You can order a board at oshpark if you wish https://oshpark.com/shared_projects/zoG0M0tR
I looked at the turnoffee pi and reversed engineered the way it worked from the video. I did try and find the turnoffee pi script but I couldn’t find it anywhere. Its a really simple python script that looks for a interrupt on pin 23, This is to cut down on CPU cycles.
#import the modules to send commands to the system and access GPIO pins from subprocess import call import RPi.GPIO as gpio import os # Define a function to keep script running def loop(): raw_input() # Define a function to run when an interrupt is called def shutdown(pin): gpio.setup(16, gpio.OUT) # Set up pin 16 as an output gpio.output(16, True) # Turn on pin 16 (LED) os.system("sudo shutdown -h now") # Shutdown command gpio.setmode(gpio.BOARD) # Set pin numbering to board numbering gpio.setup(15, gpio.IN) # Set up pin 15 as an input gpio.add_event_detect(15, gpio.RISING, callback=shutdown, bouncetime=200) #Set up an interrupt to look for button presses loop() # Run the loop function to keep script running
If you are on raspbian wheezy follow the steps below and files can be found on github
- To get this script to run at boot you will need to move the script abovd to /home/pi/shutdown/ folder
- Next you will need to edit /etc/rc.local as root with sudo vi etc/rc.local
- Add “sudo python /home/pi/shutdown/softshut.py” without quotes before the exit 0 line in the file
- Save file with ctrl c then ZZ next reboot and enjoy
If your on a Raspberry pi zero or just running raspbian jessie then you will need to follow the steps below and files can be found on github
- Copy the code below into a text file called pishutdown.service and move it onto the pi into etc/systemd/system
- Copy pishutdown.py code below into text file and move that onto the pi into /home/pi/shutdown/
- Enable service ‘sudo systemctl enable pishutdown.service’
- Run service at boot ‘sudo systemctl start pishutdown.service’
- Reboot and you are good to go
[Service] ExecStart=/usr/bin/python /home/pi/shutdown/pishutdown.py WorkingDirectory=/home/pi/shutdown/ Restart=always StandardOutput=syslog StandardError=syslog SyslogIdentifier=pishutdown User=root Group=root [Install] WantedBy=multi-user.target
# shutdown/reboot(/power on) Raspberry Pi with pushbutton import RPi.GPIO as GPIO from subprocess import call import time shutdownPin = 15 GPIO.setmode(GPIO.BOARD) GPIO.setup(shutdownPin, GPIO.IN) GPIO.setup(16, GPIO.OUT) # Set up pin 16 as an output def buttonStateChanged(pin): if not (GPIO.input(pin)): #print"button press" GPIO.output(16, True) # Turn on pin 16 (LED) #print"Shutdown" call(['shutdown', '-h', 'now'], shell=False) GPIO.add_event_detect(shutdownPin, GPIO.BOTH, callback=buttonStateChanged) while True: # sleep to reduce unnecessary CPU usage time.sleep(5)
Quick video of the pi button in action