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?

  • Regarding Option 3: Is the i2C device something you're building? You could configure a microcontroller via i2c to initiate the shutdown sequence. If powered independently of the RPi, it could also trigger a reboot -- pulsing GPIO 3 to start up when shut down shouldn't cause a problem. I've been playing around with the idea of using an ESP8266 to provide shutdown, restart, hard reset and 5V power supply functionality. – bobstro Sep 18 '17 at 14:55
  • @bobstro that seems a nice approach. Do you have any reference to share? – Luis Diaz Sep 18 '17 at 15:22
  • 1
    @Luis Diaz - Nothing to share yet. I've hacked together a script that allows me to shut down and restart the RPi via MQTT. Next steps are to interface the ESP8266 to the RPi, then use MQTT to control it. That would eliminate the concern about interfering with i2C on GPIO 3 altogether by only pulsing it when shut down already. One could communicate between the ESP8266 and RPi using i2c (I believe), although I'm not sure if that's a big benefit, although it might be a good solution if using a non-networked microcontroller (e.g. Arduino Nano). Put the buttons on it instead. – bobstro Sep 18 '17 at 15:30
  • @bobstro I'm building a music player based off a Pi 0 W and pHAT DAC. I'm also trying to build in a FM radio chip RDA5807M which operates on i2C. I'm toying with the idea of adding sort form of arduino powered off the Pi's 5V line to be able to interpret a potentiometer and change the station. As far as I can tell the Pi's 5V line is always on, even when the Pi is "sleeping". I guess it would then be possible to have a single button could be connected to the arduino and then relayed via i2C to wake it up? – daniel_l Sep 18 '17 at 16:04
  • @daniel_l - If you use a 3.3v Arduino part, you might save some signal level conversion. I'd have to read up on power available off the GPIO headers to be safe. You can have several devices on the i2c bus, so one could be the Arduino. If you're planning on using other buttons, this should be straightforward to implement. – bobstro Sep 18 '17 at 16:30
2

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.

  • Yeah, the Pimoroni shim (which I have) has some solid-state switching to pull power, then reinstate it via GPIO. It does make the Raspberry Pi's power connection redundant, as power goes through GPIO. The 2s reset/5s shutdown solution was the best I could come up with using cheap hardware and one pin. – scruss Nov 8 '17 at 3:01
  • I would have gotten the shim if it didn't have it's own power connector. Seems a weird design choice given that all you need is a button and some code. – YetAnotherRandomUser Nov 8 '17 at 3:04
  • The shim completely cuts power: all LEDs are off on the Raspberry Pi. Any other solution that uses the power connector will draw power until you do the GPIO 3/RUN thing, or unplug and replug the power cable. I'm thinking of making something to achieve the latter with a couple of breakout boards and a switch – scruss Nov 8 '17 at 3:23
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
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.