Simple Raspberry Pi Shutdown Button

243,734

648

68

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.

8 People Made This Project!

Recommendations

  • Water Contest

    Water Contest
  • Metalworking Contest

    Metalworking Contest
  • Fix It! Contest

    Fix It! Contest

68 Discussions

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.

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:

try:
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.
time.sleep(1.5)

#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
else:
os.system("sudo reboot -h now")
except:
pass

GPIO.cleanup()


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

IMG_20180212_002817.jpgIMG_20180212_003136.jpg

Works perfectly. Thanks!

note:

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

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

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

to

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.

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

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

Legend!

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

0
None
BoboC6

1 year ago

Hi,

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

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.

hi..is there a way to make his on gpio 23?

0
None
cmelyo

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

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.

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

6 replies

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

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

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.

Ray

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

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

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