Shutdown button using gpio pin
  • Hi, I,m quite new to this forum, but need someone's help in getting raspberry pi to shutdown properly using a button connected to the gpio pins. Have followed the tutorial by Adafruit regarding building a mame emulator and all is well apart form the above issue. Have built coffee table mame cabinet and installed joystick and buttons and have gotten the mame to autostart when powered up. Buttons and joystick all work and also have a key escape button which when pressed goes back to the mame menu allowing user to choose another game, a 2nd press closes mame and waits for a input from the terminal, I then have to get the keyboard out and type sudo shutdown -h now. Can any one help
  • The message board won't let me post an answer,maybe because it's too long. I'll try to break it up into parts...
  • From the FAQ...


    Shutdown Switch
    ---------------
    To install python for shutdown switch:
    sudo apt-get install python-dev
    wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.1a.tar.gz
    untar and install with "sudo python setup.py install"
  • SET GPIO#17 to be internally pulled up, then need no resistors and only 2 wires
    Connect one side of button to gnd and other to GPIO#17 - i.e. shorts pins 9 and 11.

    Script to monitor GPIO #17:
    [Save as /home/pi/bin/shutdown.py]

    import RPi.GPIO as GPIO
    import time
    import os
    GPIO.setmode(GPIO.BCM)
    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")
    break
    time.sleep(1)

  • Edit file /etc/rc.local and just before last line add..
    python /home/pi/bin/shutdown.py &
  • Cheers. I will try this later this week. Many thanks for your reply. Let you know how I get on
  • Just a quickie, do I untar file first and then install with "sudo python setup.py install" or if I just run this command will it untar and install it? sorry to be a pest.
  • Managed to install python setup, but now run up against another problem. Inputted you script into "idle" and saved it in "home/pi/shutdown.py", couldn't find home/pi/bin/. Edit the file in etc/rc.local. Restarted pi and error message pops up saying invalid syntax line 1 "import RPi.GPIO as GPIO" what am I doing wrong.
  • Sounds like the GPIO python class didn't install properly. Did you get any errors when installing it.

    Does it give the same error if you run from the command line i.e.
    python /home/pi/shutdown.py

    (yep, I manually created a "bin" directory. Anywhere should work)
  • probably need to do:
    sudo python /home/pi/shutdown.py (to run as root)
  • Also, if you copied and pasted the script from this message board straight into an editor it may have hidden control characters in.
    Best to start with an empty editor session and key all script in manually.
  • Yep, well spotted. The formatting above in your post matches the script I am using.

    I am using the version of python that comes ready installed on Pimame - 2.7 ? (Shea?)

    I don't have my Pi here to check.
  • johnnygal,
    when I perform this - it is polling and showing the value "true" repeatedly in the pimame menu and at command line. Is there a way to prevent this from displaying?
  • Nevermind - i commented out the "print" statement.

    Nice script - works awesome!
  • johnnygal,
    After successfully doing this, i was wondering if the new RPi-GPIO will work with the new interrupts. It doesn't require polling.
    http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio

    What do you think?
  • Whoops, yep, sorry. I had left a "debug" print line in from when I was testing.

    That looks interesting regarding the interrupt driven GPIO library. I suppose it should reduce cpu usage. The arcade machine I built is now with my nephew but I may do some testing on another Pi when I get the chance. I didn't notice any slowdown on mame, although I was using mame4all with the old romset which is good for performance anyway.
    Mind you, there is a 1 second sleep on every poll. so won't be utilising cpu. But using an interrupt means it should be more responsive to the press of the shutdown button.

    Cheers,
  • To install python for shutdown switch:
    sudo apt-get install python-dev
    wget http://pypi.python.org/packages/source/R/RPi.GPIO/RPi.GPIO-0.4.1a.tar.gz
    untar and install with "sudo python setup.py install"


    Is there a way off doing this first part if my pi is not hooked up to the net? can i download the file on my mac and then transfer it over with a usb stick and then install?
    will it not work by just creating the shutdown.py script and adding it to rc.local?
    thanks

  • Hi adamrobots,
    "Is there a way off doing this first part if my pi is not hooked up to the net? can i download the file on my mac and then transfer it over with a usb stick and then install? "
    Yes, that will work. The wget is just to download the file.

    "will it not work by just creating the shutdown.py script and adding it to rc.local?"
    Yes, that the way I did it. The first part is to install python which wasn't installed by default in the version of PiMame I was using (don't know if python is there now in current version of PiMame)
  • I see i thought python was installed as standard. I cant see any reference to it being included in any updates so I have downloaded it.
  • Sorry, I should have said it's the GPIO library for python that really needs installing.
  • im looking a momentary push buttons and there seem to be many variations with different volts/amps etc. do i need a certain one or will any do the trick? and im right in saying that no resistors are required?
  • iv read that if you set pin to be internally pulled up you need no resistors... is this something you physically have to do to the pins or is just written in the script/software?
  • Any one of them will do the trick, and you don't need any resistors. Setting the pull up/pull down resistor is something that you do in the code. If you are just using one of the non-power pins, you don't need to worry about that. You should check out this page for more info on the GPIO's as well as a few examples of setting a power pin to an input/output. http://elinux.org/RPi_Low-level_peripherals#Internal_Pull-Ups_.26_Pull-Downs
  • thanks im having a read now.
    appologies if this sounds stupid but can i use this type of switch
    http://www.ebay.co.uk/itm/12v-Push-Button-Switch-Momentary-ON-OFF-SPST-Car-Ignition-Engine-Start-Horn-/250955228751?pt=UK_CarsParts_Vehicles_CarParts_SM&hash=item3a6e18e64f#ht_2514wt_990

    its listed as 12v so do i actually need to supply it with power or can i use non-power pins?
    theres also some i am looking at listed as 125/250v. or would it be better going for a micro switch like this...
    http://www.ebay.co.uk/itm/Quality-Momentary-Tactile-Push-Button-Switch-SPST-Miniature-Mini-Micro-Small-PCB-/151263723213?pt=UK_BOI_Electrical_Components_Supplies_ET&var=&hash=item2338053ecd#ht_297wt_752

    preferably i would like to use non-powered pins with a good looking button..

  • You can use the first one (or the second). You don't need to supply power to it. The reason that it says 12v is because it is rated for 20a @ 12v DC. That's pretty much the MAX that you would want to put through that guy, any more and you would fry it. Luckily though, the raspberry pi puts no where near that amount of current through it. Basically, you can think of a momentary push button as two wires that are bare at the ends. When you push the button, it's the same as quickly touching the two wires together. Since you are only going to be plugging this button into gpio pins, you can get pretty much anything that you want. If you were planning on wiring one of these buttons to say, a light switch in your house (not that you would), that is when you would need to worry about the electrical rating as you would want to make sure that too much current isn't running through the switch and melting it.
  • thanks mholgatem

    I thought that to be the case i just wanted to make sure before i put the order in because all of the reviews i have seen show the small micro switches being used.
    Geting close to completing it now thanks again for your assistance......
  • yup, no problem. I know how it can be. I asked around on like 3 different forums when I was looking for answers about how many led's I could run off the gpio pins. I was sure that I was going to fry something.
  • ok i have wired it all up and done all the above but no response from the button. when i type in command line
    sudo python /home/pi/bin/shutdown.py it keeps me in command line
  • i can't do anything so i don't think I'm even in command line but i can see previous commands but can't type any in. had to ctrl alt del to restart
  • you never guess what i have done, had it plugged into 11 and 13 rather than 9 & 11. all working. thanks mate
  • haha, can't tell you how many times I've done that sort of thing!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!