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

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

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

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.

10 People Made This Project!


  • 1 Hour Challenge

    1 Hour Challenge
  • Pets Challenge

    Pets Challenge
  • Classroom Science Contest

    Classroom Science Contest

69 Discussions


5 months ago

Unfortunately, this seems to put quite a strain on the system, because a Python instance must run in the background all the time to keep the pin monitored. I've tested it myself and when reading processes with top it shows a 100% usage of a single core (using a quad-core Rapspberry Pi 3 model B) and 50,5°C of temperature, with more energy used. Disabling the process (sudo pkill python) not only releases the core but also decreases the temperature to about 45°C. Despite being very safe, allowing for a clean shutdown with a single button pressing, its efficiency is very low.

Given this, in my opinion a kernel module should be built instead. It would have to handle the thing in a much tidier way at a much lower level. This Python solution should be used only if there is no alternative.

Edit: there is a solution already. The device tree overlay (dtoverlay) framework already gives a way to perform a clean shutdown with minimal cost (no CPU usage discernible, and no temperature increasing). Just add the following line:


to /boot/config.txt as a superuser, reboot, and you're done. Add a pushbutton to GPIOs 5 and a GND and you can do a buttonized shutdown out of the box. You may also change the GPIO on which the signal is caught.

Further info can be found in the file /boot/overlays/README.


Question 10 months ago on Step 3

Forgive my ignorance. I believe I have customised the script but I run in to trouble with;

NameError: name 'gpio_pin_3' is not defined

Any help would be much appreciated.


1 year ago

Glued and soldered a push button to female header pins for easy removal from the pi. Also, i modified the code a bit. Import the time library and change the code like this:

GPIO.wait_for_edge(gpio_pin_number, GPIO.FALLING)
#Use falling edge detection to see if pin is pulled
#low to avoid repeated polling

# Wait for 1.5 seconds. If the button is still being pressed (still low)
# then shutdown.

#Send command to system to shutdown
if (GPIO.input(gpio_pin_number) == 0):
os.system("sudo shutdown -h now")

# If the button is high after the 1.5 second delay (not being held down),
# reboot instead
os.system("sudo reboot -h now")


Now you can shutdown AND reboot with a single button. Press it once for a reboot, or leave it pressed to shutdown the pi! :)


1 year ago

Works perfectly. Thanks!


1 year ago


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


2 years ago

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.


4 years ago on Introduction

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?

2 replies

Reply 2 years ago

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


Reply 2 years ago


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


2 years ago


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.


2 years ago there a way to make his on gpio 23?


2 years ago

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.


2 years ago

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.


3 years ago on Introduction

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

5 replies

Reply 2 years ago

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


Reply 3 years ago on Introduction

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


Reply 3 years ago on Introduction

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.



Reply 3 years ago on Introduction

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


Reply 3 years ago on Introduction

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