Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.
Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".
Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.
Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?
I assume there must be some way to have:
1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v
How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?
Re: What's the simple and safe way to do a "shutdown" button
no need to have x running to show images
why shut it down - leave it running all the time?
or
have it powered from the TV and boot the FS as readonly so it never matters it it gets switched off ?
why shut it down - leave it running all the time?
or
have it powered from the TV and boot the FS as readonly so it never matters it it gets switched off ?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe
WARNING - some parts of this post may be erroneous YMMV
1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe
Re: What's the simple and safe way to do a "shutdown" button
Ahem ... simply pull the plug out?
Might upset the purists but I've not had any subsequent SD corruption so far. And I do take the precaution of always backing up the image to PC so I can instantly re-image another SD card should the worst happen (or having a spare in the pocket).
KISS is the acronym so many IT people forget
Might upset the purists but I've not had any subsequent SD corruption so far. And I do take the precaution of always backing up the image to PC so I can instantly re-image another SD card should the worst happen (or having a spare in the pocket).
KISS is the acronym so many IT people forget

Re: What's the simple and safe way to do a "shutdown" button
What's the best (light) pi-compatible cli slideshow application? qiv seems to work very well but wasn't happy without x.RaTTuS wrote:no need to have x running to show images
I have to say I've had no problems doing that at home...brainsys wrote:Ahem ... simply pull the plug out?
Not a bad idea...if I knew how to do it!RaTTuS wrote:boot the FS as readonly so it never matters it it gets switched off ?
Then again, I'd quite like to know how to progress my original idea - as much for the information value as the end product.
edit: this might be what I need: http://www.cl.cam.ac.uk/~db434/raspi/bu ... _switches/
Re: What's the simple and safe way to do a "shutdown" button
Yes, that is just what you need to do a real "shutdown button"
It is actually so simple that you don't need a breadboard, just a way to connect to two GPIO pins. Just connect a pushbutton to a GPIO pin and to ground.
Confugure the GPIO pin as input and with pull-up resistor enabled. Then it will read 1 normally and 0 when the button is pressed.
Now all you need is software to monitor that pin and do a shutdown when it sees a 0.
This could be anything from a full blown daemon written in C to a Python program or even just a bash script.
Whatever method you choose, run in the background, started from /etc/local.rc, and you should be good to go.
It is actually so simple that you don't need a breadboard, just a way to connect to two GPIO pins. Just connect a pushbutton to a GPIO pin and to ground.
Confugure the GPIO pin as input and with pull-up resistor enabled. Then it will read 1 normally and 0 when the button is pressed.
Now all you need is software to monitor that pin and do a shutdown when it sees a 0.
This could be anything from a full blown daemon written in C to a Python program or even just a bash script.
Whatever method you choose, run in the background, started from /etc/local.rc, and you should be good to go.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com
http://raspberrypihobbyist.blogspot.com
-
- Posts: 7
- Joined: Fri Sep 28, 2012 9:13 pm
Re: What's the simple and safe way to do a "shutdown" button
GPIO pins are 3.3V IIRC, so whatever you decide to do, use the 3.3V rail, not the 5V onea push-button linked to +5v pin and a gpio pin

- mahjongg
- Forum Moderator
- Posts: 15056
- Joined: Sun Mar 11, 2012 12:19 am
- Location: South Holland, The Netherlands
Re: What's the simple and safe way to do a "shutdown" button
Yes putting 5V on a GPIO pin will actually put 5V on the 3V3 power supply blowing everything 3V3 powered up, not a good idea! 
In any case just put a small (220 Ohm or so) resistor in series with the switch, and things will no blow up.
My preference would be to use a 10K pullup to 3K3, on the GPIO pin, and from that same pin a switch with 220 Ohm in series to GND. The pullup will pull the pin up to 3V3, and when pressing the button it will go to near zero volts. If you accidentally program the GPIO to output a high, pressing the button doesn't damage the GPIO by shorting the high signal to ground, but will just draw a little current, not enough to do any damage.

