Simple 1 button GPIO on off switch: 1 switch, 2 wires, 10 lines of code

Home Forums RetroPie Project GPIO Adapter, ControlBlock etc. Simple 1 button GPIO on off switch: 1 switch, 2 wires, 10 lines of code

RetroPie has a new website and forum. Please visit https://retropie.org.uk/ for the new site. The new forum is located at https://retropie.org.uk/forum/. This forum is left here as a read-only archive.

This topic contains 20 replies, has 5 voices, and was last updated by Profile photo of spaceinvader spaceinvader 1 year ago.

Viewing 21 posts - 1 through 21 (of 21 total)
  • Author
    Posts
  • #94854
    Profile photo of kiwijam777
    kiwijam777
    Participant

    Shutdown and power-up button using only a button and 2 wires connected to GPIO! NO resistors, python script , downloading libraries, loading drivers, use of C++, polling pins! There were too many overly complicated shutdown buttons; I found the most simple solution by inderpreets with a nice modification by TOMAS. Huge credit to you two! https://embeddedcode.wordpress.com/2013/10/18/adding-a-shutdown-button-to-the-raspberry-pi/

    NO NEED FOR RESISTORS!
    inderpreets – “The Pi’s GPIOs have in built pull up resistors and hence you don’t need an external pull up. This means that without any external connections, the GPIO will be read as a ‘1’ or high. As soon as you connect the GPIO pin to ground by pressing the button between the pins, it reads it as a ‘0’ or LOW. This status is read by the script and issues the shutdown command and viola!”

    SETUP
    For this example, I used a common arcade button microswitch I had laying around and connected one wire to a ground and another wire to GPIO 3 (you can use any GPIO pin).

    ON BUTTON!!!!!!!!!
    As part of RPI default behavior,if you complete pins 5 (GPIO3) and a ground (like pin 6) your RPI will power up. By taking advantage of the default GPIO 3 power-up behavior, and writting the shutdown code below for GPIO 3, you can use the same button to both power on and power off your RPI!!!!

    CODE “BOURNE AGAIN SHELL”:
    inderpreets – “The working of the bash script is quite simple and the comments explain the working of the script. It is basically monitoring the button for a press.” To make it work, it takes 2 steps: 1. Create a shutdown.sh file using nano. 2. Add the command to run shutdown.sh on startup in the background.

    1. CREATE FOLDER AND FILE:
    mkdir scripts
    sudo nano /home/pi/scripts/shutdown.sh
    (SEE 2 OPTIONS BELOW FOR CODE TO USE FOR THE SHUTDOWN.SH FILE. I USED OPTION B BECAUSE IT WAS SHORTER.)

    —OPTION A—inderpreets CODE FOR SHUTDOWN.SH FILE—–
    #!/bin/bash
    # monitor GPIO pin 3 (wiringPi pin 3) for shutdown signal
    # export GPIO pin 3 and set to input with pull-up
    echo “3” > /sys/class/gpio/export
    echo “in” > /sys/class/gpio/gpio3/direction
    echo “high” > /sys/class/gpio/gpio3/direction
    # wait for pin to go low
    while [ true ]
    do
    if [ “$(cat /sys/class/gpio/gpio3/value)” == ‘0’ ]
    then
    echo “Raspberry Pi Shutting Down!”
    halt &
    exit 0
    fi
    sleep 1
    done
    ———-END OF CODE FOR SHUTDOWN.SH FILE———
    OR
    ——-OPTION B—TOMAS CODE FOR SHUTDOWN.SH FILE——–
    #!/bin/bash
    echo “3” > /sys/class/gpio/export
    echo “in” > /sys/class/gpio/gpio3/direction
    echo “high” > /sys/class/gpio/gpio3/direction
    while [ “$(cat /sys/class/gpio/gpio3/value)” != ‘0’ ]
    do
    sleep 5
    done
    poweroff &
    exit 0
    ———-END OF CODE FOR SHUTDOWN.SH FILE———

    CLOSE AND SAVE SHUTDOWN.SH AND TEST THE BASH SCRIPT!!!!!
    sudo chmod 755 /home/pi/scripts/shutdown.sh
    sudo sh /home/pi/scripts/shutdown.sh
    AT THIS POINT YOU WILL SEE A CURSOR OR UNDERSCORE, IT IS WAITING FOR YOU TO PRESS/HOLD THE BUTTON. PRESS/HOLD THE BUTTON SO SEE THE SCRIPT START UP AND SHUT YOUR SYSTEM DOWN.

    2. ADD SCRIPT TO LOAD AT STARTUP BY ADDING COMMAND TO /ETC/RC.LOCAL:
    sudo nano /etc/rc.local
    TYPE THE FOLLOWING IN RC.LOCAL AFTER THE LINE “#By default this script does nothing”:
    sudo /home/pi/scripts/shutdown.sh &
    MAKE SURE YOU USE ‘&’ AT THE END OR YOU SYSTEM WILL GET STUCK ON STARTUP WAITING FOR YOU TO PUSH THE SHUTDOWN BUTTON, RENDERING YOUR PI USELESS; ‘&’ MAKES IT RUN IN THE BACKGROUND.

    SAVE THE FILE AND REBOOT.

    • This topic was modified 2 years ago by Profile photo of kiwijam777 kiwijam777.
    • This topic was modified 2 years ago by Profile photo of kiwijam777 kiwijam777.
    • This topic was modified 2 years ago by Profile photo of kiwijam777 kiwijam777.

    #95928
    Profile photo of tombofix
    tombofix
    Participant

    Amazing! Fitted with an old reset switch off a PC as they fit perfectly across the pins on a Pi 2.

    Things to note:
    Make sure your quote marks are the quote marks generated by nano. If you copy and paste the code your quotes and apostrophes will be incorrect and the script won’t run. It’ll typically result in an error about the directory not existing. If they look like they have curls then they aren’t correct.

    rc.local should be
    sudo sh /home/pi/scripts/shutdown.sh

    I also found that I needed to install the GPIO components on my build before they worked.

    sudo apt-get update
    sudo apt-get remove python-rpi.gpio python3-rpi.gpio

    Mine now works beautifully, can’t thank you enough as feel it is ready to be fitted in to a console!

    • This reply was modified 2 years ago by Profile photo of tombofix tombofix.
    • This reply was modified 2 years ago by Profile photo of tombofix tombofix.
    • This reply was modified 2 years ago by Profile photo of tombofix tombofix.
    #95934
    Profile photo of kiwijam777
    kiwijam777
    Participant

    what version of RPI are you using? I have used the code above in its exact form on RPI2 and RPI B+, I have used it on the latest beta version of retropie and a 6 month old version of retropie. In all cases, I used the code as stated above and it worked, quotes and all. Also in all cases rc.local had to have the ampersand at the end or the pi would never fully boot because it would be stuck waiting for the shutdown command. I needed rc.local to be exactly:
    sudo /home/pi/scripts/shutdown.sh &

    #121576
    Profile photo of snoogens
    snoogens
    Participant

    has anyone gotten this to work with RetroPie SD-card image Version 3.6 for Raspberry Pi 2 and 3.
    I’m new to all this and I followed the instructions and it still will not work.

    #121579
    Profile photo of kiwijam777
    kiwijam777
    Participant

    It worked for me until it didn’t for one particular switch. After troubleshooting I found out there was an error in the code; it didn’t need the line to go “high” since the 6 pins on that end are always high. Try this one:
    Create a file called “/home/pi/scripts/shutdown.sh”
    Inside the file, type.

    #!/bin/bash
    echo “3” > /sys/class/gpio/export
    echo “in” > /sys/class/gpio/gpio3/direction
    while [ “$(cat /sys/class/gpio/gpio3/value)” != ‘0’ ]
    do
    sleep 1
    done
    poweroff &
    exit 0

    Remember you still have to refer to this file in RC.local

    ADD SCRIPT TO LOAD AT STARTUP BY ADDING COMMAND TO /ETC/RC.LOCAL:
    sudo nano /etc/rc.local
    TYPE THE FOLLOWING IN RC.LOCAL AFTER THE LINE “#By default this script does nothing”:
    sudo /home/pi/scripts/shutdown.sh &

    If you still can’t get it, I will email you the files.

    #121585
    Profile photo of snoogens
    snoogens
    Participant

    when I try to test the file with
    sudo chmod755 /home/pi/scripts/shutdown.sh
    sh /home/pi/scripts/shutdown.sh
    I get this
    : not foundripts/shutdown.sh: 1 /home/pi/scripts/shutdown.sh:
    : permission deniedutdown.sh: 5: /home/pi/scripts/shutdown.sh: cannot creat /sys/class/gpio/export
    : Directory nonexistantwn.sh Syntax error: end of file unexpected (expecting “do”)

    I am very new to programing and have no idea why I am getting this error.
    Thanks for any help you can be.

    #121587
    Profile photo of kiwijam777
    kiwijam777
    Participant

    Are you sure you named the file correctly? Check by typing:
    ls /home/pi/scripts/
    <enter>
    Do you see it? Is it spelled correctly?

    If not, Did you first create the folder and then the shutdown.sh file?

    cd /home/pi/
    <enter>
    sudo mkdir scripts
    <enter>
    sudo nano shutdown.sh
    <enter>
    From here, you are in the file where you put all the text above. Exit and save. Now you are back at the command line. Try this to change permissions:
    sudo chown pi shutdown.sh
    <enter>
    Now test it out again.

    #121589
    Profile photo of snoogens
    snoogens
    Participant

    ok, I checked and the folder and the file are where they belong.
    I did the sido chown pi shutdown.sh
    and ran my test again…
    I get the same response as before.

    : not foundripts/shutdown.sh: 1 /home/pi/scripts/shutdown.sh:
    : permission deniedutdown.sh: 5: /home/pi/scripts/shutdown.sh: cannot creat /sys/class/gpio/export
    : Directory nonexistantwn.sh Syntax error: end of file unexpected (expecting “do”)

    #121615
    Profile photo of spaceinvader
    spaceinvader
    Participant

    I can’t get this to work too.

    When doing the test get

    sh: echo: I/O error

    And after booting also does not work.

    Don’t know what I am doing wrong…

    #121989
    Profile photo of kiwijam777
    kiwijam777
    Participant

    Here are copies of my files, in the attached zip:

    /etc/rc.local
    /home/pi/scripts/shutdown.sh

    I drop these into every new pi i have, and then i reboot and they work. If they dont work for you i am not sure what to suggest. Do you have the the folders “/sys/class/gpio”? If not, I think you can do:

    sudo apt-get update
    and
    sudo apt-get install rpi.gpio

    #122044
    Profile photo of skizz
    skizz
    Participant

    I’d like to confirm what spaceinvader is seeing.

    My scripts are copies of kiwijam777’s

    I’ve done sudo apt-get update and sudo apt-get install rpi.gpio

    Output is still:

    sh: echo: I/O error

    Are we all using the RPi 2 B+ and the latest RetroPie (3.6)?

    I can’t imagine earlier versions would cause issues in something basic as writing a script in nano.

    Is it a privilege issue? Your previous code had mkdir scripts without sudo in front so I don’t know if that caused an issue (I’m not a Linux expert so forgive me if the questions seem dumb).

    I’m still searching for the issue but I just wanted to let others know that it doesn’t work on my RPi 2 w/ RetroPie 3.6

    On a side note: Where is rc.local? I’m just looking through the pi’s directories with FileZilla so is it under etc/initd/rc.local? Or somewhere else?

    It seems

    • This reply was modified 1 year ago by Profile photo of skizz skizz.
    • This reply was modified 1 year ago by Profile photo of skizz skizz.
    #122116
    Profile photo of spaceinvader
    spaceinvader
    Participant

    Hi!

    I have transferred your files, and half work…

    the script Works when I test it, I don’t know what happened with my script, I think it was exactly the same, but… it doesn’t matter.

    The trouble is that it doesn’t work anytime. Despite the rc.local you uploaded was wrong (it says shutdownm.sh), I corrected it but it keeps on not working…

    I will carry on trying…

    #122117
    Profile photo of kiwijam777
    kiwijam777
    Participant

    Sorry about the misspell in RC.local. I have mine named something sightly different so I made a quick edit before I copied it over. Again, my apologies.

    With the switch you have to hold it down in order for it to turn off or on. I set the timing to 0 so it would be the quickest possible. Are you holding it down or just pressing it?

    And this is for a momentary switch by the way that could also be your problem. I have another version I use for a toggle on and off switch.

    #122118
    Profile photo of spaceinvader
    spaceinvader
    Participant

    I tried pressing and holding, and no way. I also tried putting “sudo sh” like in the test, and no way…

    by the way skizz, I have noted that althogh getting the I/O error message, the script works, try testing it and pressing the shutdown button.

    #122119
    Profile photo of skizz
    skizz
    Participant

    I didn’t have a proper jumper last night to short the two pins but I do now so I’ll test tonight and let you know spaceinvader.

    • This reply was modified 1 year ago by Profile photo of skizz skizz.
    #122165
    Profile photo of skizz
    skizz
    Participant

    I got mine to work with both shutting down and booting up the the Pi so kiwijam777’s code works.

    How are you shorting the two pins spaceinvader? I have a momentary button that I will solder on today but I used this to test last night at least.

    Hopefully you can get it going

    #122193
    Profile photo of spaceinvader
    spaceinvader
    Participant

    Skizz, it works to you while testing or at any time?

    #122199
    Profile photo of spaceinvader
    spaceinvader
    Participant

    Now working!
    Don’t know what the hell was doing wrong before…
    Thanks for the help.

    #122204
    Profile photo of kiwijam777
    kiwijam777
    Participant

    So what did you guys do differently to get it to work without the I/o error?

    #122217
    Profile photo of skizz
    skizz
    Participant

    The I/O error only appeared when doing a sudo sh /home/pi/scripts/shutdown.sh command but as spaceinvader said the code would still work even with the error output so I’m not sure if it matters since echo I/O is a very vague error in linux apparently.

    I think the big part was shorting the two pins longer than half a second. It must be the TTL logic firmware for the GPIO that needs longer than a certain pulse width to see a HIGH or LOW logic. Simply put, I was impatient haha.

    I had to hold my momentary button at least a full second (1 mississippi) to get it to boot/poweroff.

    Also if you guys want a to wire an external LED to show whether the Pi is on or off just wire the cathode (+) of a regular old LED with a resistor (500-1000 ohm is fine) to pin 8 (UART TXD) and then wire anode (-) to GND. That pin automatically pulls LOW when OFF and HIGH when ON so the LED will light accordingly and you don’t need to program or script anything (woot).

    Thanks for the help and discussion.

    • This reply was modified 1 year ago by Profile photo of skizz skizz.
    #122220
    Profile photo of spaceinvader
    spaceinvader
    Participant

    I don’t know what I was doing wrong, I started again with a fresh sd image and got it to work.

Viewing 21 posts - 1 through 21 (of 21 total)

Forums are currently read only - please visit the new RetroPie forums at https://retropie.org.uk/forums/

Skip to toolbar