Introduction: Simple Raspberry Pi Shutdown Button

EDIT: I've since moved to a new location, see 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):


(For those of you who want the origional link:


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



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 "" with a dot before its name.

Step 3: Customise the Script

Picture of 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 script for editing:


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

Picture of 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/

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/ &" 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

Picture of 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.


PHolker made it! (author)2017-06-15

I have a Raspberry Pi 3 Model 3.

I connected an old PC switch to PIN5&6 and changed the script to read



When the RasPi is shut down, it will startup again on PIN5 being connected to PIN6(=GRD).

Thus I have a simple Power-on/-off configuration. Thanks a lot!

P.S. This is the best script, I've found floating around on the web, that gets the job done reliably! The Raspi in my home cinema rack!

TzahiA2 (author)PHolker2017-10-25

Perfect ! just tried this variation, and it's exactly what i needed.

A couple of things I learned from this project. (I'm a complete noob with Raspi) :

1. Do not confuse pin physical number with GPIO number. The "gpio_pin_number=3" in the code, refers to GPIO03, which is physically pin #5 (in Pi 3 Model B).

2. As PHolker wrote, the pushbutton goes between GPIO03 and GND - pins #5 and #6, (in the attached drawing, pins #2 and #6 are marked, which was a little confusing).

3. GPIO03 is a special, in that shorting it to ground will wake the Raspberry Pi from halt/shutdown state. This is what makes it possible for the same pushbutton to serve the two purposes : trigger the shutdown script, when the device is on, and wake it up, when the device is at shutdown (and there is no OS to run any script).

Of course, all the credit goes to PHolker. I've just added my two cents, for anyone else who may have been wondering, like me.


TzahiA2 (author)TzahiA22017-10-25

BTW - For some reason, I'm unable to run things at startup using the rc.local procedure suggested by AndrewH7. (I think it has to do with my specific OS configuration). So I ended up using systemd, as explained here :

LeandroA68 (author)PHolker2017-06-26

Dear PHolker,

You did exactly what I'm looking for on my PI3, can you please send more pictures and more details on your GPIO, as I don't want "burn" my be loved PI3?

Thank you very much in advance.



PHolker (author)LeandroA682017-08-21

Hey Leandro,

have you goit it done already.
sorry, but I was on vacation and did not check this email for a long time.

Gods bless and happy raspi-ing,

MasterShifu (author)2017-09-30

Works perfectly. Thanks!

PHolker (author)2017-08-21


pi in off state: red LED on
pi in on state: red LED on, yellow blinking on SDD access (spordically)
to turn pi on: press button and wait ... yellow LED starts to flicker
to turn pi off: press pi and wait ... yellow LED flickers, then blinks 10x before powering off

ThomasR35 made it! (author)2017-07-17

I turned it into a reset button on my retro nintendo emulator.

Matthijs Kooijman (author)2017-07-10

Instead of a custom python script to watch the GPIO pin on the rpi
side, you can also configure the kernel to do the same. See my blogpost
on how to set this up:

ThomasR35 (author)2017-04-04

This is just what I was looking for. I modified the code

os.system("sudo shutdown -h now")


os.system("sudo reboot -h now")

so I could use it as a restart button.

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.

gusper (author)2015-05-29

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?

raman_raja (author)gusper2016-10-30

It may be because the '&' symbol at the end of the line calling your script is missing: /home/pi/ &

AndyT101 (author)raman_raja2017-02-14


Mine was stuck on some Samba thing, & symbol fixed it, not mentioned in the tutorial though!

BoboC6 (author)2017-02-06


The script works fine when in I forcibly run it. {python}

However when running retropi shorting the pins does nothing. Do I somehow need to add a startup somewhere within retropi config?

Many thanks in advance.

GlenP19 (author)2017-01-10 there a way to make his on gpio 23?

cmelyo (author)2017-01-07

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.

rc.local works fine with ubuntu mate.

dsstrainer (author)2016-12-17

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.

Raymadaddy (author)2015-07-30

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?

Thanks, Ray

grandwigg (author)Raymadaddy2016-12-10

It sounds like you may need a resistor on the switch, to cancel out noise.

jgroner (author)Raymadaddy2015-08-03

Did you use a switch or are you using the method described with a piece of wire dangling from the GPIO?

Raymadaddy (author)jgroner2015-08-03

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.


jgroner (author)Raymadaddy2015-08-03

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 "commenting out" the "debounce" section of the code for edge detection... I don't see an issue with bouncing because as soon as the "sudo shutdown -h now" command is issued, then everything just stops. I suppose it wouldn't really matter if it triggered it several times...

jgroner (author)jgroner2015-08-03

Also, just saw where you said you tried different GPIO pins... so I guess that's out..

Raymadaddy (author)jgroner2015-08-04

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.

Thanks, Ray

raman_raja (author)Raymadaddy2016-10-30

Actually, it might be due to the noise on the input pin. Try connecting a 1K resistor as suggested above.

peter6961 (author)Raymadaddy2015-08-10

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.

Raymadaddy (author)peter69612015-08-11

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?

lhwong (author)2015-06-18

Very nice and concise Instructable!

One problem: I tried it, and it didn't work on my Raspberry Pi 2. It gave me an error with something about "RPi.GPIO".

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.

To fix this, I did the following:

1) sudo apt-get update

2) sudo apt-get install python-dev (this step installs python source files)

