paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

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

Tue Aug 06, 2013 7:55 am

Clever circuit, I like it.

One warning though, the Pi (actually Debian) needs somewhere between 25-35 seconds to completely go through the shutdown procedure, after you initiated the shutdown command. Cutting the power prematurely could cause incomplete or corrupted log files, or worse, a corrupted SD card.

With the shutdown command, the Pi does not really power-down, it goes into the Halt state. In that phase, the current consumption drops from about 400mA to about 110mA. Sensing that drop and then turning off the power would be safer. Or, use a timer to wait a safe 40 seconds.

There is a post with information that is somewhat related and may be helpful, because it addresses a power failure and a safe shutdown: http://www.raspberrypi.org/phpBB3/viewt ... 92#p399492

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

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

Wed Oct 16, 2013 12:44 pm

I'm interested in a shutdown and wake up switch.
I used the last code posted in this thread, created a shutdown.py file with the code inside.
Now I have to add the script call before the "exit (0)" in the rc.local and the software side is done?

I must use the resistors? It could be hard to fit all in a small thin case.
To be clear I have to connect the GPIO17 (pin 11) to a 3V GPIO pin, which one?


I just tried to run the script without adding the phisical switch

Code: Select all

sudo python shutdown.py
but this messagge appears:
Make sure you have a button connected so that when pressed
it will connect GPIO port 23 (pin 16) to GND (pin 6)

But why port23 (pin 16) and GND?

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

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

Wed Oct 16, 2013 3:09 pm

Hi mcgyver83,

It does not matter what GPIO Port you select or use.
Any Port will work, as long as the button "shorts"the input the ground (GND).
Even that you can change in the code.

mcgyver83
Posts: 358
Joined: Fri Oct 05, 2012 11:49 am

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

Wed Oct 16, 2013 9:41 pm

Great!
I'll build the switch in the week end and test it. I just find out in the code the comment related to the GPIO port tu check for signal.
I must use the resistors? Why? I have only very basic knowledge on this kind of things (GPIO)

Thanks!

paulv
Posts: 558
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

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

Wed Oct 16, 2013 9:48 pm

You need to create a pull-up to the 3.3V supply in order to have the switch pull it down to ground.
If you don't use a resistor (anything at or above 1K will do), the GPIO Port will float, and this could cause incorrect readings.
You could use the GPIO pull-up method, but that only works when the Ports are assigned.
I suggest you use a resistor to be safe.

Connect it as follows:
3.3V ---resistor----switch-----ground

LeighdePaor
Posts: 6
Joined: Thu Jan 09, 2014 8:40 pm

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

Sat Jun 28, 2014 8:31 pm

There's a daemon available for this, written by Sanjeev Sharma.
The daemon will safely shutdown the Pi if the button is pressed for less than 2 seconds, it will reboot if pressed for 2 seconds or more.

Code here...
https://github.com/sanje2v/buttonshutdown-daemon

Wiring details etc here...
http://sanje2v.wordpress.com/2014/02/16 ... pberry-pi/

pageauc
Posts: 224
Joined: Fri Jan 04, 2013 10:52 pm

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

Fri Mar 04, 2016 1:16 am

Shutdown Raspberry Pi using a momentary switch or equivalent

Here is the python script I use to safely halt or startup a rpi safely using a momentary switch.
You can also use jumper wires or anything to ground the appropriate gpio_pin
I have added an optional button_hold variable seconds before action is initiated.
See comments for additional detail. Default location for shutdown.py is in /home/pi
change the crontab entry if you decide to put it someplace else.

Regards Claude ..

Code: Select all

#!/usr/bin/env python

# Safely shutdown Raspberry pi using a momentary switch
# Connect momentary switch to pin 5 and 6 (default)
# or set gpio_pin variable to desired pin below
#
# Set button_hold variable to number of seconds to
# hold down momentary switch before action taken
# default is 2 seconds.  0=No delay
#
# Add line below to sudo crontab -e (without #)
# @reboot /home/pi/shutdown.py
#
# make sure shutdown.py is executable
# chmod +x shutdown.py
#
# After initial power boot press switch for specfied time
# to Initiate safe shutown (halt)
# After shutdown wait 5 seconds then press switch again
# for specified time to intiate a startup
# Wait a few seconds between between operations
#

import RPi.GPIO as GPIO
import time
import subprocess

button_hold = 2     # number of seconds to hold button before action taken
gpio_pin = 5          # Set GPIO pin you want to use.  default is 5 and ground is 6

GPIO.setmode(GPIO.BOARD)
# set GPIO pin to input, and enable the internal pull-up resistor
GPIO.setup(gpio_pin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

start_time = time.time()
shutdown = False
while not shutdown:
    if GPIO.input(gpio_pin):
        start_time = time.time()
    else:
        hold_time = time.time() - start_time
        if hold_time > button_hold:
            shutdown = True
    time.sleep(.1)
subprocess.call("/sbin/shutdown -h now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
GitHub - https://github.com/pageauc
YouTube - https://www.youtube.com/user/pageaucp

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6053
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

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

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.

viewtopic.php?f=107&t=115394
http://developer.toradex.com/knowledge- ... ement_keys

pageauc
Posts: 224
Joined: Fri Jan 04, 2013 10:52 pm

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

Fri Mar 04, 2016 8:02 pm

Shutdown button won't restart from halt except when using pin 5

I have been using my script above and it works great on pin 5 to shutdown and restart my B+. When I use another gpio pin eg 40, 26, 13, etc the rpi shuts down OK but won't restart like it does on pin 5. I then have to power off and plug back in to restart.
I noticed that pin 5 is i2c and the others are just GPIO or SPI type. Is there something special about pin 5 that allows the crontab to restart the RPI from a halt. I thought the problem might be pins above 26 but pin 13 does not work to restart pi after a halt.

The reason I want to relocate the pin is because I have another RPI with a RTC (real time clock) that is on pins 1-26 and I did not want to use those pins if I could use a higher pin. My first choice was pin 40 with 39 grounded and this shuts the system down OK but does not restart the rpi from halt. Just trying to understand why?
Is it possible that pin 5 is activating a cpu reset that is not available on any other pins. I know there is a reset contact on the rpi pcb that I saw being used on a video. Not sure if this can be used in conjunction with a gpio pin similar to what pin 5 does.
Any help would be appreciated.

Thanks Claude ...
Last edited by pageauc on Sat Mar 05, 2016 1:29 am, edited 1 time in total.
GitHub - https://github.com/pageauc
YouTube - https://www.youtube.com/user/pageaucp

ShiftPlusOne
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 6053
Joined: Fri Jul 29, 2011 5:36 pm
Location: The unfashionable end of the western spiral arm of the Galaxy

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

Fri Mar 04, 2016 8:12 pm

Hmm.... just noticed this thread was necroed from 2014. Best to start a new topic for things like that in the future.

I don't remember off the top of my head which pin(s?) allow you to wake up from halt, but that's hard-coded in an can't be changed. I think it's GPIO pins 1 and 3 (BCM numbering scheme, not board numbering). One of those is probably physical pin 5.

User avatar
rpdom
Posts: 15451
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

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

Fri Mar 04, 2016 8:24 pm

ShiftPlusOne wrote:Hmm.... just noticed this thread was necroed from 2014. Best to start a new topic for things like that in the future.

I don't remember off the top of my head which pin(s?) allow you to wake up from halt, but that's hard-coded in an can't be changed. I think it's GPIO pins 1 and 3 (BCM numbering scheme, not board numbering). One of those is probably pin physical pin 5.
It is pin 5, which needs to be pulled low to restart the Pi.

Return to “General discussion”