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:


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


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


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



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.

<p>This is just what I was looking for. I modified the code </p><p>os.system(&quot;sudo shutdown -h now&quot;)</p><p>to </p><p>os.system(&quot;sudo reboot -h now&quot;)</p><p>so I could use it as a restart button.</p><p>I now have a power button and a restart button. I wish the power button did more than just disconnect the power supply. I would like for it to shutdown the pi and then disconnect power supply but I have yet to find a way to do that.</p>
<p>I've tried 3 fresh installs now and each time at boot it always gets stuck at My IP address is XXX.XXX.XXX.XXX and gets stuck. Any ideas?</p>
<p>It may be because the '&amp;' symbol at the end of the line calling your script is missing: /home/pi/.off_button.py &amp;</p>
<p>Legend!</p><p>Mine was stuck on some Samba thing, &amp; symbol fixed it, not mentioned in the tutorial though!</p>
<p>Hi,</p><p>The script works fine when in I forcibly run it. {python .off_button.py}</p><p>However when running retropi shorting the pins does nothing. Do I somehow need to add a startup somewhere within retropi config?</p><p>Many thanks in advance.</p>
<p>hi..is there a way to make his on gpio 23? </p>
<p>For Raspberry Pi 3 model B , I use GPIO = GPIO21 (is board pin 40 on the gpio board close to the ground, so perfect) with wires and a real push button.</p><p>rc.local works fine with ubuntu mate.</p>
<p>You should also note that if you use certain pins... at least pin 5 (BOARD pin) on RPi3 you can use it to boot up again as well.</p>
<p>Hi, I carried out your instructions and it works great. Well, not great because a couple of times a day the script gets called without me pressing the button. I have tried a variety of GPIO pins but this still happens. Does anyone have any pointers that would help me track down the cause?</p><p>Thanks, Ray</p>
It sounds like you may need a resistor on the switch, to cancel out noise.
<p>Did you use a switch or are you using the method described with a piece of wire dangling from the GPIO?</p>
<p>Thanks for your reply, no dangling. I originally had female plugs on the end of leads which I touched together to shut down things. I have now installed a momentary button. So there shouldn't be any accidental physical touching. Its only a couple of times a day but its still a pain.</p><p>Ray</p>
<p>Yeah that sounds like a huge pain. There is nothing much to the script so I don't see an issue there. Maybe try changing the script to a different GPIO pin and see if that has an effect? There could be so interference causing the Pi to see your pin going low? Also you might try &quot;commenting out&quot; the &quot;debounce&quot; section of the code for edge detection... I don't see an issue with bouncing because as soon as the &quot;sudo shutdown -h now&quot; command is issued, then everything just stops. I suppose it wouldn't really matter if it triggered it several times...</p>
<p>Also, just saw where you said you tried different GPIO pins... so I guess that's out..</p>
<p>Thanks for your replies. The only thing I can think of is a damaged Pi. I have taken care never to short the 5volt pins but maybe I have.</p><p>Thanks, Ray</p>
<p>Actually, it might be due to the noise on the input pin. Try connecting a 1K resistor as suggested above.</p>
did you ever find a fix to this, Im having the same problem but needing to have it on I cant afford to lose any infomation or running programs. I haven't tryed commenting out the sections the author mentioned yet, so hopefully that fixes it.
<p>No I didn't. I think I am on my fourth pair of pins and the problem seems to have gone away of its own accord. The current pin I am using on a Pi B Mk2 is GPIO 17. Previous pairs of pins are 1 and 3, and 39 and 40. These are actual pin numbers not GPIO. What seems to make GPIO pin 17 special is that it is soley a GPIO pin and does not double up as something else. I dont know whether this is relevant?</p>
<p>Very nice and concise Instructable!</p><p>One problem: I tried it, and it didn't work on my Raspberry Pi 2. It gave me an error with something about &quot;RPi.GPIO&quot;. </p><p>I researched the issue, and it had to do with the Python GPIO module version on my Pi being out of date, and not compatible with the Rpi 2.</p><p>To fix this, I did the following:</p><p>1) sudo apt-get update</p><p>2) sudo apt-get install python-dev (this step installs python source files)</p><p>3) sudo apt-get install python-pip</p><p>4) sudo pip install -U RPi.GPIO</p><p>Then, when I rebooted and checked your script, it was working fine. </p><p>I love your modification! Thank you so much for posting it.</p>
<p>Oh, and incidentally, I tested it by modifying it to use GPIO 21 (which <br>is all the way at the end of the GPIO pins on the RPi2), and triggered it <br> using a paperclip to touch the GPIO 21 pin to the ground which is right next to it. This is a nice option in a pinch, if you haven't had a <br>chance to rig up a pushbutton.</p>
<p>Hi, mine is also Raspberry Pi 2, and change YOUR_CHOSEN_GPIO_NUMBER_HERE to 21. But a after I put script on rc.local, the RPi can not finish booting. It freeze after &quot;My IP address is XXX.XXX.X.XXX&quot;. When I click alt+F2, it result black screen. Please help me. Thank you very much</p>
<p>Note that there should be an '&amp;' symbol at the end of the line calling your script:</p><p>/home/pi/.off_button.py &amp;</p><p>Without that the terminal will be captured by your script, and will never return.</p>
<p>First, at terminal, type</p><p>&quot;cd /home&quot; and press enter</p><p>make all 3 steps instructions</p><p>On step 4, make whats is saying but remove the folder &quot;/pi/&quot; </p><p><em>&quot;</em>python /home/<strong>pi</strong>/.off_button.py<em>&quot;</em></p><p><em>leaving like this:</em></p><p><em>python /home/.off_button.py</em></p><p>Save the rc.local and reboot.</p><p>I think this will work.</p>
<p>Has anyone tried this with a button and jumper cables? Would you need a resistor?</p>
<p>A 1 K resistor connected between the input pin and 3.3 V power supply will eliminate false triggering. Highly recommended.</p>
<p>I have done this with jumpers and a button. No resistors. Works great. </p>
<p>As as engineer, never leave a pin &quot;floating&quot; this would explain some of the false triggering issues below. Put a 1K resistor to positive. When shorted to ground, the 1K will be pulled low. Even internal pullups can't overcome noise on power rail.</p><p>Thanks for the code.</p>
<p>Note: The resistor has to be connected to +3.3 volts, not 5 volts</p>
<p>So would the resistor go between GPIO and the switch, or between the switch and ground?</p>
<p>It does not matter, as long as it is in between the ground and pin. </p>
<p>Hi Andrew, great post!</p><p>I am currently trying to do this using GPIO21 + GND on my RPI3. In order to prevent my board from frying, how do I ensure that the script is running on bootup?</p>
I already have a Python script that will run on boot , if I follow this tutorial will it effect my original startup Python script?
<p>I am making this with a rpi 3 model b on Libreelec. When I get to step four the <em>nano /etc/rc.local</em> command does not work. I did some poking around on the net and think I need to make a script with nano /storage/.config/autostart.sh. My experience with this is limited. Can anyone help with setting the file to run at startup? </p>
<p>Thank you so much for this. took my old PC power on/off switch. Tried it on my raspberry pi 2 b+ model and the raspberry pi 3. works like a magic. Thanks again</p>
<p>I added a pushbutton switch to the box right above the power connector. This should help keep it from getting bumped. Fits really nicely. Also, placed it here so flexible wires could be run to the GPIO pins thus allowing the switch to be wired and installed easily. I'm not using the GPIO so hard soldering to the necessary pins and then heat shrinking worked. One wire for 3.3V to one side of the switch and a junction of the two resistors to the other.</p>
<p>uhm.. silly question. how do i know if script is running?</p><p>when booting and there are 3 raspberries and a bunch of text i see FAILED somewhere but it goes so fast i dont see what failed</p>
<p>it was my vncserver. but still how do i know if the script works ? :(</p>
<p>use gpio_pin_number=3 to power off and power on the pi. Simple but very nice tricks !</p>
<p>Hello,</p><p>I have a Pi 2B, what I read in a answer, I have taken the 6 and 26 pins, connected to a button who has only a pres status. My desire is to have two possibilities: reset (short press) and halt (long press). Is that possible? I am a noob with all these things, what options should I modify of your script and with which parameters? Thanks for your time.</p>
<p>Step 4: amending the line with an ampersand forks the command to a subshell and runs it asynchronously, rather than blocking later commands. You should always use &amp; unless you intend the command to block until it finishes.</p>
<p>every time i reboot i get a message saying off-button-py.py can't load because the directory doesn't exist. everything i is coded exactly like it is in the examples</p>
<p>Firstly thanks for great tutorial. I have problem with this solution.<br>My PI run by UPS.when my main power source is down pi run by UPS charge bt when main power source is up pi shutdown. This problem only happens when i plug the power off button.I'm using Raspberry pi 2. I use Pin no 39 40 for poweroff.</p><p>Please help me.Thanks in advance.</p>
I assume i could use jumper wires and a momentary button?
<p>Hello I am new to the forum and I have a big doubt .<br>I'm trying to create an on off button, I have doubts. I have a raspberry pi 2 model B , and from what I saw the pins that I use are pin 4 and pin 6 .<br><br>but recalbox off incorrectly, just cutting energy.<br><br>I found you script that helps shut down properly , I would like to know which file should I paste this script in recalbos.<br><br>Sorry for my English.<br><br>Beto .</p>
<p>Hi Beto,</p><p>if you use pin 4 and 6 an a PI2, you make a short circuit between 5V and GND. Use eg pin 6 (GND) and pin 26 (GPIO 7) instead.</p>
<p>Hello Giovani! thanks a lot!</p><p><br>My doubt now is where to place the script within the recalbox . In this case, also [ in I enter it at the root ?</p><p>And finally , I could use tb these last two pins like in your video ?<br>Thanks!</p>
<p>I like what you have done here, a much needed improvement. You can extend it have a Restart button as well. As some commenters have pointed out, adding a 1k to pull the pin up would likely solve most of the false triggers that some are seeing. I wonder if changing the script to require a button press for x seconds, might not be a better idea. In the interests of formality, it might not be a bad idea to place the script in a normal script directory or program directory /opt or /etc instead of in the pi home directory.</p><p>Neat, I was just looking for a way to do this and stumbled across your post, thanks!</p>
Now I would like tonitor the 10 flshes on the front via an led. Thank you for showing how it can be done I had a lot of fun doing it. I hope the pictures come out ok.<br>
<p>Thanks for the very useful Instructable! I used momentary switches and if it hasn't been noted: the Pi B Rev. 2 board has open holes at P6 where you can solder a switch to perform a soft reset when pressed with no extra programming. Pressing this will also &quot;wake up&quot; the Pi after a shutdown while the red LED is lit, which keeps you from needing to unplug the power and plug it back in.</p>

About This Instructable




More by AndrewH7:Simple Raspberry Pi Shutdown Button 
Add instructable to: