User avatar
piglet
Posts: 809
Joined: Sat Aug 27, 2011 1:16 pm

What's the simple and safe way to do a "shutdown" button?

Fri Sep 28, 2012 9:53 am

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?

User avatar
RaTTuS
Posts: 9630
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 10:20 am

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

brainsys
Posts: 68
Joined: Fri Jun 29, 2012 7:16 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 10:36 am

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 :roll:

User avatar
piglet
Posts: 809
Joined: Sat Aug 27, 2011 1:16 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 1:43 pm

RaTTuS wrote:no need to have x running to show images
What's the best (light) pi-compatible cli slideshow application? qiv seems to work very well but wasn't happy without x.
brainsys wrote:Ahem ... simply pull the plug out?
I have to say I've had no problems doing that at home...
RaTTuS wrote:boot the FS as readonly so it never matters it it gets switched off ?
Not a bad idea...if I knew how to do it!



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/

User avatar
tedhale
Posts: 114
Joined: Thu Sep 20, 2012 4:52 pm
Location: Williamsburg, VA, USA
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 3:20 pm

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.
- Ted B. Hale
http://raspberrypihobbyist.blogspot.com

Maarten Baert
Posts: 7
Joined: Fri Sep 28, 2012 9:13 pm

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 9:41 pm

a push-button linked to +5v pin and a gpio pin
GPIO pins are 3.3V IIRC, so whatever you decide to do, use the 3.3V rail, not the 5V one :).

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 9814
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

Fri Sep 28, 2012 10:50 pm

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! :twisted:

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.

User avatar
Dweeber
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Fri Sep 28, 2012 11:11 pm

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:

Image

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

trazer
Posts: 22
Joined: Fri Sep 28, 2012 2:32 pm

Re: What's the simple and safe way to do a "shutdown" button

Sat Sep 29, 2012 10:55 pm

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.

User avatar
piglet
Posts: 809
Joined: Sat Aug 27, 2011 1:16 pm

Re: What's the simple and safe way to do a "shutdown" button

Mon Oct 01, 2012 9:27 pm

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

Image

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)
Launch of code added to /etc/rc.local on the line before "exit 0" :

Code: Select all

python /home/pi/bin/button
Press button for 1 second to ensure it's polled, and the pi shuts down. Perfect.


User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2808
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Wed Oct 10, 2012 7:36 am

Well done. Looks like a tight squeeze to get it all in the case.
Alex Eames RasPi.TV, RasP.iO

User avatar
Dweeber
Posts: 606
Joined: Fri Aug 17, 2012 3:35 am
Location: Mesa, AZ
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Wed Oct 10, 2012 9:01 am

piglet wrote:Hurrah!
Trying to figure out what you have connected.

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

User avatar
alexeames
Forum Moderator
Forum Moderator
Posts: 2808
Joined: Sat Mar 03, 2012 11:57 am
Location: UK
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Wed Oct 10, 2012 10:34 am

He shows it here...
http://www.raspberrypi.org/phpBB3/viewt ... 37#p186137

and gives a link to where he got the info. :D
Alex Eames RasPi.TV, RasP.iO

Tenocticatl
Posts: 19
Joined: Fri Jan 11, 2013 11:11 pm

Re: What's the simple and safe way to do a "shutdown" button

Sat Jan 12, 2013 3:18 pm

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?

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sat Feb 02, 2013 2:31 am

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?

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sat Feb 02, 2013 2:42 am

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.

Image

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

Image


User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 12:44 am

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.

dextrus
Posts: 119
Joined: Tue Jan 24, 2012 10:10 pm
Location: Eastleigh, Hampshire
Contact: Website

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 12:55 am

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
Have more FUN with your Pi. Visit www.pi-fun.com

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 12:58 am

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.

:-)

jozien17
Posts: 23
Joined: Sun Oct 28, 2012 4:35 pm

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 1:00 am

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?

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 1:05 am

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"

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

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 1:07 am

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?
Your reply was entirely formatted as a quote. Hard to figure what, if any, is anything you have added.

What are you trying to ask?

User avatar
tonyhughes
Posts: 951
Joined: Wed Dec 26, 2012 3:46 am

Re: What's the simple and safe way to do a "shutdown" button

Sun Feb 03, 2013 1:50 am

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.

Return to “General discussion”

Who is online

Users browsing this forum: dshaw619 and 37 guests