Posted on

RetroPie Shutdown and Startup Switch – The Easy Way

Tired of unplugging your Raspberry Pi to turn it off?

Don’t want to purchase one of those switched USB power adapters and have to shutdown your Pi from a menu every time? Do you also want an easy way to turn your Pi on?  There’s a script for that.  And you may have already seen it floating around on YouTube but there’s a lot of code you have to copy and paste to get it working.  So, want a push button that can turn on and safely shutdown your Pi, and an easy way to get the software working?  Well, here it is –

Hardware setup:

You will need to connect a momentary switch from pin 5 (GPIO 3) to pin 6 (GND).  Pin 6 tends to be the most convenient ground, but it doesn’t actually matter which one you use – pins 6, 9, 14, 20, or 25 will all work.  See the pinout if you’d like to use a different ground pin.

Raspberry Pi Pinout (all versions)

Pi 3 Connections:

Raspberry Pi 3 Reset Pins

Pi Zero Connections:

Raspberry Pi Zero Reset Pins

You can use pretty much any momentary switch that is normally open.  You can even use a reed switch for a magnet activated switch to avoid drilling holes in your case!

On Raspberry Pi 3, you will need to connect pin connectors (Dupont connectors) to the switch wires, or find a switch that already has them (just an example, not a recommendation).

If you need to add pin connectors to your switch, you can solder/splice on female Dupont wires.

Or, (especially if you plan to do multiples), you can crimp your own connectors with a kit like this.  While, it is *possible* to crimp those connectors with some regular pliers, it’s not really ideal so you’d also want to get a crimping tool.

Software setup:

Once you have your switch connected, you will need to install a bit of software to allow the switch to shut down your Pi (pressing the switch while the Pi is shut down will turn it back on without any software.) Your Pi needs to be connected to the internet to download the install script and packages.

Hat tips to Barry Hubbard who I believe is the originator of the Python script that makes this work, and to ETA Prime for creating an awesome how-to video that’s popularized this mod.  I used ETA Prime’s video to do this initially and realized that the process was more complicated and error prone than it needs to be.  So, I created a script that automates the entire software setup.

There are two ways you can run the setup script: using a keyboard plugged into your Pi, or over a network terminal session using your computer. Using your computer over the network was the preferred way to do this using the ETA Prime instructions due to the amount of copying and pasting required.  However, it’s might be easier to use a keyboard (at least in the UK) for this method unless you’re already set up to connect over the network.

Keyboard Method (UK):

  1. Press the start button on your game controller and exit emulationstation to get to the shell.
  2. Type the following and press enter:

curl https://pie.8bitjunkie.net/shutdown/setup-shutdown.sh | bash

That’s it! You’re done.

(Skip to testing section)


Keyboard Method (US):

Some distros including RetroPie have the keyboard layout set to GB by default, so there’s no way to type the | symbol on a US keyboard. So you can do this instead:

  1. Press the start button on your game controller and exit emulationstation to get to the shell.
  2. Enter the following commands (press enter after typing each line)

curl https://pie.8bitjunkie.net/shutdown/setup-shutdown.sh --output setup-shutdown.sh

sudo chmod +x setup-shutdown.sh

./setup-shutdown.sh

That’s it! You’re done.

(Skip to testing section)


SSH/Network Method:

Make sure your Pi is connected to the same network as your computer either via Wifi or Ethernet. For Pi Zero you will need a WiFi dongle.

Using Windows:

  1. Download putty.exe http://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html to your desktop (if you have a relatively modern computer, you probably want the 64-bit version)
  2. Start putty
  3. Enter ‘retropie’ as the host name, make sure SSH is checked, and click open.
  4. If you are presented with a security alert about the server’s host key, click yes.

Using Mac/Linux:

  1. Open the Terminal application.
  2. Type ‘ssh retropie’ and press enter.

Host not found / other error?

If you get an error like ‘host not found’, you may need to use the IP address vs the host name of the Pi.  To get the IP address: in RetroPi select the RetroPi channel and select Show IP.  Use the IP in place of ‘retropie’ in the previous instructions.

