RetroPie forum home
    • Recent
    • Tags
    • Popular
    • Home
    • Docs
    • Register
    • Login
    Please do not post a support request without first reading and following the advice in https://retropie.org.uk/forum/topic/3/read-this-first

    [TUTORIAL]Adding A Dedicated On/off Switch

    Scheduled Pinned Locked Moved Help and Support
    tutorialswitchturn offoffbutton
    5 Posts 4 Posters 15.5k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • retroprogrammerR
      retroprogrammer
      last edited by

      Hey guys here is a quick and easy tutorial on installing a dedicated shutdown button for your RaspberryPi for Retropie :)

      Parts list:
      . Momentary switch
      . 2 Male to female wires
      . RaspberryPi 3 with Retropie installed
      . Necessary connections for RaspberryPi (keyboard, mouse, etc.)

      Lets start:
      So first solder your male to female wires to your momentary switch, like so:

      0_1511249389333_Capture.PNG

      And then connect to GPIO pins 5 and 6 with your wires with this diagram:
      alt text

      Now go to the terminal by pressing and F4 on your keyboard and type the following:

      1)sudo apt-get install update
      2)sudo apt-get install python-dev
      3)sudo apt-get install python3-dev
      4)sudo apt-get install gcc
      5)sudo apt-get install python-pip

      Next you need to get RPi.GPIO:

      6)wget https://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.5.11.tar.gz

      Uncompress the packages:

      7)sudo tar -zxvf RPi.GPIO-0.5.11.tar.gz

      Move into the newly created directory:

      8)cd RPi.GPIO-0.5.11

      Now install the module by doing:

      9)sudo python setup.py install
      10)sudo python3 setup.py install

      Next we create a directory to hold the scripts:

      11)mkdir /home/pi/scripts

      call our script shutdown.py (it is written in python). Create and edit the script by doing:

      12)sudo nano /home/pi/scripts/shutdown.py

      This is the script, paste it in the blank area:

      #!/usr/bin/python
      import RPi.GPIO as GPIO
      import time
      import subprocess
      
      # 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)
      
      GPIO.setmode(GPIO.BOARD)  
      
      # 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)  
      
      oldButtonState1 = True
      
      while True:
          #grab the current button state
          buttonState1 = GPIO.input(5)
      

      Press CRTL X Then Y and restart the pi

      Now we configure our script to run at startup,

      14)sudo nano /etc/rc.local

      And add the following to the file by typing :

      sudo python /home/pi/scripts/shutdown.py &

      Press CRTL X Then Y

      Now the button should work after a reboot, thanks for using this tutorial and make sure to comment :)
      @Retroprogrammer

      Here is me converting the original Xbox for a case:
      https://retropie.org.uk/forum/topic/15232/converting-original-xbox-into-case-for-raspberrypi/15
      Check out my GitHub: https://github.com/retro-programmer

      1 Reply Last reply Reply Quote 2
      • mituM
        mitu Global Moderator
        last edited by

        A few observation to your instructions:

        • You don't need to install both Python3 and Python2 modules (steps 9 - 10), it's enough to install the modul for the default Python on the system
        • I think the RPi.GPIO library is available as a package (python-rpi.gpio) so it should be installable with a simple apt-get install python-rpi.gpio on a Raspbian system.
        • The sudo in step 14, for running the script on startup, is not needed, since the commands there are executed as root anyway on start.
        • The script you posted seems incomplete, there is no shutdown command attached to the GPIO toggle, so your script will loop ad infinitum and not do anything.
        • The script will also consume 1 CPU core 100%, since it will run in a tight loop forever. I find the approach described in http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3 to be the best, since it's using almost no CPU.
        • Most importantly, there is no provisioning for shutting down EmulationStation cleanly, there have been numerous posts here with solutions for how to safely shutdown a RetroPie system, maybe you should incorporate that.
        1 Reply Last reply Reply Quote 0
        • retroprogrammerR
          retroprogrammer
          last edited by

          @mitu Thanks for the observations, I did this with what I knew I will add dome provisions for safer shutdown thanks :)

          Here is me converting the original Xbox for a case:
          https://retropie.org.uk/forum/topic/15232/converting-original-xbox-into-case-for-raspberrypi/15
          Check out my GitHub: https://github.com/retro-programmer

          1 Reply Last reply Reply Quote 0
          • Beretta9B
            Beretta9
            last edited by

            Retro Gamer Supreme

            1 Reply Last reply Reply Quote 0
            • K
              KlaxMaster
              last edited by KlaxMaster

              @retroprogrammer
              sudo apt-get update
              not
              sudo apt-get install update

              1 Reply Last reply Reply Quote 0
              • First post
                Last post

              Contributions to the project are always appreciated, so if you would like to support us with a donation you can do so here.

              Hosting provided by Mythic-Beasts. See the Hosting Information page for more information.