3) sudo apt-get install python-pip

4) sudo pip install -U RPi.GPIO

Then, when I rebooted and checked your script, it was working fine.

I love your modification! Thank you so much for posting it.

lhwong (author)lhwong2015-06-18

Oh, and incidentally, I tested it by modifying it to use GPIO 21 (which
is all the way at the end of the GPIO pins on the RPi2), and triggered it
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
chance to rig up a pushbutton.

prabuchresno (author)lhwong2015-09-27

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 "My IP address is XXX.XXX.X.XXX". When I click alt+F2, it result black screen. Please help me. Thank you very much

raman_raja (author)prabuchresno2016-10-30

Note that there should be an '&' symbol at the end of the line calling your script:

/home/pi/ &

Without that the terminal will be captured by your script, and will never return.

First, at terminal, type

"cd /home" and press enter

make all 3 steps instructions

On step 4, make whats is saying but remove the folder "/pi/"

"python /home/pi/"

leaving like this:

python /home/

Save the rc.local and reboot.

I think this will work.

vladster333 (author)2015-11-22

Has anyone tried this with a button and jumper cables? Would you need a resistor?

raman_raja (author)vladster3332016-10-30

A 1 K resistor connected between the input pin and 3.3 V power supply will eliminate false triggering. Highly recommended.

kcbaldmike (author)vladster3332015-12-16

I have done this with jumpers and a button. No resistors. Works great.

TerryP23 (author)2015-11-18

As as engineer, never leave a pin "floating" 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.

Thanks for the code.

raman_raja (author)TerryP232016-10-30

Note: The resistor has to be connected to +3.3 volts, not 5 volts

WolfgangR5 (author)TerryP232016-04-08

So would the resistor go between GPIO and the switch, or between the switch and ground?

CodyZ2 (author)WolfgangR52016-04-13

It does not matter, as long as it is in between the ground and pin.

jaykeub (author)2016-09-20

Hi Andrew, great post!

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?

lego mindstorms (author)2016-08-17

I already have a Python script that will run on boot , if I follow this tutorial will it effect my original startup Python script?

JefferyC10 (author)2016-08-04

I am making this with a rpi 3 model b on Libreelec. When I get to step four the nano /etc/rc.local command does not work. I did some poking around on the net and think I need to make a script with nano /storage/.config/ My experience with this is limited. Can anyone help with setting the file to run at startup?

fvarteji made it! (author)2016-06-08

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

tarrow3 made it! (author)2016-05-28

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.

arx123 (author)2016-05-21

uhm.. silly question. how do i know if script is running?

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

arx123 (author)arx1232016-05-21

it was my vncserver. but still how do i know if the script works ? :(

SullivanB1 (author)2016-04-18

use gpio_pin_number=3 to power off and power on the pi. Simple but very nice tricks !

alvaro_cuesta made it! (author)2016-04-12


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.

miprosocial (author)2016-03-23

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 & unless you intend the command to block until it finishes.

jelly.turf (author)2016-03-16

every time i reboot i get a message saying can't load because the directory doesn't exist. everything i is coded exactly like it is in the examples

About This Instructable




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