This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
GPIO power button for Raspberry Pi
#1
Hello,

I want to add a power button to the Raspberry Pi, to switch off correctly the device before cut the battery power.

Is is possible to add somethinf like this:
[Image: PcfULk3.png]

It will be great to get a light to know if the Raspberry Pi is on.

Anyone has something like this? Any recomendation for the power button?

Kind regards.
Reply
#2
I've done it in a couple of iterations of systems. You use a momentary switch, and it can have another, GPIO powered LED.

Usually what they do is set them up so that a short press (less than 3 seconds) does a re-boot, while a longer press does a safe shutdown. Also, if you use the right GPIO pins, you can also wake up your RPI.

Here's a good explanation and instruction.
https://pythonrepo.com/repo/fire1ce-rasp...h-hardware
Reply
#3
Thanks!

I got it working!

The power button with this guide: https://www2.quartoknows.com/page/raspbe...own-button

And the LED indicator adding this in the /boot/config.txt file:


Code:
gpio=13=op,dh


I connect the LED to the GPIO 13 and GND.

Best regards.
Reply
#4
Great job!
Reply
#5
threadjacking for a related issue

I have openplotter running on a RPi4B.  Power supply is 12v>UPS>12v:5vstep-down>RPi

I want to shut the RPi power off completely following a shut down to save power (I don't have a lot of battery capacity or charging capacity so every bit counts).

I have this switch unit. It will sit electrically between the UPS and the 12v:5v converters ie 12v>UPS>Power Switch>12v:5vstep-down>RPi

It can switch on and off triggered by a pulse of >1v.  On is easy - a simple momentary switch is all I need there So my hardware solution exists, I now need to implement the software bit so that the system automatically shuts off the power after a shut-down command.  

I understand  I can use a GPIO pin to provide the "off" trigger by making a GPIO pin go high on a shut down command.  But I am clueless as to how to do this.

Helpful suggestions that assume I am a total beginner on RPi and anything to do with coding would be really appreciated!
Reply
#6
It's an interesting problem, as you really don't want the power to go away until the RPI itself is shut down, and of course you'd need something like a little Python script to run to manage the GPIO, which would require the RPI to be up.

The only thing I can think of is an external circuit that has something like a 20 second timer that starts when the RPI gets the shutdown command. This would give it time to shut down before the timer expires and disconnects the power completely.

You may find in testing that the system draws nearly zero current when the RPI is shutdown anyway, so you might not need this.
Reply
#7
(2022-01-01, 04:20 PM)abarrow Wrote: The only thing I can think of is an external circuit that has something like a 20 second timer that starts when the RPI gets the shutdown command. This would give it time to shut down before the timer expires and disconnects the power completely.

Thanks for the reply.  

The time to power down thing is not a big issue - I can resolve that with a simple capacitor/resistor network that stores, say, 20s of power at the output of the UPS. eg 12v>UPS>Power Switch>Storage Network>12v:5vstep-down>RPi

Its the software that is my downfall!
Reply
#8
(2022-01-01, 08:59 PM)aardvarkash10 Wrote:
(2022-01-01, 04:20 PM)abarrow Wrote: The only thing I can think of is an external circuit that has something like a 20 second timer that starts when the RPI gets the shutdown command. This would give it time to shut down before the timer expires and disconnects the power completely.

Thanks for the reply.  

The time to power down thing is not a big issue - I can resolve that with a simple capacitor/resistor network that stores, say, 20s of power at the output of the UPS. eg 12v>UPS>Power Switch>Storage Network>12v:5vstep-down>RPi

Its the software that is my downfall!

you may need to do some googling to get this right and may have to look at which GPIO pins are naturally pulled up and down.

Below are my instructions to add the momentary switch, which just involve some minor changes to config.txt:

Momentary Shutdown switch
Connect up the momentary switch across Pin 39 and 40, if the led has a separate ground then connect up the LED using 38 and 34. Watch polarity.
 
In the file /boot/config.txt (use sudo nano /boot/config.txt)
 
Add the following:

Code:
#on/off switch
dtoverlay=gpio-shutdown,gpio_pin=21
dtoverlay=gpio-poweroff,gpiopin=20,active_low="y"

GPIO numbers are different than the pin number so make sure you use GPIO20(actual pin 38) and GPIO21 (actual pin 40)
 
The behavior you will see is that when Pi is booted up the LED will come on.  When the momentary switch is pressed, the PI will shutdown and once safely down, the LED will power off.  the main power can be turned off safely now.

what I think you need is the opposite to the statement above where the power (for the led) would be low, then go high at 'poweroff'.

I have since changed this circuit and added a relay to disconnect my permanent power source (in this case your ups).  the code I use is below:


Code:
dtoverlay=gpio-shutdown,gpio_pin=21,gpio_pull="down",active_low=1
gpio=20=op,dh

the system is connected to the instrument breaker and a permanent 12V, through a relay.  the system boots when the instrument breaker is turned on and powers down when the instrument breaker is turned off.  On power down, pin 20 is depowered and disconnects the permanent 12V supply totally isolating the Pi.

hope this helps
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)