Shutdown Switch for a Raspberry Pi – or: too many Legs

A Raspberry Pi lacks a shutdown button. To keep the price down I heard.
However, simply cutting off the power of a running Pi might damage the system. This is avoidable with a simple button.

In projects that require a Pi running headless (without display) it certainly helps to have a button which triggers a safe shutdown procedure. It might not always be possible to log on to the Pi, run the custom shutdown procedure for the specific project and manually type the shutdown command.

A script triggered by a simple button can do this! The script can trigger the custom shutdown procedure for the project and can turn off the Pi afterwards.

Here’s the description of how it works for me:

Hardware

Raspberry Pi (Zero) with running OS etc
Momentary switch
Some cables

Wiring

Wiring
Wiring

The switch is connected to GND and a free pin next to the GND pin. In this case it is BCM pin 21. When the switch is pressed, an edge is detected. This signal can be used to trigger the desired actions.

Software

Short vs. Long Press

A short and a long press can be easily distuingished using a simple counter in a callback function. The callback function is triggered when an edge is detected on pin 21.

# This script will wait for a button to be pressed and then shutdown or reboot the Raspberry Pi.
# A long press initiates a reboot, a very long press initiates a shutdown.

import time
from time import sleep
import RPi.GPIO as GPIO
import os

##############CONFIG##########################
demo_mode = False
debug = False
GPIN=21
##############END CONFIG######################

GPIO.setmode(GPIO.BCM)

# Pin 21 will be input and will have its pull-up resistor (to 3V3) activated
# so we only need to connect a push button with a current limiting resistor to ground
GPIO.setup(GPIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
int_active = 0

print "Shutdown / Reboot script started."

# ISR: if our button is pressed, we will have a falling edge on pin 21
# this will trigger the interrupt:
def shutdown_reboot_callback(channel):

# button is pressed
# possibly shutdown our Raspberry Pi

global int_active

# only react when there is no other shutdown process running

if (int_active == 0):
int_active = 1
pressed = 1
shutdown = 0

# count how long the button is pressed
counter = 0

while ( pressed == 1 ):
if ( GPIO.input(GPIN) == False ):
# button is still pressed
counter = counter + 1
if debug:
print "pressed: " + str(counter)
# break if we count beyond 20 (long-press is a shutdown)
if (counter >= 20):
pressed = 0
else:
sleep(0.2)
else:
# button has been released
pressed = 0

# button has been released, count cycles and determine action

# count how long the button was pressed
if (counter < 2):
# short press, do nothing
pressed = 0
int_active = 0
if debug:
print str("short press, do nothing")

else:
if debug:
print "else " + str(counter)
# longer press
if (counter < 10):
# medium length press, initiate system reboot
if debug:
print("rebooting..")

# run the reboot command as a background process
if demo_mode:
print("sudo reboot now")
exit(0)
else:
os.system("sudo reboot now")

elif (counter>=10 and counter<20):
# long press, initiate system shutdown

if debug:
print("shutting down..")

# run the shutdown command as a background process
if demo_mode:
print("sudo shutdown now")
exit(0)
else:
os.system("sudo shutdown now")


# Program pin 21 as an interrupt input:
# it will react on a falling edge and call our interrupt routine "shutdown_reboot_callback"
GPIO.add_event_detect(GPIN, GPIO.FALLING, callback = shutdown_reboot_callback, bouncetime = 500)

while True:
if debug:
print "."
time.sleep(1)

Setup

To launch the script at startup of the Raspberry Pi place it on the pi (e.g. in /usr/bin) and  add this line to /etc/rc.local just before exit 0:

sudo python /usr/bin/shutdown_reboot.py&
Shutdown / Reboot switch on a Raspberry Pi Zero
Shutdown / Reboot switch on a Raspberry Pi Zero

And here is the solution for the attentive reader on the remark on too many legs: a common momentary switch has four legs and is squared. Actually it is a matter of orientation whether the switch works as expected…

Advertisements

Kommentar verfassen

Trage deine Daten unten ein oder klicke ein Icon um dich einzuloggen:

WordPress.com-Logo

Du kommentierst mit Deinem WordPress.com-Konto. Abmelden / Ändern )

Twitter-Bild

Du kommentierst mit Deinem Twitter-Konto. Abmelden / Ändern )

Facebook-Foto

Du kommentierst mit Deinem Facebook-Konto. Abmelden / Ändern )

Google+ Foto

Du kommentierst mit Deinem Google+-Konto. Abmelden / Ändern )

Verbinde mit %s