In any case just put a small (220 Ohm or so) resistor in series with the switch, and things will no blow up.
My preference would be to use a 10K pullup to 3K3, on the GPIO pin, and from that same pin a switch with 220 Ohm in series to GND. The pullup will pull the pin up to 3V3, and when pressing the button it will go to near zero volts. If you accidentally program the GPIO to output a high, pressing the button doesn't damage the GPIO by shorting the high signal to ground, but will just draw a little current, not enough to do any damage.
Re: What's the simple and safe way to do a "shutdown" button
Note: I have little electronics background.. It is one of the reasons why I got a Raspberry Pi so I could learn.
I've been looking into doing this. this is what I was told could work:

Raw code which I have not yet tried yet...
https://raw.github.com/gist/3610105/3ce ... ensor.bash
Was planning on giving this a shot when I got my bread board setup. Still waiting for part.
Script look for the button press, then issues a shutdown. Of course, the power light is still on after the shutdown.
I've noted that when the RPi shuts down in flashes what looks to be 8 blinks on the ACK led just before it is done.
I've been looking into doing this. this is what I was told could work:

Raw code which I have not yet tried yet...
https://raw.github.com/gist/3610105/3ce ... ensor.bash
Was planning on giving this a shot when I got my bread board setup. Still waiting for part.
Script look for the button press, then issues a shutdown. Of course, the power light is still on after the shutdown.
I've noted that when the RPi shuts down in flashes what looks to be 8 blinks on the ACK led just before it is done.
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
My RPI Info Pages including Current Setup - http://rpi.tnet.com
Re: What's the simple and safe way to do a "shutdown" button
Great thread, I have the exact same needs with a Pi running a video display. Still waiting on my Pis to come in and I'll get started on my version of this. I'm still figuring out how to display the photos lol, but I'm sure as I read more I'll get it figured out.
I may get the GPIO cable so I can just short the correct pins together with the resistor itself as needed. Should be simple enough.
I may get the GPIO cable so I can just short the correct pins together with the resistor itself as needed. Should be simple enough.
Re: What's the simple and safe way to do a "shutdown" button
Enough stuff arrived in the post today to test this out:
1) Electronic starter kit - including a breadboard and resistors needed: http://www.ebay.co.uk/itm/110887787457 (£9.99) It doesn't include any switches.
2) Female/Male Jumper leads: http://www.ebay.co.uk/itm/130761796863 (£4.40 for 40)
3) I also scrounged a momentary switch from the inside of a broken toy one of my children bought in a jumble-sale...
Jumpers attached to +3.3v, ground, and GPIO17 (as detailed on the wiki. Circuit with switch, 10K pull-down resistor and 1K current limiting resistor exactly as on the link posted previously

Python script placed in /home/pi/bin/button
Launch of code added to /etc/rc.local on the line before "exit 0" :
Press button for 1 second to ensure it's polled, and the pi shuts down. Perfect.
1) Electronic starter kit - including a breadboard and resistors needed: http://www.ebay.co.uk/itm/110887787457 (£9.99) It doesn't include any switches.
2) Female/Male Jumper leads: http://www.ebay.co.uk/itm/130761796863 (£4.40 for 40)
3) I also scrounged a momentary switch from the inside of a broken toy one of my children bought in a jumble-sale...
Jumpers attached to +3.3v, ground, and GPIO17 (as detailed on the wiki. Circuit with switch, 10K pull-down resistor and 1K current limiting resistor exactly as on the link posted previously

Python script placed in /home/pi/bin/button
Code: Select all
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
while True:
if(GPIO.input(17)):
os.system("sudo shutdown -h now")
break
time.sleep(1)
Code: Select all
python /home/pi/bin/button
Re: What's the simple and safe way to do a "shutdown" button
Well done. Looks like a tight squeeze to get it all in the case.
Alex Eames RasPi.TV, RasP.iO
Re: What's the simple and safe way to do a "shutdown" button
Trying to figure out what you have connected.piglet wrote:Hurrah!
1 wire to the 3.3v line
1 wire with a resistor to Ground and a resistor to GPIO17.
Dweeber A.K.A. Kevin...
My RPI Info Pages including Current Setup - http://rpi.tnet.com
My RPI Info Pages including Current Setup - http://rpi.tnet.com
Re: What's the simple and safe way to do a "shutdown" button
He shows it here...
http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137
and gives a link to where he got the info.
http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137
and gives a link to where he got the info.

Alex Eames RasPi.TV, RasP.iO
-
- Posts: 19
- Joined: Fri Jan 11, 2013 11:11 pm
Re: What's the simple and safe way to do a "shutdown" button
I've been looking to do something similar; I want to use gpio buttons to navigate through raspbmc. Any ideas on how to do that?
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Great thread, and good info from all, especially Piglet - thankyou.
I have bought and installed a momentary switch in my case, and purchased the 1K and 10K resistors.
Im comfortable soldering (and adding header plugs to slide onto the GPIO pins), but Im not quite 100% on what to connect to what.
Can someone spell it out a little simpler for me please, so I don't muck it up please?
I have bought and installed a momentary switch in my case, and purchased the 1K and 10K resistors.
Im comfortable soldering (and adding header plugs to slide onto the GPIO pins), but Im not quite 100% on what to connect to what.
Can someone spell it out a little simpler for me please, so I don't muck it up please?
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Here's a photo of my switch.
I just drilled a hole with the correct size bit in my ModMyPi case. I chose the end, so it will not accidentally have anything placed on top of it, and I can stack Pi's in cases if need be.

I am not 100% sure if I have the right resistors or not - I'd appreciate if someone could let me know? (Until then, don't take this as a gospel parts list).
Switch $4.90 for one: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
1K resistor 53cents for 2: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
10K resistor 55 cents for 8: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
(The Jaycar site is just showing stock pics, the actual items are below...)

