6

As far as I can tell, these are the options for power buttons with little to no extra hardware:

Option 1: add a momentary switch to the P6 header (reset) breakout. Can't turn off, only reset, or turn on RPi if powered down from OS.

Option 2: add momentary switch to a GPIO pin, run a script that detects the button, and run a sudo shutdown. Can't turn it back on.

Option 3: if you modify option 2 to use the Board pin (GPIO 3), you can turn it on and off.

The problem with option 3 is that it's also an I2C pin. I assume if you use a script to pull it high and then try to use another I2C device, it's just going to fail miserably.

I have looked at the code for Pimoroni's OnOffShim, and even it uses one pin for shutdown and another for boot (I still don't understand how the latter works).

Is there another pin on the Pi that can act the same way as GPIO 3? Or should I just move to one of the many power management add-ons like the pimoroni shim, etc?

YetAnotherRandomUser
  • 1,120
  • 2
  • 11
  • 34
daniel_l
  • 69
  • 1
  • 1
  • 2

4 Answers4

3

I came across a number of tutorials for adding power, reset, and shutdown buttons. That's how I came across this question; looking for a how-to.

This particular tutorial will give you reset and shutdown on any arbitrary pin, but I don't think it will do power on, like using pin 5/GPIO 3 will do.

Combined Restart / Shutdown Button for Raspberry Pi

A very simple systemd service for Raspberry Pi that provides a software-controlled restart / shutdown button. Code: scruss/shutdown_button Use

Default behaviour is:

your Raspberry Pi will reset if the button is held for more than two seconds but fewer than five seconds;
your Raspberry Pi will shut down if the button is held for more than five seconds.

By default, the software assumes the switch is connected to pin BCM 27. Both the pin and the timing can be changed in the Python source file.

To get all three functions, it looks like you have to use pin 5/GPIO 3, like with this library (Github, blog; a different blog with the same solution) (emphasis mine):

pi-shutdown

Shutdown/reboot(/power on) Raspberry Pi with pushbutton Usage:

Connect pushbutton to GPIO pin 5 and ground then run:

sudo python pishutdown.py

When button is pressed for less than 3 seconds, Pi reboots. If pressed for more than 3 seconds it shuts down. While shut down, if button is connected to GPIO pin 5, then pressing the button powers on Pi.

While I was poking around pinout.xyz, it seems that WiringPi lets you do i2c on pins 8 and 9. I confirmed that by doing a DDG search and coming across this relatively recent Reddit thread where a user had a PEBKAC problem and eventually got i2c working. You could use WiringPi for your i2c needs, and then the top method for your power button.

As far as the Pimoroni board you mentioned, if you are willing to use multiple buttons, you could also use an arbitrary GPIO pin for software-based reset and power off, and then the additional button could go to the run header for hardware based reset and power on. I haven't checked out their code, but that might be what they are doing.

YetAnotherRandomUser
  • 1,120
  • 2
  • 11
  • 34
1

You need custom circuitry to isolate I2C bus when the Pi is ON (FET switch) and re-direct the switch to another GPIO PIN. I tested this circuit for an open source product I am developing. You can checkout the schematic below:

https://github.com/ubopod/ubo-pcb#power-button

You need to also setup a system boot-up service to set the GPIO PIN value to LOW (in this design) and start listening to GPIO callback to shut down if the button is presses. I will put up the script on my GitHub page soon and update this response. But the script is fairly easy to setup and there are already a few that do a similar function but you just need to GPIO pull down function to it.

mehrdadm
  • 11
  • 2
1

There is another Simple solution which works well for Both PowerUp out of standby, and shutdown from active. Avoiding issues with pin#5(I2C SCL). Build a script to monitor pin#7(or any gpio other than #5), when it goes low- use subprocess to run 'sudo shutdown'. Use pin#5 shorted to gnd to wake from standby. Both can be switched separately using a dpdt momentary switch. Or a spst switch and a couple mosfets. This way pin#5 is only manipulated while the raspi is in standby- no worries corrupting an active I2C bus.

WAD
  • 11
  • 1
1

Unfortunately, wake from halt works only on GPIO3/SCL [1]. Some of the described alternatives involve complicated circuits [2].

I found a simple way: Connect the switch to GPIO4, connect a diode from GPIO3 to GPIO4, so that current can flow from GPIO3 to GPIO4.

Pressing the switch will now pull GPIO3/SCL and GPIO4 low, but GPIO3/SCL activity will not influence the state of GPIO4. This is electrically safe with I2C, but a transaction may be interrupted while you press the switch. This could be solved with an NFET and another GPIO pin that drives the gate and isolates I2C from the switch when the Raspi is on.

On the software side, simply put this line in /boot/config.txt: dtoverlay=gpio-shutdown,gpio_pin=4

That's it for current Raspbian! For older Raspbian or some other distributions (e.g. Volumio), put this line in the new file /etc/udev/rules.d/30-shutdown.rules: ACTION!="REMOVE", SUBSYSTEM=="input", KERNEL=="event*", SUBSYSTEMS=="platform", DRIVERS=="gpio-keys", ATTRS{keys}=="116", TAG+="power-switch"

  1. https://www.raspberrypi.org/forums/viewtopic.php?f=29&t=24682
  2. https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=140994