snazzify
Posts: 17
Joined: Fri Jul 29, 2016 4:49 am
Location: California, USA

GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Thu Jan 17, 2019 1:48 am

I'm using the following code to detect shutdown so i can cut off power.

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=10,active_low=1
My real concern is, is it safe to immediately cut off power when the GPIO pin goes low? Or do I need some delay like maybe 1-3 seconds or something?
Last edited by snazzify on Thu Jan 17, 2019 5:29 am, edited 1 time in total.

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

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Thu Jan 17, 2019 1:54 am

Yes, it is safe. You should see the 10 flashes of teh ACT LED just before the pin changes state.

snazzify
Posts: 17
Joined: Fri Jul 29, 2016 4:49 am
Location: California, USA

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Thu Jan 17, 2019 2:12 am

rpdom wrote:
Thu Jan 17, 2019 1:54 am
Yes, it is safe. You should see the 10 flashes of teh ACT LED just before the pin changes state.
I can only see about 3-4 blinks of the ACT LED when issuing

Code: Select all

sudo shutdown -h now
but if I shutdown without using the GPIO-POWEROFF the ACT LED certainly blinks more about 10 flashes.

Paul Hutch
Posts: 449
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Mon Jan 21, 2019 2:45 pm

snazzify wrote:
Thu Jan 17, 2019 2:12 am
I can only see about 3-4 blinks of the ACT LED when issuing

Code: Select all

sudo shutdown -h now
but if I shutdown without using the GPIO-POWEROFF the ACT LED certainly blinks more about 10 flashes.

I was trying out the gpio-poweroff overlay this weekend and I saw the same thing. When I use the overlay, instead of the normal power off activity LED flashing, I get just a couple quick flashes.

I monitored the current draw and found that when using the overlay on the latest updated distro it does not appear to shutdown correctly.

For reference:
RasPi 3b+
At idle the current consumption is ~0.4A
External LED on pin 25
The only configuration difference between tests is in config.txt
Config 1 (gpio=25=op,dh)
Config 2 (dtoverlay=gpio-poweroff,gpiopin=25,active_low=1)
Both configurations turn on the LED @ power up and turn off the LED @ power down

Here's what I'm seeing at power off (sudo poweroff)

Config 1:

Ten long flashes of the activity LED
Power consumption drops to ~0.1A

Config 2:

Three quick flashes of the activity LED
Power consumption goes up to ~0.7A

Based on the descriptions I've read this does not seem to be the correct result when using the gpio-poweroff overlay.

bk4nt
Posts: 7
Joined: Fri Dec 06, 2019 6:00 pm

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Tue Dec 17, 2019 10:53 pm

snazzify wrote:
Thu Jan 17, 2019 2:12 am
rpdom wrote:
Thu Jan 17, 2019 1:54 am
Yes, it is safe. You should see the 10 flashes of teh ACT LED just before the pin changes state.
I can only see about 3-4 blinks of the ACT LED when issuing

Code: Select all

sudo shutdown -h now
but if I shutdown without using the GPIO-POWEROFF the ACT LED certainly blinks more about 10 flashes.
+1 on RPi 4 Linux raspberrypi 4.19.75-v7l+

I have also an OLED display and corresponding stop/start service which nicely turns of that display on an init 0 if I do not activate dtoverlay gpio-poweroff option.

As soon as I add dtoverlay gpio-poweroff feature, I do see the corresponding GPIO pin state change like expected (I added a LED on it), but my OLED display isn't blanked anymore on init 0.

So I assume the halt scripts are at some point interrupted when dtoverlay gpio-poweroff feature is activated. That seems unsafe.

bk4nt
Posts: 7
Joined: Fri Dec 06, 2019 6:00 pm

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Wed Dec 18, 2019 8:15 pm

I made further tests. I was using the Pi headless, via ssh only, so I didn't notice... that my kernel each time crashes in the middle of shutdown process as soon I activate gpio-powerdown option (but the GPIO pin behaves like expected).

I wouldn't have noticed it either if I would already have external power cut off components. It would so just crash and cut the power.

Image

bk4nt
Posts: 7
Joined: Fri Dec 06, 2019 6:00 pm

Re: GPIO-POWEROFF: Is it safe to immediately cut power off when pin goes LOW?

Fri Dec 20, 2019 10:15 pm

snazzify wrote:
Thu Jan 17, 2019 1:48 am
I'm using the following code to detect shutdown so i can cut off power.

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=10,active_low=1
My real concern is, is it safe to immediately cut off power when the GPIO pin goes low? Or do I need some delay like maybe 1-3 seconds or something?
Some quick answers... might help

GPIO state change indicates that the CPU just went to inactive state (system was halted). So it is then safe to cut the power.

I noticed a kernel panic (I currently haven't power cut off hardware installed). If one waits up to 3 seconds instead of cutting the power in delay, this "panic" will be triggered, as an indicator. This is expected and described here:

https://www.kernel.org/doc/Documentatio ... weroff.txt

gpio-poweroff for Raspberry being described here:

https://raw.githubusercontent.com/raspb ... ays/README
snazzify wrote:
Thu Jan 17, 2019 2:12 am
rpdom wrote:
Thu Jan 17, 2019 1:54 am
Yes, it is safe. You should see the 10 flashes of teh ACT LED just before the pin changes state.
I can only see about 3-4 blinks of the ACT LED when issuing

Code: Select all

sudo shutdown -h now
but if I shutdown without using the GPIO-POWEROFF the ACT LED certainly blinks more about 10 flashes.
That overlay for GPIO triggered poweroff runs different code, so green and red LED don't flash the same way.

Moreover, I noticed some of my codes didn't stop like I expected on poweroff with that feature enabled. They where just crashing/interrupted. But this was because my codes didn't handle the correct signals, I had to add SIGHUP to delay Linux shutdown untill they finished their execution.

The basic code I used for testings, makes a LED blink slowly, then faster after SIGHUP, for some 10 seconds (during which system halt gets by the way paused):

Code: Select all

import RPi.GPIO as GPIO
import signal
import sys
import time

GPIO_LED   = 20

def signal_handler(sig, frame):
        GPIO.output(GPIO_LED, GPIO.HIGH)
        for y in range(0, int(10.0/0.05)):
                GPIO.output(GPIO_LED, GPIO.LOW)
                time.sleep(0.05)
                GPIO.output(GPIO_LED, GPIO.HIGH)
                time.sleep(0.05)
        GPIO.cleanup()
        sys.exit(0)

signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
signal.signal(signal.SIGHUP, signal_handler)

GPIO.setmode(GPIO.BCM)

GPIO.setup(GPIO_LED, GPIO.OUT)
GPIO.output(GPIO_LED, GPIO.HIGH)

while True:
        GPIO.output(GPIO_LED, GPIO.LOW)
        time.sleep(1)
        GPIO.output(GPIO_LED, GPIO.HIGH)
        time.sleep(1)
        

Return to “Advanced users”