I just drilled a hole with the correct size bit in my ModMyPi case. I chose the end, so it will not accidentally have anything placed on top of it, and I can stack Pi's in cases if need be.

I am not 100% sure if I have the right resistors or not - I'd appreciate if someone could let me know? (Until then, don't take this as a gospel parts list).
Switch $4.90 for one: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
1K resistor 53cents for 2: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
10K resistor 55 cents for 8: http://jaycar.co.nz/productView.asp?ID= ... rm=KEYWORD
(The Jaycar site is just showing stock pics, the actual items are below...)

Re: What's the simple and safe way to do a "shutdown" button
Looks good to me. No just follow this: http://www.cl.cam.ac.uk/~db434/raspi/bu ... _switches/
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Thanks.
For others: my parts list above works out fine, and as a n00b, I followed instructions here:
http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/
Helped with a diagram here:
http://www.raspberrypi-spy.co.uk/2012/0 ... -and-pins/
And my only mistake was connecting to "Pin 17" instead of "GPIO17" (Which is actually Pin 11).
Works perfectly, and I have this code running, that I will add to /etc/rc.local
<removed CPU eating code lol>
My code needs work.
For others: my parts list above works out fine, and as a n00b, I followed instructions here:
http://www.3cc.org/blog/2013/01/raspber ... ff-the-pi/
Helped with a diagram here:
http://www.raspberrypi-spy.co.uk/2012/0 ... -and-pins/
And my only mistake was connecting to "Pin 17" instead of "GPIO17" (Which is actually Pin 11).
Works perfectly, and I have this code running, that I will add to /etc/rc.local
<removed CPU eating code lol>
My code needs work.
Re: What's the simple and safe way to do a "shutdown" button
Just have a backup image on another SD card. Yank the power on a daily basis. If you are unlucky enough to suffer corruption to the SD card data, employ the back-up SD-card and re-image the now defunct one.
When I really *try* and corrupt data cards, I can succeed almost all of the time. With an application like yours, I can't imagine a problem occurring more than monthly at most. I have no data as yet to back this up though. Sorry.
/Dextrus
When I really *try* and corrupt data cards, I can succeed almost all of the time. With an application like yours, I can't imagine a problem occurring more than monthly at most. I have no data as yet to back this up though. Sorry.
/Dextrus
Have more FUN with your Pi. Visit www.pi-fun.com
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Why are you sorry? Do you think those of use who are doing this have wasted our time?
Our needs are different to yours.
I need to be able to move my Pi around at will - its great having a shutdown button, no SSH or commands needed to do it - I just flick the button, great.

