brijesh4u
Posts: 9
Joined: Wed Jul 17, 2013 6:34 am

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

Sun Jul 14, 2019 6:52 am

piglet wrote:
Fri Sep 28, 2012 9:53 am
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?
[Moderated] (split from 2012 thread and locked that one for being obsolete) because in 2019 we may need a new solution.

Hey Everyone,

Any idea why this solution is not working with latest Raspberry pi 4. Earlier my pi can be reboot from the same button if I press again after an orderly shutdown.

Here is my script:

Code: Select all

import subprocess
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)

# GPIO3 (pin 5) set up as input. It is pulled up to stop false signals
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# wait for the pin to be sorted with GND and, if so, halt the system
GPIO.wait_for_edge(3, GPIO.FALLING)
subprocess.call(['shutdown -h now "System halted by power switch "'], shell=True)

# clean up GPIO on normal exit
GPIO.cleanup() 



Thanks
Brijesh

User avatar
recantha2
Posts: 277
Joined: Wed Nov 14, 2012 9:34 am
Location: Potton, Bedfordshire
Contact: Website

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

Sun Jul 14, 2019 11:16 am

Try this:
It's a tutorial I wrote a while back that lets you shutdown a Raspberry Pi with a paperclip shorting across the last two GPIO pins on the right (with the pins at the top left):
https://www.recantha.co.uk/blog/?p=13999

Just wire up a button if you don't want to use the paper clip :-)
Last edited by recantha2 on Mon Jul 15, 2019 10:21 am, edited 1 time in total.
--
Michael Horne - @recantha
Raspberry Pi blog - http://www.recantha.co.uk/blog

Cambridge Raspberry Jam
Website: http://camjam.me
Facebook: https://www.facebook.com/cambridgeraspberryjam
Follow the Cambridge Raspberry Jam on Twitter - @cambridgejam

User avatar
davidcoton
Posts: 3942
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

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

Sun Jul 14, 2019 11:32 am

recantha2 wrote:
Sun Jul 14, 2019 11:16 am
Try this:
It's a tutorial I wrote a while back that lets you shutdown a Raspberry Pi with a paperclip shorting across the last two GPIO pins on the right:
https://www.recantha.co.uk/blog/?p=13999

Just wire up a button if you don't want to use the paper clip :-)
This is dangerous. If you have the board upside down (relative to the author's expectation), connecting the two right-hand end GPIOs will activate the magic smoke release. Permanently and irrevocably, especially on the Pi3B+.

Disclaimer: not tested :o :shock: :lol: :roll:
Signature retired

User avatar
clicky
Posts: 301
Joined: Thu Oct 25, 2012 7:34 am

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

Sun Jul 14, 2019 12:02 pm

ShiftPlusOne wrote:
Fri Mar 04, 2016 10:33 am
Don't know if anyone has mentioned this yet. You can add a shutdown button using gpio-keys, defining it as the power key and then adding a udev rule to let systemd know that it should be treated as such. That will work the same as a shutdown button on the PC. A reboot button can be done the same way.

I don't have the exact instructions handy, but I've had it working very well. It involved creating and enabling the overlay then adding a simple udev rule.

https://www.raspberrypi.org/forums/view ... 7&t=115394
http://developer.toradex.com/knowledge- ... ement_keys
That's worth knowing and seems like 'proper' solution (even though it seems the least configurable).

For our PiWars rovers we had two GPIOs used + what many did here - a Python program to do the shutdown. One GPIO was for reading the button and another to give us 'feedback' (originally to light the underside of rover for camera for following line) but on some other RPis just an LED was connected to it.
Python code was implemented not to shutdown RPi immediately but only if button was held for at least 5 seconds during which LED was flashing quickly. At any time you could not proceed and nothing would happen (LEDs would go off). If you kept button during those 5 seconds, LED would continue to flash slower showing that shutdown was confirmed and as consequence of halt command at the end LED would go off showing that it is safe switching the RPi off...

W. H. Heydt
Posts: 10586
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

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

Sun Jul 14, 2019 4:20 pm

Here https://www.raspberrypi.org/forums/view ... 1&t=221617 is a relatively recent, and very simple solution. It's in the second post of the thread and is from 2018. Use the dtoverlay in config.txt to set it up and connect a mometary contact button across pins 5 and 6 (by default). It will not only do a proper shutdown, but if you leave the power connected, pushing the button again will reboot the Pi.

I have a Pi3A+ set up this way. It plays random tracks from a collection (about 16GB worth) of .wav files. The Pi, plus noise reducing headphones and a powerbank and one can have a calm and pleasant time in an otherwise noisy environment.

3dh
Posts: 22
Joined: Tue Nov 24, 2015 11:44 am

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

Mon Jul 15, 2019 4:47 pm

https://youtu.be/qAzheiZlB9k

PS. This is enough if you use GPIO3 + GND (pins 5 + 6):
dtoverlay=gpio-shutdown

Return to “General discussion”