Momentary On/Off Switch with soft shutdown

Raspberry Pi related support

Re: Momentary On/Off Switch with soft shutdown

Postby PeteB » 21 Apr 2016, 04:47

Probably, with one small change. The mosfet latching circuit is connected to one of the GPIO pins on the Pi through a resistor to 5V. This forms what is basically a voltage divider with the internal pullup so as not to be too incompatible with 3.3V logic. To work with a user selectable voltage, I think the resistor would have to be replaced with something else. 12V is too far from 3.3V to be safe for the Pi. (Simon may have a better answer)

A latching relay would work either way, b/c the contact is isolated from the control voltage.

I assume your DAC has a 12 V supply, but feeds the Pi a regulated +5?
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary On/Off Switch with soft shutdown

Postby Dohmar » 21 Apr 2016, 06:29

Yeah spot on. It has an input that provides 5v/3amp to the Pi, and uses (x) amount to feed the onboard mosfet (I dont know how much).
The dac has a 2.54pitch header that puts out 5v, though I dont know if it has its own converter or if its drawn from the 5v rail that feeds the pi and the amplifier). It also puts out whatever it is being fed - in my case I'm feeding it 12v/5amp, total of 60 watts. I'm guessing that if the amplifier is 2x 20 watts (40 watts/5v = 8amp+the 3amp the pi is fed for 11amp @ 5v = 44 watts, which if we assumed 48 watts, that'd leave 12 watts/12v@1amp leftover for me to tap with the 12v to 5v converter) to power the hdd, optical drive, touchscreen, dab tuner and usb ADC.

The circuit you've designed could still tap 3.3v/5v via the duplicated GPIO's on the HAT. Effectively I'd just want a single button which;
a) when pressed while pi is on, executes the halt script and then cuts the 12v feed to the HAT.
b) when pressed while pi is off, simply provides the 12v to the hat until pressed again.

B) is simple enough, and I've read this forum but confess I haven't followed it all - under condition a) how would the button/circuit know when to turn off - would the pi indicate when the system is ready for physical power out, or would you just add a timer that would give it 5-10 seconds to shutdown and then cut the power?

Sorry but EE is really not my strong part

-D
Dohmar
 
Posts: 79
Joined: 14 Mar 2016, 12:27

Re: Momentary On/Off Switch with soft shutdown

Postby ingohz » 21 Apr 2016, 07:36

PeteB wrote: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.


I like it!

I'll order some IRF 7319 and an adapter to get the SO-8 on a breadboard. Another 5V connector is a good idea and a real improvement. Don't worry about the software. We can do this and it will be fun ;)
  • 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
ingohz
 
Posts: 45
Joined: 22 Feb 2016, 12:11

Re: Momentary On/Off Switch with soft shutdown

Postby PeteB » 21 Apr 2016, 15:07

Just to be clear, I did NOT design that circuit, it is published on the website that Simon has linked above, I would just add it to what I was planning already, and replace the relay, or make the relay an option for the sake of isolation.

a) Power On->Orderly Shutdown->Power Off, we already have this part. I cut and pasted other scripts, and tested it with the button you see hanging in the air in my pics. The button I was going to mount on the PCB turned out to be crap, so I have ordered the smallest Alps switch I could find, from Mouser (still only about $1).

b) is what the circuit Simon posted is really for. The latching is done by the pair of complementary mosfets, instead of the relay.

The challenge is not the engineering so much as coming up with something that is actually useful, and does not end up costing more than a Pi (!).
PeteB
 
Posts: 421
Joined: 06 Feb 2016, 05:07

Re: Momentary On/Off Switch with soft shutdown

Postby Wizzer » 23 Sep 2016, 15:32

It's been a while since anyone last posted about the shutdown circuit. Has this progressed any further? I need to do the exact same thing and I'm hoping someone has designed it already.

Wizzer
Wizzer
 
Posts: 1
Joined: 23 Sep 2016, 15:24

Re: Momentary On/Off Switch with soft shutdown

Postby moocowe » 25 Sep 2016, 15:13

I've built the shutdown circuit previously linked to by Ingohz.
http://www.mosaic-industries.com/embedded-systems/microcontroller-projects/raspberry-pi/on-off-power-controller

I originally built it for a RetroPie setup, but intend to make another one for RuneAudio once (if?) I get the software side working. Apologies in advance if some of this is specific to Raspbian and not applicable in Arch.
Image
Setting up a Python script to detect a button press on the GPIO pin and issue shutdown commands is simple enough.
Code: Select all
import RPi.GPIO as gpio
import os

gpio.setmode(gpio.BCM)

gpio.setup(4, gpio.IN, pull_up_down=gpio.PUD_UP)
gpio.wait_for_edge(4, gpio.FALLING)

os.system('sudo shutdown -h now')


My problem is, I can't figure out how to drive the pin low at the last moment before halt/poweroff, but not at reboot.

I've tried the following in systemd, but it doesn't work, presumably because it's after everything is unmounted and I'm unsure of a way round this. The Python script sets pin 4 output low and instantly kills the power if I slightly modify the service and remove umount.target.
Code: Select all
[Unit]
Description=Cut power on shutdown
DefaultDependencies=no
Conflicts=reboot.target
After=umount.target
Before=shutdown.target

[Service]
Type=oneshot
ExecStart=/bin/true
ExecStop=/usr/bin/python /home/pi/cutpower.py
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target


I've also tried the following script in /lib/systemd/system-shutdown, but I don't think it's doing anything at all.
Code: Select all
#!/bin/sh

if [ "$1" = "poweroff" -o "$1" = "halt" ]
then
        echo "4" > /sys/class/gpio/export
        sleep .5
        echo "out" > /sys/class/gpio/gpio4/direction
        echo "0" > /sys/class/gpio/gpio4/value
fi


Since the circuit only uses 1 GPIO pin, I can't use dtoverlay=gpio-poweroff in /boot/config.txt, otherwise pin4 is changed to low at boot, and the power is lost.

I'm hoping someone has managed to get this working with a single GPIO pin.
moocowe
 
Posts: 2
Joined: 25 Sep 2016, 14:13

Re: Momentary On/Off Switch with soft shutdown

Postby hondagx35 » 25 Sep 2016, 21:43

Hi moocowe,

I've also tried the following script in /lib/systemd/system-shutdown, but I don't think it's doing anything at all.

This should be the correct way to do it.
Is your script executable?
Code: Select all
chmod +x /usr/lib/systemd/system-shutdown/yourscript


Frank
User avatar
hondagx35
 
Posts: 3041
Joined: 11 Sep 2014, 22:06
Location: Germany

Re: Momentary On/Off Switch with soft shutdown

Postby moocowe » 25 Sep 2016, 23:12

Hi Frank,

Yes, the script "cutpower.sh" is executable, and owner is root. Is the system still able to write to /sys/class/ at the end of the shutdown sequence? I'm really not sure why systemd isn't running it otherwise.

Thanks,
Andrew
moocowe
 
Posts: 2
Joined: 25 Sep 2016, 14:13

Re: Momentary On/Off Switch with soft shutdown

Postby Tall Person » 31 Oct 2017, 00:20

Hi everyone.
Before I discovered Runeaudio, I was using BubbleUPNP with a shutdown script running very nicely on my original Raspberry Pi. The script was as follows:
Code: Select all
from time import sleep
import os
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(25,GPIO.IN,pull_up_down=GPIO.PUD_UP)
btn_timer=0
while True:
    if ( GPIO.input(25) == False ) :
        btn_timer = btn_timer+1
    else:
        if ( btn_timer > 5 ) :
            os.system('sudo shutdown -h now &')
        elif (btn_timer > 2) :
            os.system('sudo reboot &')
        elif (btn_timer > 0.5) :
            os.system('sudo ifup wlan0 &')
        btn_timer = 0
    sleep(0.5);

This was saved as a python script in the home/pi folder and then run using sudo chmod -x offswitch.py.
This piece of script worked great for reset, shutdown and resetting the wireless. HOwever, since installing Runeaudio and being happy with the interface, I find that I do not have enough knowledge to understand where or how this piece of code would be saved. I guess that the folder structure is different to the standard Raspbian build?

Could anyone please help me?

Thanks.
Raoul.
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

Re: Momentary On/Off Switch with soft shutdown

Postby Tall Person » 31 Oct 2017, 00:41

Would this procedure work with Runeaudio on a raspberry pi with a Hifiberry DAC anyone?
https://howchoo.com/g/mwnlytk3zmm/how-to-add-a-power-button-to-your-raspberry-pi
Sorry if it's a dumb question - from a relatively inexperienced perspective it seems straightforward.
-Raspberry Pi B rev 2, Hifiberry DAC
-Raspberry Pi Zero W, Hifiberry DAC, Runeaudio 0.4
Tall Person
 
Posts: 16
Joined: 16 Sep 2017, 19:43

support RuneAudio Donate with PayPal

Previous

Return to Raspberry Pi

Who is online

Users browsing this forum: cmh714 and 2 guests