Once you’ve connected:

  1. Once you’ve connected to the Pi via SSH, you will be prompted for a username and password. The username is ‘pi’ and the password is ‘raspberry’ (all lower-case).
  2. You should now be at a shell prompt. Copy and paste the following command and press enter:

curl https://pie.8bitjunkie.net/shutdown/setup-shutdown.sh | bash

That’s it! You’re done.


Testing:

Now the shutdown script should be installed and set up to run every time the Pi boots.  To test it out, press the switch.  The Pi should shut down.  Press the switch again and the Pi should boot up.

Troubleshooting:

If your Pi didn’t shut down, shut it down manually.  Press the switch.  If your Pi doesn’t start up, there’s something wrong with your switch/wiring/connection.  If it does start up, wait for emulationstation to load and press it again.  If it doesn’t shut down, there’s something wrong with the software setup.  A good place to get help at that point would be the RetroPie gaming group on Facebook.

 


Copyright © 2017 8bitjunkie.net

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and limitations under the License.

41 thoughts on “RetroPie Shutdown and Startup Switch – The Easy Way

  1. Will this work if there is a rtc installed as well? The rtc board uses pins 1,3,5,7,9

    1. Probably not. PIN 5 is special in that it can wake the Pi up from deep sleep (“off”) so if your RTC board requires it, I don’t think the shutdown script will work.

  2. Will this work with an on/off switch not momentary?

    1. no, it needs to be momentary

  3. Can I use these with another OS like raspbian? Btw nice work!

    1. I think so

  4. That´s great, would this work on Recalbox too?

    1. I think so

  5. Where can I setup a hold time to signal power down?

    IE: PiCade Hat has a 3 sec hold time for shutdown while a momentary press to start up.

  6. I was able to modify this script to use a 3 sec hold to shutdown. here is the full moded script

    import RPi.GPIO as GPIO
    import time
    import subprocess
    from threading import Timer

    GPIO.setmode(GPIO.BOARD)

    SHUTDOWN_HOLD_TIME = 3 # Time in seconds that power button must be held

    # we will use the pin numbering to match the pins on the Pi, instead of the
    # GPIO pin outs (makes it easier to keep track of things)
    # use the same pin that is used for the reset button (one button to rule them all!)
    GPIO.setup(5, GPIO.IN, pull_up_down = GPIO.PUD_UP)

    def perform_shutdown():
    subprocess.call(“shutdown -h now”, shell=True,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE)
    oldButtonState1 = buttonState1

    oldButtonState1 = True

    while True:
    #grab the current button state
    buttonState1 = GPIO.input(5)
    global shutdown_timer
    # check to see if button has been pushed
    if buttonState1 != oldButtonState1 and buttonState1 == False:
    # shutdown
    shutdown_timer = Timer(SHUTDOWN_HOLD_TIME, perform_shutdown)
    shutdown_timer.start()

    time.sleep(.5)

    1. awesome, thanks!

    2. Question to Dan
      Is it something more to do except writing the script.
      I can’t get it to work the button is not responding.
      it worked before but not with the timer.

      Is it something to do with “from threading import Timer”.
      Uffe

      1. I am also curious about this issue as I also cannot get the code to work. Any insight or solutions would be greatly appreciated.

    3. Hi, I tried substituting your code for the code in the shutdown.py script but I cannot seem to get it to work.

      Is the way it is depicted in your text box the way I can cut-paste it into the script or are there changes I need to make to it on account of textwrapping in the comment box causing the code to appear other than how it is supposed to look in the editor. Thanks in advance!

    4. can you email this to me in a text file w/ the indenting intact? 8bitjunkie8@gmail.com

      1. can you send me that email as well? Thanks in advance!
        roachman5000@yahoo.com

  7. Unfortunately this does not work so well with the official pi 7″ monitor with touch enabled, since the monitor also uses those two pins for the touch interface.

    1. yeah, anything that uses pin 5(the official touch screen and several hifi berry sound cards) require a 2 button approach. just get the shutdown code and change pin 5 to the pin of your choice and connect one button to your new pin and ground. this button shuts down. connect another button to the “run” header on the pi and that will be “reset” or “power on” that button does not require a script.

      1. sounds like a great solution. thanks for posting

  8. I’d love to use this on my pi 3 running Libreelec but I can’t install the script automatically due to the sudo command not being used by default. Any ideas on how to overcome this? Great work!!!

    1. Try removing all the references to sudo or install and setup sudo

  9. I tried it with a recalbox, while install the script it says:

    Updating packages
    bash: line 36: sudo: command not found
    Installing python.gpio
    bash: line 40: sudo: command not found
    Downloading shutdown scripts
    bash: line 46: /home/pi/scripts/shutdown.py: No such file or directory
    ######################################################################## 100.0%
    bash: line 48: sudo: command not found
    chmod: /etc/init.d/pi_shutdown: No such file or directory
    Add pi_shutdown to rc.d
    bash: line 53: sudo: command not found
    Running shutdown listener
    Shutdown is now installed

    Nothing happend when i push the button any suggestions?

    1. Try editing the script to delete all of the `sudo` commands and then run it. Or, install and setup sudo.

  10. […] out this video by ETA Prime.  He uses the “easy button” install script I wrote and includes directions on how to install […]

  11. im having an issue where a momentary (tried several) hooked up to the pins will turn off the pi but only when pressed twice, shutting down works normal, but then at random intervals when the pi is “off” (sleeping) it turns on by itself… still puzzled…

    1. using “borkin” buttons? very high % of those are defective and need a resistor added to make them work properly

  12. Hey just wanted to say, this is awesome! Worked like a charm. Glad my frantic google searching paid off 🙂

  13. […] original source is at https://pie.8bitjunkie.net/retropie-shutdown-and-startup-switch-the-easy-way. I installed the script using SSH method. I am Windows user so I used […]

  14. Is there a trick to using the button ?
    Picked one up following the eta prime video
    Soldered up correctly
    Ran the script the link here
    Downloaded the script fine rebooted

    It works sometimes I can’t tell if I need to hold it or just press it sometimes I press it once quick it does nothing other times it works but usually after a couple of minutes of trying

    Any thoughts?

    1. I’ve heard that those buttons are often defective or unreliable. You might check that it’s operating correctly with a multimeter or try a different one. You should have to hold the button down no more than 0.5 seconds or so for it to register.

  15. I’m running the latest version of retropie and attract mode off of a hard drive with a small SD card for booting do I need to do anything fancy to the commands to shutdown the hard drive when shutting the system down with the button ( I’m super new at this) thanks in advance

    1. I don’t think so, but you may want to ask here: https://www.facebook.com/groups/726218244186933/

  16. this works great! but my fan stays running after i shutdown… anyway to get the fan to turn off as well?

    1. I can’t remember off the top of my head. You may need to control the fan with a transistor. ask here: https://www.facebook.com/groups/726218244186933/

  17. My pi wont stat at all after installing the script. i can see power light on the pi but it just wont do anything, no picture or sound, just nothing.

    1. can you check that your sd card is working (check on your computer) and inserted properly?

      what distro? retropie? what version?

  18. When I plug in a keyboard and type the commands, it doesn’t recognize the url. Am I supposed to be connected to internet, even for the keyboard method? I have a Pi Zero without wifi. If you need to be on wifi, why isn’t that in the instructions?

    1. Yes, how can you get to a URL without being connected to the internet? It wasn’t in the instructions because I must have thought that was obvious, but I’ve edited the instructions to make it more clear. Besides the initial download of the install script, there are several packages that must be downloaded and installed. I don’t know how this can be done offline but I’m sure there’s some linux wizardy that could make it possible. I used a $10 wifi usb dongle to do this on my non-wifi pi zeros

      1. OK, once I figure out how to go online, I’ll try again. For the keyboard method, once I type the script, does it automatically go online and download what it needs? Sorry, I’m a complete idiot when it comes to this. Never used linux before I got the pi a few days ago.

        1. yes, type the script, it does everything else

  19. I’m on NOOBS Jessie, not retropie. Will the script work, and can’t I just short the two pins manually with a screwdriver or similar (0.5s contact), instead of “installing” a commercial switch?

Leave a Reply

Your email address will not be published. Required fields are marked *