Shut it down (Raspberry pi that is) (Updated rev b avilable)

UPDATE

New rev b version of the shutdown button has been released. Much smaller and improved get it HERE

tl:dr

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

Hardware

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.

raspberry_pi_button_cut

You can order a board at oshpark if you wish https://oshpark.com/shared_projects/zoG0M0tR

Software

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

pishutdown.service

[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

pishutdown.py


# 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)

Video

Quick video of the pi button in action

Advertisements

13 responses to “Shut it down (Raspberry pi that is) (Updated rev b avilable)

  1. Pingback: Rpi_status (The raspberry pi has got his hat on, Sort of) | Facelesstech·

  2. Pingback: Raspberry Pi shutdown button Rev.b | Facelesstech·

  3. Pingback: Shut it down (Raspberry pi that is) — Facelesstech – OSH Park·

  4. I can’t seem to get it to run the service properly. I follow the above, but it doesn’t work. When I query the service I get the following:

    pi@raspberrypi:~ $ sudo systemctl status pishutdown.service -l
    ● pishutdown.service
    Loaded: loaded (/etc/systemd/system/pishutdown.service; enabled)
    Active: failed (Result: start-limit) since Thu 2016-08-18 08:25:57 CDT; 1min 30s ago
    Process: 649 ExecStart=/usr/bin/python /home/pi/shutdown/pishutdown.py (code=exited, status=1/FAILURE)
    Main PID: 649 (code=exited, status=1/FAILURE)

    Aug 18 08:25:56 raspberrypi systemd[1]: Unit pishutdown.service entered failed state.
    Aug 18 08:25:57 raspberrypi systemd[1]: pishutdown.service holdoff time over, scheduling restart.
    Aug 18 08:25:57 raspberrypi systemd[1]: Stopping pishutdown.service…
    Aug 18 08:25:57 raspberrypi systemd[1]: Starting pishutdown.service…
    Aug 18 08:25:57 raspberrypi systemd[1]: pishutdown.service start request repeated too quickly, refusing to start.
    Aug 18 08:25:57 raspberrypi systemd[1]: Failed to start pishutdown.service.
    Aug 18 08:25:57 raspberrypi systemd[1]: Unit pishutdown.service entered failed state.

    I tried setting the permissions of the shutdown dir to 777 and I also tried doing a sudo systemd daemon-reload before enabling. Any ideas what could be going wrong?

  5. I figured out why. The “code below” is not formatted properly. The indentation is lost in the python file with the code plugin that displays it. I was too quick to copy paste without checking the code!

    • So sorry about that, I will have to have a look at my formatting. Thanks for bring it to my attention. Glad you got it running in the end tho. Did you get some boards made or did you use jumpers and a bread board?

      Happy hacking

      • I sure did! I was thinking “this would be a lot better with SMD, but I’ll just give this a shot for fun.” Then when I was looking for the code again, I saw you already had a version two! Ha.

        This was my second open source board from OSH that I assembled. The first was a Microview Gameboy-like board called MicroJoy.

        Instead of soldering it on permanently, I used a female stackable ISP header because it fit perfectly. Don’t really have a use for the stackable part, but it’s nice to have it removable.

        What would it take to keep the light on a little longer until the pi is completely off?

        I’m considering that question, and how to add a switch of some nature to allow switching back on when used it a mobile application with a battery.

      • Wow I think your the first person I know of that has made one of my boards.

        Sorry you didn’t see the version 2 before making the first. The version 2 is a lot smaller and is great for the pi zero.

        I think the pi turns the led off as it goes through its shutdown sequence but I’m not 100% sure on that one.

        If you want to turn it back on again after using the shutdown button you need to short the run pins with a button, this resets the pi’s processor and will tell it to start up again. Hope this helps you

      • I’m lucky then! The board works great. I need to check out how you implemented the SMD 2.0 version.

        I am guessing that since the script is having the Pi turn on your board’s LED, that the script is stopped at some point during the shutdown procedures. This is why the LED turns off before the Pi’s LEDs indicate that the shutdown is complete.

        I tried shorting the run pins together and it certainly did start it back up! Now the only issue is the battery drain of the two bright SMD LEDs on the Pi. I can’t imagine a way to turn those off…

        Do you know what the unmarked exposed copper is on the back of the Pi3 near BCM 27?

        Thanks for the tips and the board!

      • Yeah the pi kills the power to GPIO when going thought the shutdown sequence.

        The pi pulls around 0.30A idling after boot up. After its been through the shutdown sequence it only draws 0.03A

      • Ok, so maybe the LEDs wont draw too much then.

        So I was curious the other night and I checked the voltages on gpio pins after shutdown but it still plugged in. I found many pins that still held 3 and 5v at that stage. What does that indicate?

  6. With the raspberry pi the GPIO is connected directly to he power rail and you can even power the pi from the GPIO pins like I did in my portable pi project.

    How are you getting on with KiCAD

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s