Our needs are different to yours.
I need to be able to move my Pi around at will - its great having a shutdown button, no SSH or commands needed to do it - I just flick the button, great.

Re: What's the simple and safe way to do a "shutdown" button
piglet wrote:Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.
Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".
Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.
Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?
as root, just '# halt".
That shuts down the monitor and halts the rpi.
jozien17
I assume there must be some way to have:
1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v
How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Heres my Python code. Borrowed from here there and everywhere, as I have never written/used Python before today.
Saved as /usr/bin/run.py
Called from /etc/rc.local with the line "python /usr/bin/run.py"
Tested and working perfectly. Only catches one button press (so only runs your os.system command once, so you could happily run another script (BASH or whatever) from there).
Its also very instant. Catches the shortest button press I can manage! So I know its worked.
Negligible amounts of mem and CPU used...
top - 14:04:01 up 4 min, 1 user, load average: 0.06, 0.25, 0.13
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2810 root 20 0 6584 3600 2060 S 0.0 0.7 0:00.71 python
Saved as /usr/bin/run.py
Called from /etc/rc.local with the line "python /usr/bin/run.py"
Code: Select all
import RPi.GPIO as GPIO
import time
import os
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
prev_input = 0
while True:
input = GPIO.input(17)
if ((not prev_input) and input):
os.system("sudo shutdown -h now")
prev_input = input
time.sleep(0.05)
Its also very instant. Catches the shortest button press I can manage! So I know its worked.
Negligible amounts of mem and CPU used...
top - 14:04:01 up 4 min, 1 user, load average: 0.06, 0.25, 0.13
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
2810 root 20 0 6584 3600 2060 S 0.0 0.7 0:00.71 python
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Your reply was entirely formatted as a quote. Hard to figure what, if any, is anything you have added.jozien17 wrote:piglet wrote:Our school has a large screen in the reception area which has a rolling display of nice photos. It's currently driven by a laptop.
Getting inspiration from this thread http://www.raspberrypi.org/phpBB3/viewt ... 63&t=17797 I'm going to try replacing this with a "Poor Man's Photo Kiosk".
Set up the way described there I've got a Pi that you can plug in, switch on and will autologin, auto start x, and then run the rolling display of pictures.
Here comes the issue. This pi will be running "head-only" as opposed to "headless". ie. it will have a screen but no network, mouse or keyboard. How do I safely shut it down?
as root, just '# halt".
That shuts down the monitor and halts the rpi.
jozien17
I assume there must be some way to have:
1) a push-button linked to +5v pin and a gpio pin (and a maybe pull-down resistor on the gpio to ground *says he hoping that people don't laugh at how little he knows*)
2) a background task/interrupt to then initiate "sudo shutdown now" when that pin goes to +5v
How to do 1, and 2 and have 2 running all the time is beyond me. Is anyone willing to help?
What are you trying to ask?
- tonyhughes
- Posts: 951
- Joined: Wed Dec 26, 2012 3:46 am
Re: What's the simple and safe way to do a "shutdown" button
Just thought I would mention that basically any momentary switch would do fine. I bought a $5 one, because it looks nice, as if it was stock. Most electronics parts stores should have switches from as low 30 - 80 cents. You just need to pay some thought to how you will mount it nicely.
If anyone wants some clear & unambiguous photos of my wiring setup for this switch, let me know.
To connect to the GPIO pins, I scavenged header plugs from a internal 3.5" bay memory card reader out of an old desktop PC.
If anyone wants some clear & unambiguous photos of my wiring setup for this switch, let me know.
To connect to the GPIO pins, I scavenged header plugs from a internal 3.5" bay memory card reader out of an old desktop PC.