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
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)
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.
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.
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?
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.
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)
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
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.
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