Introduction: Simple Raspberry Pi Shutdown Button

EDIT: I've since moved to a new location, see huffhacks.com for more guides like this one, also check out my youtube channel called huffHacks.

Since the Raspberry Pi foundation decided to leave out an off button to safely shutdown the Raspberry pi, I'll show you a simple method I came up with to build one, so there are no more excuses for yanking the power cable out of your Pi!

This method uses no extra components apart from a piece of wire, so as long as you have a way of connecting two pins together you can get on and make this right away.

This instructable assumes you know the basics of using a Raspberry Pi, i.e. connecting it to a monitor and keyboard / SSH into it and being able to type commands into the terminal.

Soldering is also useful but not totally necessary to make a more useful button.

Step 1: Download the Python Script

For this whole instructable I'll assume you're in your home directory, so if you're not there yet enter:

cd

First we want to download the script which will wait for the Pi to detect that the "button" has been closed. Open the Raspberry Pi terminal and enter (all on one line):

wget http://tinyurl.com/off-button-py

(For those of you who want the origional link:

"wget https://www.instructables.com/files/orig/FVM/K0WJ/IA0WRZBF/FVMK0WJIA0WRZBF.py”)

Alternatively, you can download the file attached to the instructable and transfer it to your home directory.

Check to see if the file is there using the command (it uses a lower case L if it's not clear in that font):

ls

You should now see the file name "FVMK0WJIA0WRZBF.py" in your home directory.

Step 2: Make It a Hidden File and Change the Name

This step is optional but it will help neaten your system by changing the file name to something more memorable and making the script a hidden file so it's not immediately visible in your home directory (unless you want it that way).

Enter:

mv FVMK0WJIA0WRZBF.py .off_button.py

Now check to see if the file is hidden by entering the "ls" command again. The file should no longer be visible unless you enter:

ls -al

Now you should see ".off_button.py" with a dot before its name.

Step 3: Customise the Script

Now you want to check which raspberry pi revision you have so that you can choose which pin you want to connect the button to using this command:

cat /proc/cpuinfo

Near the bottom where it says "Revision", check the eLinux wiki and compare your number with the ones on that page. Once you know your model and PCB revision, you can select which GPIO pin you want to use from the hobbytronics webpage . Bearing in mind that the switch is activated when the GPIO pin is connected to ground I suggest you use a GPIO pin which is near enough to a ground pin to make a connection.

Then open up the .off_button.py script for editing:

nano .off_button.py

Use the cursor keys to navigate, and replace the variable near the top called, "YOUR_CHOSEN_GPIO_NUMBER_HERE" with the GPIO pin number you just chose. In my case, with a Raspberry pi B revision 2 board I used 7.

Once you've set the pin save and exit the editor:

CTRL + x

y

ENTER

Step 4: Set the Script to Run at Boot-up

Now we want to set up the system so that this script will run at every boot-up. Open the rc.local file with this:

sudo nano /etc/rc.local

Then navigate to the bottom of this file and just before the "exit 0" line which is at the very end, add this:

python /home/pi/.off_button.py

Then exit as before using "CTRL + x" "y" "ENTER". If any of you are looking to run other scripts at start up and want to add them to run at the end of this file, I found that on my system I had to amend the line so it looked like "python /home/pi/.off_button.py &" and then add the location of the next script on the line bellow. This symbol means the scripts are run at the same time.

Step 5: Add the Button

This part is left to your own imagination. In order to register the switch as closed and shut-down the Pi, the GPIO pin you chose earlier should be connected to ground. BE CAREFUL! Only use the pin you set up, if you short other pins you could easily loose your Pi in a puff of smoke.

Before you do anything to the pins switch off the raspberry pi, or if you are just going to use a jumper wire to connect the pins reboot the Pi to make sure the script is running first.

UPDATE: the above statement is very important, the switch will only safely work becuase the internal pull up resistor of the raspberry pi is activated by the Python code at boot up. If this code isn't run and the internal pull up resistor not enabled then the Pi will be permanently damaged. Thanks to gtoal for pointing this out to be more clear.

For this I used some solid core hookup wire which is used with breadboards. I soldered it to the GPIO pin so that there was a tiny gap between it and the ground pin next to it. Make sure the gap isn't too wide as it would mean you are flexing the pin too much, stressing it unnecessarily which could lead to it breaking off in a few weeks, but also not so small that the wire is always touching the ground pin! I also wrapped a piece of tape around the top of the wire so that my finger doesn't send a static charge to the raspberry pi.

You could use the cut off wire on a resistor or diode or any material suitable for some flexing which conducts electricity, however if you are going to solder the wire to one of the pins check before you start whether it will bind to the solder.

Also, be aware that soldering things to your raspberry pi WILL VOID its warranty and I'm not responsible for anything you do here.

As an alternative which won't void your warranty (unless you accidentally connect the wrong pins together) use a female to female jumper wire to touch the pins together.

DO NOT leave the connection permanently in place as some pins are configured as an output automatically on boot up before the script has started to run, which could damage the Pi. A mere touch is all that's needed!

Step 6: Finished

Now when you connect the GPIO pin to ground you should see immediately that the green LED starts flashing and after a few seconds it will flash 10 times, signalling a safe shutdown. Now you can remove the power supply.