erk1313
Posts: 2
Joined: Sun Apr 05, 2020 7:53 pm

Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Sun Apr 05, 2020 8:28 pm

Here is an idea for a one button approach to shut down or wake up a Raspberry PI-2B (but may work for other Pi's, but no the Pi4 ). Shutting down a PI using a GPIO pin is no-problem. See other posts for that. It is waking it up that is difficult. Typically this can be done by one of these ways:

1. Briefly disconnect power
2. Momentarily bring the “Run” pin low (not for Pi4).
3. Bring GPIO3 low (on Pi 3 or later; note that this GPIO is also used for the I2C clock).

My approach is based on bringing the Run pin low. However if I were to use a button to do this, a user pressing this button during run time would cause a hard reset. So I need a way to arm this button only after the Pi is shutdown.

When the Pi is shut down, it is not really "powered off", just put into an idle condition. There is a way to tell the Pi to set a GPIO pin high or low during the graceful shutdown routine. Here is a proposed (untested) circuit for arming the reset button after shutdown, relying on the GPIO pulled High after shutdown:
Image

Thoughts before I attempt this?
Note: Another way is to use a microcontroller to manage this. The best solution I have found (not tested) is the UPS PiCo

alphanumeric
Posts: 2999
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 1:04 pm

Nice, don't see why it wouldn't wok.
There are dual pole switches out there by the way. It's basically two separate isolated sets of contacts in the one switch body, both activated by the one button press. You could wire one set (switch) to GPIO 3 and the other to the GPIO pin you want to do the shutdown with. Will work even if i2c is enabled. It's the way the Pimoroni Fan shim works. It grounds GPIO 3 and GPIO 17 when pressed.
I use dtoverlay=gpio-shutdown,gpio_pin=17,active_low=1,gpio_pull=up for shutdown.

For some reason dtoverlay=gpio-poweroff gives me a kernel panic on my Pi 4B. Thats the overlay you use to pull a GPIO pin low on shutdown.
Works OK on my Pi 3B+ though? I was using it to turn the Fan Shim Fan off on shutdown.

User avatar
thagrol
Posts: 5543
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 1:19 pm

I'm curious, why not use GPIO 3?

Yeah it's used for the I2C clock but with the Pi off it'll be high all the time. I2C is master slave (one Master in the case of a Pi) so no slaves will be transmitting unles the Pi requests it. Which it won't if it's off.

I've used the GPIO 3 (re)start approach with i2C without problems. You can't use GPIO 3 for shutdown when I2C is active but you can wire a different GPIO (chosen when setting up the gpio_shutdown overlay) to the same button.

Both GPIO to one side, ground to the other. Pressing the button will shutdown a running Pi and start a shutdown one.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

alphanumeric
Posts: 2999
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 1:22 pm

Yeah as you've mentioned, GPIO 3 will do boot up and shut down "if" i2c is disabled. Once you turn on i2c the shutdown part doesn't work via GPIO 3. You can set it to use another pin, its what I do with my fan shim. As posted above to switch the shutdown to GPIO 17.
Just wondering what wiring another GPIO too GPIO 3 will do to i2c.

User avatar
thagrol
Posts: 5543
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 4:23 pm

alphanumeric wrote:
Mon Apr 06, 2020 1:22 pm
Yeah as you've mentioned, GPIO 3 will do boot up and shut down "if" i2c is disabled. Once you turn on i2c the shutdown part doesn't work via GPIO 3. You can set it to use another pin, its what I do with my fan shim. As posted above to switch the shutdown to GPIO 17.
Just wondering what wiring another GPIO too GPIO 3 will do to i2c.
Nothing. At least in my experience. And no, I2C transfers do not trigger a shutdown via the other GPIO.

If there's a transfer in progress when a button on GPIO3 is pressed it may confuse things when GPIO 3 gets pulled low. That's unlikely to break things permanently unless you're doing someting like writing to an EEPROM at the time.

With the Pi off there won't be any transfers taking place over I2C. Slaves don't transmit unless requested to do so by the master. That's why some I2C devices have seperate interrupt pins.

I've been using the tw pins, one button appraoch for quite some time now with no problems. Admittedly I've only been using RTC and GPIO expander chips via I2C so not really extensively tested.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

alphanumeric
Posts: 2999
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 5:32 pm

I have one setup that I use a shutdown button on. The shutdown is called up via a python file I have running on boot up via crontab. It's a headless setup with several i2c devices. I went that route because I also I do some house keeping in that file prior to issuing the shutdown command.
I do the one button thing on my Pimoroni Fan Shim but don't have any i2c devices hooked up on that Pi. Not yet anyway. I have a Blinkt plugged in but it doesn't use i2c.
I have a couple of other headless setups I'd like a shutdown button on but they are running Motion Eye OS and it just ignores the dtoverlay entry in config.txt. I can boot them up via GPIO 3, but have to shut down from the web gui. I believe dtoverlay support is coming in a future release.

One downside to this is, things like my Blinkt and Sense Hat don't clear their displays if shutdown is done via the dtoverlay. That means I have to unplug them anyway to clear any lit LED's. Or do the shutdown from the python file and do a clear first.

User avatar
thagrol
Posts: 5543
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Mon Apr 06, 2020 9:54 pm

alphanumeric wrote:
Mon Apr 06, 2020 5:32 pm
I have one setup that I use a shutdown button on. The shutdown is called up via a python file I have running on boot up via crontab. It's a headless setup with several i2c devices. I went that route because I also I do some house keeping in that file prior to issuing the shutdown command.
I do the one button thing on my Pimoroni Fan Shim but don't have any i2c devices hooked up on that Pi. Not yet anyway. I have a Blinkt plugged in but it doesn't use i2c.
I have a couple of other headless setups I'd like a shutdown button on but they are running Motion Eye OS and it just ignores the dtoverlay entry in config.txt. I can boot them up via GPIO 3, but have to shut down from the web gui. I believe dtoverlay support is coming in a future release.

One downside to this is, things like my Blinkt and Sense Hat don't clear their displays if shutdown is done via the dtoverlay. That means I have to unplug them anyway to clear any lit LED's. Or do the shutdown from the python file and do a clear first.
The overl;ay documentation says:
The given GPIO pin is configured as an input key that generates KEY_POWER events. This event is handled by systemd-logind by initiating a shutdown.
I'm not a systemd expert but shouldn't it be possible to trap the KEY_POWER event and run a custom leanup script?
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

alphanumeric
Posts: 2999
Joined: Tue Jan 19, 2016 2:17 pm
Location: Sydney, Nova Scotia, Canada

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Tue Apr 07, 2020 9:58 am

What you propose is likely possible, I have no idea how to do it though? I don't have those skills.
Doing it from my python file works just fine for me so that's how I do it, on those setups that need it.
Ones where I have an LED Shim or a Blinkt etc. Anything with blinky lights I want off at the end.;)

erk1313
Posts: 2
Joined: Sun Apr 05, 2020 7:53 pm

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Tue Apr 07, 2020 12:40 pm

thagrol wrote:
Mon Apr 06, 2020 1:19 pm
I'm curious, why not use GPIO 3?
I am using a Raspberry Pi2B (for lower power consumption), which doesn't wakeup on GPIO3 pin.

User avatar
thagrol
Posts: 5543
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Idea for one-button shutdown or wakeup using MOSFET to arm a reset button

Tue Apr 07, 2020 1:03 pm

erk1313 wrote:
Tue Apr 07, 2020 12:40 pm
thagrol wrote:
Mon Apr 06, 2020 1:19 pm
I'm curious, why not use GPIO 3?
I am using a Raspberry Pi2B (for lower power consumption), which doesn't wakeup on GPIO3 pin.
Odd. Mine does. Though that probabkly isn't of much help to you.
I'm a volunteer. Take me for granted or abuse my support and I will walk away

All advice given is based on my experience. it worked for me, it may not work for you.
Need help? https://github.com/thagrol/Guides

Return to “Advanced users”