From the beginning of playing with the Pi I wanted a way to power on and off the Pi without having to unplug the micro usb cable. For the most part, this is just out of fear of wearing out the USB connection, but it is also nice to have a physical button to perform relatively common and simple tasks.http://visualcage.ru
This switch should turn the Raspberry Pi on if the Pi is sleeping, and it should initiate a clean shutdown if the Pi is on. This should be a physical button, not just a software remote button.
Please be advised, this projects rating is: More Difficult – Involves soldering, use of ssh, command prompt, and coding.
There are four major steps.
First, make and connect a simple “wake-up” button. To wake the Raspberry Pi up when it is asleep, all you need to do is short Pin 5 to ground. (Pin 5 is also known as GPIO03). Since Pin 6 is already at ground, you can do this by shorting Pins 5-6.
Find a contact switch
You can find these for a lot cheaper online, but sometimes you just want to complete a project right now! I suppose I’m just too impatient sometimes.
The important thing is that you are looking for a Normal Open (N.O.) Momentary Contact switch. This will short the two pins only when the button is pressed. If you choose a Normal Close (N.C.) button, you may have additional problems (such as the Pi perpetually resetting)
Below is a picture of the button mounted in the case:
Header Strip Plug The next thing that you need is a header strip to plug onto to the Pi. You could use a regular female header strip like this one:
If you have an old computer case laying around (which many of us do), you can just steal one of the cables connecting a case LED to the mother board. Motherboards have the same spacing as the Raspberry Pi header. For a previous project, I used a former speaker cable:
In this case, I used a jumper like the one shown here. I used two of them, one for each lead coming from your button.
The advantage is that you don’t have to do any soldering to the straight pins. Also, it allows your switch to be removable, but the contacts are pretty solid. As long as you use solid wire, place a solid wire lead on each terminal of the switch. Then you can plug the wire directly into the jumper. You want the switch to be able to unplug from the Pi so that you can mount it in a case and remove it if necessary (which always seems the case with my projects).
I recommend covering each lead as well as both contacts together with some head shrink tubing. This gives a little support to the connection and also prevents unintentional shorts. In this example, the leads are shown.
Connecting to the Pi
When you are all done, simply plug your wired switch across pins 5 and 6 on the Pi, as shown below:
The reset switch is connected using the green and black jumper cables. Since the switch only provides a short when pressed, it shouldn’t matter which lead is connected to which pin.
You’ll notice that this is shown with the original Raspberry Pi Model 2, but this should work with all versions of the Pi.
That’s it. You now have a button that can wake the Raspberry Pi from sleeping mode. This is a hardware switch, nothing needs to be done in software to wake the Pi from the sleeping state, it will automatically wake when Pin 5 is tied to ground.
Note: All of the following commands can either be done from the command prompt on the Pi, or through an SSH session. The default username for raspbian is
pi and password is
If they are not already installed, you will want to install the following packages using apt-get with the following command :
Next you need to get RPi.GPIO by doing the following:
Uncompress the packages by doing the following:
Next move into the newly created directory
Next, install the module by doing:
The following commands should be done either through the shell or through SSH. By default, SSH is enabled on Raspbian.
I recommend creating a directory to hold your scripts. In my case, I created the directory under the root /home/pi directory. It can be created by doing:
This will make a scripts directory under the
Next, we need to write the script. I personally use nano as my editor of choice, but you can use any editor you wish. Please Note: I highly recommend retyping the script. Do not copy and paste. It is possible that if you copy and paste, there will be slight changes in formatting (i.e. comment lines wrapping and no longer being comments) or incorrect characters (particularly quotes) that will keep your script from working!!!
We will call our script shutdown.py (it is written in python). Create and edit the script by doing:
The content of the script should be:
Once you have created your script, you can test it by typing the following at the command prompt:
It may give warnings regarding a pull-up resistor. This is OK. If error’s are encountered, resolve them first. If it is able to run, try pushing the button and see if the Pi shuts down.
Once you have the script working the way that you want it, let’s make it run at startup.
To configure our script to run at startup, it should be added to /etc/rc.local
You can do this with the following:
Add the following to the file
That’s it! Now your script should run at startup. Once you restart your pi, you should be able to see shutdown.py running in the background. You can verify this either by pressing the button and seeing if your pi shuts down, or doing the following from the command line: