Momentary On/Off Switch with soft shutdown

Raspberry Pi related support

Re: Momentary Contact Switch

Postby ingohz » 14 Apr 2016, 20:13

PeteB wrote:And... it looks like the library I loaded from XploD's tutorial has interrupts, even better!
(why don't people just tell me these things up front... :roll: )

With rune 0.4 (build: beta-20160313) there was nothing to install additionally so far. GPIO is version 0.6.1. Script runs fine - I only had to change line 1 (!/usr/bin/env python -> !/usr/bin/env python2) and the PinNo. I'm toggling PAUSE/PLAY now "by button" 8-)
  • HiFi 1: Alix/voyage-mpd, MusicalFidelity M1 DAC (RP2/HifiBerry Digi+), Accuphase DP-67, Accuphase E-212, Infinity Kappa 8.2i
  • HiFi 2: RP2/7" Touchscreen/IQaudIO Pi-DAC+, Philips Fidelio X1
Posts: 45
Joined: 22 Feb 2016, 12:11

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 20:47

After adding the call to rune_shutdown, from Frank's post above:

Code: Select all
#!/usr/bin/env python2
import RPi.GPIO as GPIO
import time
import subprocess

#Select unused GPIO header pin to be used for shutdown
InputPin = 13

# Set selected pin to input, and enable internal pull-up
GPIO.setup(InputPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

# Wait for a button press on the selected pin (pin pulled to ground, falling edge)
GPIO.wait_for_edge(InputPin, GPIO.FALLING)

# When pressed, execute a shutdown, or any other command
print "*** Rune shutdown activated ***""/var/www/command/rune_shutdown poweroff", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print "*** Netctl shutdown activated ***""/sbin/shutdown now", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

After installing it as a service, it can be checked using
Code: Select all
sudo systemctl status soft_off.service

and the output should look like this:
Code: Select all
[root@runeaudio ~]# sudo systemctl status soft_off.service
● soft_off.service - Soft_Off Service
   Loaded: loaded (/usr/lib/systemd/system/soft_off.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 1970-01-01 01:00:08 CET; 46 years 3 months ago
 Main PID: 241 (python2)
   CGroup: /system.slice/soft_off.service
           └─241 /usr/bin/python2 /home/pi/

I can't really test anything until later tonight or tomorrow. Looking forward to other ideas.

For example, I am wondering if we can use the "RUN" header on the Pi2, the one next to the LEDs to bring the Pi out of shutdown, sort-of as a warm start. As far as I know, it works even when the OS is halted, so it should work here, too.
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby SimonDechain » 14 Apr 2016, 21:42

You should be careful with the "RUN" header. It is a reset input, if you toggle it while the RPI is running, you will reset it and that is not healthy (it could corrupt the sd-card).

If you want to have a soft-power on button without the reset danger, you could use GPIO3 (Pin 5 on the header). When shorted against ground, the pi will start booting. it was previously used as a safe-boot input, but that part of the firmware was removed. Some infos about the pin can be found here:

The disadvantage is, that the pin can not be used for something else (for example as the I2C interface).

I also implemented the same functionality (shutdown through a spst toggle button) abeit a bit differently. In my case I created a device tree overlay (gpio-keys), which registered the button as an input and triggered the "Shutdown key" from Linux. Only thing is, that you are not able to call the rune shutdown script directly. I'm still working on that, currently by trying to register a shutdown service.

If you want, I can share the dts file tomorrow evening, it is already late here in Austria.
Posts: 7
Joined: 12 Mar 2016, 19:19

Re: Momentary Contact Switch

Postby PeteB » 14 Apr 2016, 23:20

Hi Simon,

If you'd care to share then yes, please post your code. :)

Unfortunately my HiFiBerry DAC+ uses I2C, and so to some LCD's that other people here are using. So, yes, that would be ideal, but not everyone can make use of it.

I guess that the start circuit would have to be wired so it could NOT operate and cause a reset when the soft shutdown script is active. That should be possible because the pin used for shutdown is pulled high after start, and stays high until about three seconds after the button is pressed, and presumably released.

However, it may not be worth the effort to do that just for a soft start... :(
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby SimonDechain » 15 Apr 2016, 22:49

Hello PeteB,
Sure thing.

Code: Select all

/ {
        compatible = "brcm,bcm2835", "brcm,bcm2708", "brcm,bcm2709";

        fragment@0 {
                target = <&gpio>;
                __overlay__ {
                        button_pins: button_pins {
                                brcm,pins = <3>;    /* change the 3 here to the gpio where the button is attached */
                                brcm,function = <0>;
                                brcm,pull = <2>;    /* pull direction: none=0, 1=down, 2=up */

        fragment@1 {
                target-path = "/soc";
                __overlay__ {
                        keypad: keypad {
                                compatible = "gpio-keys";
                                #address-cells = <1>;
                                #size-cells = <0>;
                                pinctrl-names = "default";
                                pinctrl-0 = <&button_pins>;
                                status = "okay";

                                button@22 {
                                        label = "pwr_btn";
                                        linux,code = <116>;      /* The KEY_POWER defined in include/linux/input.h */
                                        gpios = <&gpio 3 1>;    /* Also change the 3 here to the one where the button is attached */

This is the source code. To compile it, you will need the "dtc-overlay" package: pacman -S dtc-overlay
And the compile command: dtc -@ -I dts -O dtb -o rune-power-gpio.dtb rune-power-gpio.dts
Copy the dtb to /boot/overlay and add the following line to /boot/config: device_tree_overlay=rune-power-gpio

After a reboot, this should create the following entry in your sys-fs: /sys/devices/platform/soc/soc:keypad
And in dev: /dev/input/by-path/platform-soc:keypad-event

If it does not, then something went wrong.

For the shutdown command to work, you have to unmask the systemd-logind daemon: systemctl unmask systemd-logind
Reboot and after that a low-pulse will instantly initiate a shutdown of the system.

Concerning the RUN-Pin. If you are familir with a solderin iron, you could use an OR-gate (something like an 74LV32) to wire the shutdown-pin and the toggle button together:
OR.png (9.76 KiB) Viewed 3292 times

Pi-Power-off: Input signal for the raspberry to initiate the shutdown
Shutdown-Pin: The pin, which is held high by the raspberry/external circuit
RUN: The Reset signal

With this, the RUN pin will only be pulled low, if both inputs are low, else it'll stay high.
Posts: 7
Joined: 12 Mar 2016, 19:19

Re: Momentary Contact Switch

Postby PeteB » 16 Apr 2016, 00:50


Thanks for sharing, I will look at the code more closely this weekend. I really have no preference, but your method is interesting, and worth investigating.

As for soldering parts, as long as I can still see them without a microscope, I can probably solder them.

I started this thread to see if we could have a simple switch that activates a soft "off" function after reading about Rikardo's experience with the Audiophonics kit. There are lots of examples in the Raspberry forum and elsewhere, but they are all for Raspbian and Debian-based systems.

Soft start/reset:

If the reset pin is used only as a soft start after a shutdown, then a switch can be enabled through software, using a second pin. For example, only after the rune_shutdown call, and before systemctl shutdown.

This way, a single double-pole momentary switch can be used for both functions, and you enable one or the other in software. If that does not work, one can always pull the plug, which is what most people do now.

Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary Contact Switch

Postby SimonDechain » 20 Apr 2016, 17:58

Ah, okay. If I've understood you correctly, you want to cut power to the pi after it powered down. At least, that's what the mousberry is doing. If not, can you make a sketch who you imagine it?

Unfortunately to cut the power with a push button, you'll need a bit more than a single switch (even if not much more :) ).

To reproduce it, one would basically, need a power-managment IC like the LTC2951 (This chip then can be controlled through the device-tree). This chip would be the easiest, one only needs 3 capacitory a resistor, the pushbutton and 2 gpios from the pi. Controlling it can be done wholely through the device-tree. Downside is the price of the IC (here is austria about 5-8€ / piece).

Alternatively the discrete version of the IC could be used. The website describes the function pretty well and it uses a neat circuit, which only needs a single gpio output. Unfortunately one will need a custom script to control the circuit.

Posts: 7
Joined: 12 Mar 2016, 19:19

Re: Momentary Contact Switch

Postby PeteB » 20 Apr 2016, 19:46

Hi Simon,

I was hoping for a discussion, about different ways to accomplish this. Maybe I should change the name of the thread, LOL. I'll post a pic tonight or tomorrow. You will likely see it tomorrow your time. It is pretty self-explanatory.

I do not want to duplicate a Mausberry. I had one for a while, and I can always buy another one, saving $15 would not be worth opening a thread, let alone the fun task of soldering small parts to a proto board (chuckle).

I can switch power with a small latching relay with dual coils (set/reset coils), IF I decide to do that.

Cutting power completely is not an absolute requirement (for me), because I already have a main power switch, and in any case the Pi with the DAC and dongles only uses 8 milliamps in shutdown (measured with a USB inline meter), and the AC supply draws ~0.25 watts at no load.

The LTC2951 is around $4 in single quantity, less than the cost of the caps and choke in my power supply filter, but more than the $2 for a small latching relay. I think I can solder SOT-23 (maybe, LOL). IRF 7319, around $1 (I will order a couple). Not sure this matters much, but the circuit does look interesting. The script, less so, because I am better at reading code than writing it.

I like the idea of using only one GPIO pin. I currently would need two (one for the button, one for the relay).

Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary On/Off Switch with soft shutdown

Postby PeteB » 21 Apr 2016, 04:10

Here is how I imagine the hardware would look, with everything on a small board about half the size of a Pi.

Standard barrel connector for a 5V supply, momentary switch on the same board close to the connector, or on an extended pair of wires, if inside an enclosure. Room for a small number of other components, latching relay or the mosfet switch from the example posted above, power supplied to the Pi and a DAC through the GPIO connector. Resettable fuse (Polyfuse), maybe.

The switch in the pics is temporary, the one I intended to use turned out to be too poor quality to bother putting it on the board.

The ribbon cable is a convenience, I don't have the patience to connect/disconnect several single-pin jumpers several times, let alone make new ones.



As I think I said somewhere else, I think I am making my own Pi erector set... :mrgreen:
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary On/Off Switch with soft shutdown

Postby Dohmar » 21 Apr 2016, 04:25

Would this system work with a 12v power source for a HAT? I have the x400 and it has a few gpio pins on top free that I could probably use...

Posts: 79
Joined: 14 Mar 2016, 12:27

support RuneAudio Donate with PayPal


Return to Raspberry Pi

Who is online

Users browsing this forum: cmh714 and 2 guests