Reply To: Shut Down using GPIO

Home Forums RetroPie Project GPIO Adapter, ControlBlock etc. Shut Down using GPIO Reply To: Shut Down using GPIO

Profile photo of skizz

I found this thread awhile back concerning a shutdown script, so I’m going to apply the same principles here but adjust for a soft power disconnect (i.e low power mode).

Here’s the link but I copied the text below as well


Here is how to safely shutdown your RetroPie system via a switch wired to the GPIO pins:

List of items you will need:
raspberry pi running retropie (mine is running v3)
two wires with connectors to plug into the pin header, long enough to reach your switch

start by wiring up your switch. (this script uses GPIO17 to trigger the event, if you want to use a different pin, you will have to adjust the script.) connect the two wires from your switch to pin GPIO17 and a ground. You do not need to add any resistors.

connect to your raspberry pi via ssh. I used putty in windows. (there are many tutorials on this, basically install putty, enter retropie for the host name and click Open. log in with username pi, password raspberry)

you will first need to run the following commands to Install rpi.gpio (this is the part most of the other tutorials leave out)
sudo apt-get update
sudo apt-get -y install python-rpi.gpio

once that is done, we will create the script.

sudo nano /home/pi/

Highlight and copy the text below (stuff between the <><><><>). right-click in your puty window to paste the text.


import RPi.GPIO as GPIO
import time
import os

GPIO.setup(17, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
print GPIO.input(17)
if(GPIO.input(17) == False):
os.system(“sudo shutdown -h now”)


do a Ctrl X, press Y and then press Enter to save it.

then we have to tell the script to run but making an entry in crontab

sudo crontab -e

scroll to the bottom and add this line at the end:

@reboot sudo python /home/pi/

do a Ctrl X, press Y and then press Enter to save it.

now, launch your script manually to make sure it runs, enter the following command

sudo python /home/pi/

it should launch the script without any errors…then simply press your switch and it should send the shutdown command to your pi. let it shutdown and then power off the unit. now turn it back on, it should boot up to emulation station like normal, and when you press your switch, it should shut down.

If you followed those instructions, you should be set. i’ll try to help, but i’m really new to this stuff…so i don’t know how much help i will be.


Again, this isn’t exactly the way you want, but it describes the action of scripting a button interrupt. I’ll test soon and see what I get out of it

Skip to toolbar