Shutting down RetroPie usually requires the use of a menu in the Emulation Station interface. This tutorial explains how you can do this via a shutdown button connected to the Raspberry Pi’s GPIO header. All you need is a basic momentary action switch, some wiring and a simple Python script. It’s probably the most useful upgrade you can make to a Pi gaming system.
Why Bother With a Shutdown Button?
I decided to implement a shutdown button on my IKEA arcade table to make it easier for kids to correctly shutdown the system without needing to mess around in the menus. Using a separate switch on the side ensured it wasn’t accidentally activated while playing games with the main player buttons.
Although this guide is especially relevant to RetroPie systems it could be used in any Raspberry Pi project.
The Shutdown Button
You can use any momentary action switch for this project. This allows you to hold it down and only trigger the shutdown if it has been held for a certain number of seconds. This avoids any accidental shutdowns especially if the system is being used by curious children.
The switch/button I used was an automotive momentary action device with an illuminated power symbol.
It was designed for 12V but the red LED inside works fine with 3.3V or 5V. Two connections were required for the LED and two for the switch which is used to change the state of a GPIO pin.
Remember that GPIO numbers are different to physical Pin numbers. GPIO22 is Pin 15.
|Switch Pin||Pi Header Pin|
|LED +||Pin 4 (5V)|
|LED –||Pin 6 (Ground)|
|NO (Normally Open)||Pin 15 (GPIO22)|
|C (Common)||Pin 17 (3.3V)|
The LED is powered from the Pi’s 5V supply. The GPIO pins should never be connected to anything higher than 3.3V. Double check the wiring to make sure you do not ever connect the GPIO pins to 5V. If your switch is not an illuminated type you can ignore the LED references in the table above.
Miniature spade terminals were used to attach wires to the switch. This avoided soldering anything to the switch and gave me the option of removing or replacing the switch at a later date.
Female “Dupont” style connectors allowed the wires to be easily connected to the GPIO header :
The diagram below shows how these wires were attached to the GPIO header. I used the pins shown below because they were next to each other and made the wiring a bit easier.
Getting to the Command Line
In order to setup the script you will need to use the command line. There are two methods you can use to do this.
If you connect a keyboard to your RetroPie system you can press F4 to bring up the command line. When you need to return to EmulationStation you simply run the command “emulationstation”.
You can also connect to the Pi via SSH. Please see the How To Enable RetroPie SSH Interface tutorial for details on enabling SSH. Once enabled you can connect using any SSH client on your PC, such as “Putty”.
In order to check the status of the button you need to install the GpioZero library. This is installed by default on the Raspbian image but for RetroPie it needs to be installed manually.
Run the following command :
sudo apt-get -y install python3-gpiozero
Add Shutdown Script to RetroPie
In order to perform the shutdown when the button is pressed you will need a simple Python script. The script is launched when the Pi boots and monitors the state of the GPIO pin. This can be downloaded directly to your Pi user’s home directory. I usually do this by connecting to the Pi via SSH from my PC. You can enable SSH by following the How To Enable RetroPie SSH Interface tutorial.
When connected to the Pi you can download the script using the following command :
cd ~ wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/retropie_shutdown.py
Now edit crontab so that the script is started when the Pi boots :
sudo crontab -e
If prompted to choose an editor select option 2.
Add the following line :
@reboot python3 /home/pi/retropie_shutdown.py &
Your crontab should now look like :
# For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 