paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Pi supply with 1 Start-Stop Button (again)

Sat Dec 05, 2015 10:20 am

In the meantime, I have added several different designs, so keep on scrolling down.

Just recently, I had a need to make a simple version of an on-off switch for the Pi. When I design and try things out, I typically boot and shutdown a Pi while I’m testing things. I don’t want to always log in and run a poweroff command, to shutdown the Pi. Sometimes, when I don’t have a connection, it’s tempting to yank the power, and of course, after several years of doing that unpunished, I trashed one of my older SD cards so much that it does not work reliably any longer.
So, I put together a simple power supply with an on-off switch, that took advantage of a couple of earlier forum posts. With the amount of Pi users growing and growing, there must be demand for a simple and inexpensive solution to the challenge.
Here are both original posts:
viewtopic.php?f=107&t=113789
viewtopic.php?f=41&t=117863&p=800623#p800623

Both posts were created in a “fact-finding” mode, and several forum users and the foundation contributed to make it work. It’s a little complex and probably confusing to follow if you’re only interested in a working solution, although both posts have one. So there is nothing really new here, just more complete.

Here is the simple on-off schematic we’re working from:
Start_Stop.png
Start_Stop.png (16.24 KiB) Viewed 7821 times
Here is the python daemon code that waits for the on-off chip to signal a requested shutdown, and also the removal of power from the Pi:

Code: Select all

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets installed in /etc/init.d to run as a daemon.
#               It monitors the /INT signal coming from the start-stop device,
#               an LTC2951-1 to start a shutdown procedure.
#               If the LTC2951-1 asserts the /INT pin, we start a shutdown to
#               Halt the Pi, by executing the poweroff command.
#
#               The power to the Pi will be cut when the timer of the LT2951
#               runs out, or if the Pi has reached the poweroff state (Halt).
#               To activate a gpio pin with the poweroff state, the
#               /boot/config.txt file needs to have :
#               dtoverlay=gpio-poweroff,gpiopin=27,active_low="y"
#
# Author:      Paul Versteeg
#
# Created:     15-06-2015, revised on 4-12-2015
# Copyright:   (c) Paul 2015
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import RPi.GPIO as GPIO
import subprocess

GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)

# I use the following two GPIO pins because they are next to each other,
# and I can use a two pin header to connect the switch logic to the Pi.
INT = 17  # GPIO-17 /INT interrupt to shutdown procedure
KILL = 27 # GPIO-27 /KILL : this pin is programmed in /boot/config.txt

# use a weak pull_down to avoid noise issues
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def main():

    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)
        subprocess.call(['poweroff'], shell=True, \
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)

if __name__ == '__main__':
    main()
Then there is the shell script that gets installed in /etc/init.d, to start and stop the python program at boot/shutdown:

Code: Select all

#!/bin/sh

### BEGIN INIT INFO
# Provides: start_stop_service
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: This deamon controls the shutdown_daemon to handle a
# start_stop switch using an LTC2951-1
#
# Description: The App has code that will initiate a poweroff command when the
# user wants to shutdown the Pi and power it off.
### END INIT INFO

# Call this shell script start_stop_service.sh
# This shell script will call shutdown_daemon.py
# Copy this init script into /etc/init.d using
#       sudo cp start_stop_service.sh /etc/init.d/.
# Make sure the script is executable
#       sudo chmod 755 /etc/init.d/start_stop_service.sh
#
# At this point you should be able to start the Python script using the command
#       sudo /etc/init.d/start_stop_service.sh start
# Check its status with
#       sudo /etc/init.d/start_stop_service.sh status
# and stop it with
#       sudo /etc/init.d/start_stop_service.sh stop
#
# to install start_stop_service in the boot sequence :
# sudo update-rc.d start_stop_service.sh defaults
#
# to remove from the boot sequence :
# sudo update-rc.d start_stop_service.sh remove

# Change the next 3 lines to suit where the python script is and what you want to call it
DIR=/home/pi
DAEMON=$DIR/shutdown_daemon.py
DAEMON_NAME=shutdown_daemon

# make sure that shutdown_daemon.py is executable and the the Python shebang is present
# chmod 755 shutdown_daemon.py

# This next line determines what user the script runs as.
# Root generally not recommended but necessary if you are using the Raspberry Pi GPIO from Python.
DAEMON_USER=root

# The process ID of the script when it runs is stored here:
PIDFILE=/var/run/$DAEMON_NAME.pid

. /lib/lsb/init-functions

do_start () {
    # check to see if we can detect the start-stop connector on port 17
    # it should be pulled high. >> cannot use 27, because that port is already reserved
    # when we called the power_off overlay in /boot/config.txt <<
    port17=""
    echo "17" > /sys/class/gpio/export
    echo "in" > /sys/class/gpio/gpio17/direction
    port17=`cat /sys/class/gpio/gpio17/value`
    echo "GPIO17 is $port17"
    # clean up
    echo "17" > /sys/class/gpio/unexport
    if [ $port17 -eq 1 ]; then
       log_daemon_msg "Start-Stop connector detected on 17, starting system $DAEMON_NAME"
       start-stop-daemon --start --background --pidfile $PIDFILE --make-pidfile --user $DAEMON_USER --chuid $DAEMON_USER --startas $DAEMON
       log_end_msg $?
    else
        log_daemon_msg "NO Start-Stop connector detected on 17, NOT Starting system $DAEMON_NAME"
        echo ""
    fi
}
# we use the --background flag of start-stop-daemon to run our script
# in the background

do_stop () {
    log_daemon_msg "Stopping system $DAEMON_NAME"
    start-stop-daemon --stop --pidfile $PIDFILE --retry=TERM/10/KILL/5
    log_end_msg $?
}
################################################################################
# if you need time to backup data or do something that can extend the powerdown
# sequence, read the following carefully! :
################################################################################
# the --retry means that first of all a TERM -15 signal is sent
# to the process and then 10 seconds later it will check if the process is still there
# and if it is send a KILL -9 signal (which definitely does the job).
# if you catch the TERM-15 signal within the app, you can control the shutdown
# sequence and properly save all important data for a restart. You don't want
# to be caught unprepared for the Kill -9, if needed, extend the 10 seconds.

case "$1" in

    start|stop)
        do_${1}
    ;;

    restart|reload|force-reload)
        do_stop
        do_start
    ;;

    status)
        status_of_proc "$DAEMON_NAME" "$DAEMON" && exit 0 || exit $?
    ;;

    *)
        echo "Usage: /etc/init.d/$DAEMON_NAME {start|stop|restart|status}"
        exit 1
    ;;
esac

exit 0
Finally, here is the modification for the /boot/confix.txt file that will enable the poweroff signal for one of the GPIO pins:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=27,active_low
Put this at the end of the file.

A few comments are probably in place to explain a few things.
Let’s start with the schematic. I prefer to use 12V DC wall-warts because they are in abundance, are typically designed to supply a steady and clean power at carefully regulated voltage and current, as opposed to telephone or tabled chargers. Second, it allows the use of a dc-dc buck regulator to supply the Pi with a precise 5V0 at the Pi PCB, compensating for cable and connector losses. This second stage even regulates the power better, and the expense is less than $10 for a very small PCB.

The green LED (LED1) signals that we have power to supply the Pi. The Pi itself will light-up like a X-mass tree, especially when you have a LAN cable connected, so I don’t need an additional LED to show the Pi has power. The second LED, a red one (LED2) is normally off. It has a dual function. It will light-up as soon as the “stop” timing threshold (set by C1) has been met and that the shutdown/poweroff procedure for the Pi has started. The GPIO_poweroff action will cut the power as soon as the Pi is halted.

The second function of the LED is to protect the GPIO input from excessive voltages. The /INT input to the LTC2951-1 is open collector, so needs a pull-up to the 5V supply. We don’t want that on the GPIO input, so the LED acts as a voltage dropper, and together with R5, a 1K resistor, limits the current to significantly less than 5mA. Current kills, not so much the voltage.

If, for whatever reason, the GPIO-poweroff event does not take place, the power will be cut after approx. 60 seconds, set by C3. So even if you don’t program the gpio_poweroff event, power will still be cut.

The /KILL input to the LTC also needs a pull-up, so we use a 10K resistor, and again, to protect the Pi output, we use a 1K resistor in series. The maximum current will flow when the GPIO output is forced low, but this only takes place for less than 3 mSec, because the Pi is switched off, as soon as the Kill signal has been sent. Here is a screenshot to prove it.
IMG_2929-1.jpg
IMG_2929-1.jpg (49.37 KiB) Viewed 7821 times
The readout of 3.350V is the level before the Kill signal goes low, no problem for the Pi.
So, really no harm possible!

The python and the shell scripts are documented so you should have no problem with it. The Python code can also be embedded within your own application. Note that I used GPIO-17 and GPIO-27, because they are next to each other on the P1 connector, and I can use a dual pin header. You can use just about any GPIO pin, just shy away from the ones with a special function, unless you know the behavior of those pins really well.

Here is a picture of my implementation, it is housed in a little plastic box that they sell screws etc. in. My humble contribution to recycling plastic. I used a very good USB power cable to supply the Pi with 5V through the micro USB input. I just cut the cable at a convenient length. You can get these cables with a DC barrel connector on the other end (eBay), and they can handle several Amps.
IMG_2930.JPG
IMG_2930.JPG (36.78 KiB) Viewed 7821 times
Enjoy!
Last edited by paulv on Thu Jul 14, 2016 11:40 am, edited 11 times in total.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with Start-Stop Button (again)

Sat Dec 05, 2015 10:26 am

This design is so simple and effective, that it really begs for a complete PCB with all required components. Here is what I mean:
Power w. Start-Stop.png
Power w. Start-Stop.png (21.92 KiB) Viewed 7801 times
Unfortunately, I do not possess the skills to make a layout for this, that’s on my bucket list for another day, but maybe someone else can do that, and make the PCB layout available to others on this forum. There is a good chance that my first PCB (well, after 35+ years) will be this one, but don't hold your breath!

However, with the millions of Pi's out there, I imagine that there is interest enough.
I can send you the Eagle schematic file, but I only use Eagle to document, not with the aim to turn it in a PCB. It's probably better to start again.
Sorry!

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with Start-Stop Button (again)

Wed Dec 09, 2015 2:51 pm

Of course you don't need to use a dc-dc buck converter if you have a 5V wall-wart that supplies a solid 5V0, or better yet at least 5V1. I made yet another version for such a supply myself.

The voltage drop between the output of the wall-wart through the switch and to the Pi, in my case, was 160mV. This is not so much the MOSFET, but more the wiring, cable length and connectors.

Even if you "only" have a 5V0 wall-wart, the Pi can handle the somewhat reduced voltage, but be careful when you also "hang" devices off the USB ports. Especially WIFI adapters and disk drives can be power hungry.

So here is an even more simple power on-off switch:
IMG_2934.jpg
IMG_2934.jpg (36.83 KiB) Viewed 7646 times
it follows the hardware design in the first post, including the software.
Start_Stop-5V1.png
Start_Stop-5V1.png (29.9 KiB) Viewed 7379 times
If you consider building any of these variations, I recommend that you order the LTC2951-2 version of the start-stop controller. These chips are still pretty hard to get, and sell for around $5 per chip, so get the right one.

Originally, I wanted to use the controller in a different kind of application, and needed the EN output. In the application I show here, the /EN version of the -2 is more optimum, because it eliminates Q3, an N-FET, to drive the Q2 P-FET switch. All you need to do is to eliminate Q3 and tie the /EN output directly to the gate of the P-FET. The /EN and the EN outputs are open drain, so they both need the R6 pull-up resistor, the value of which can be higher. I can recommend the Si3443DV as a P-FET because I've used it in several designs to switch a 5V supply voltage. They sell for under $1.

The SMD to DIP adapters I use can be ordered through several places, just search for "smd adapter" on Amazon or eBay and ye shall find.

Enjoy!
Last edited by paulv on Mon Dec 14, 2015 8:55 am, edited 4 times in total.

User avatar
karrika
Posts: 1317
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Pi supply with Start-Stop Button (again)

Wed Dec 09, 2015 3:03 pm

Thank you for sharing. I have a bit similar needs. This is a very good starting point.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with Start-Stop Button (again)

Fri Dec 11, 2015 3:07 pm

And here is yet another method or variation to the same design when you use a 12V DC wall-wart as the main power supply.
Power with start-stop-2.png
Power with start-stop-2.png (37.15 KiB) Viewed 7493 times
In this case, I switched the DC-DC Buck converter with the EN output from the LTC2951-1.

This is for the more brave, because you have to pry loose pin 5 of the LM2596-ADJ in order to switch the output voltage on or off. This pin is normally tied to ground, but if you raise it to a positive level (above 1.3V), you can disable the output. http://www.ti.com/lit/ds/symlink/lm2596.pdf

With this variation, you can eliminate the P-channel MOSFET from the other schematics, and because the input voltage is above 5V, you can also select a much more common through hole N-channel MOSFET, like the BS170.

You can even eliminate the N-channel MOSFET all together if you obtain the LTC2951-2 version of the on-off controller, because that version can drive the DC-DC converter on/off pin directly. The On/Off Pin 5 of the LM2596 seems to be pulled high internally so you can also delete R4 and R6.

Enjoy!
Last edited by paulv on Thu Dec 31, 2015 5:51 am, edited 1 time in total.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with Start-Stop Button (again)

Mon Dec 14, 2015 8:32 am

After thinking some more about the start-stop-with-one-button challenge, I came up with yet another approach, and this design uses even less parts, and, no (active) SMD parts.

[EDIT to add some more safety and reliability, I have changed the circuit a bit from the original post. The schematic and text has been updated from the earlier version]

I'm using a few tricks here, and let me explain them by using the schematic:
Start_Stop-3a.png
Start_Stop-3a.png (26.73 KiB) Viewed 6981 times
Let's start with the power off situation. When the 12V DC wall-wart is powered-up, the on/off input from the LM2596 (pin 5) is pulled high. First of all by what seems like the internal pull-up of the device, and second through R5 and R3 etc. The Pi has no power, such that the GPIO (KILL_PWR) output pin has no effect yet.

Because we're using a lethal voltage level for the Pi, 12VDC, the voltage divider R1 and R2 will create a 3V3 level which is safe.

If you press the push-button, the voltage across C1 will drop to ground level, activating the LM2596. The on/off or Enable pin needs to be below 1.3V for the device to turn on. When you release the push-button, C1 will start to recharge slowly through R3, the 150K resistor. The R/C has been chosen such that it will take about 5 seconds before the voltage across C1 will rise above the 1.3V threshold. The voltage across C1 will stabilize at about 3V. This is high enough for the LM2596 to turn off the output. In the meantime however, the Pi has started to boot. In /boot/config.txt, we have added the following line:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=27
After about 3.6 seconds into the boot sequence, this statement will set GPIO-27 to active high, and the output level will be driven to ground, effectively taking over from C1 and keeping the LM2596 activated from now on. Pressing the button again during the boot process will have no effect, because our Python program is not yet running.

Toward the end of the boot process, our daemon gets activated and the program sets a pull-up on the input pin. From now on, when the push-button is pressed, we will recognize a falling edge, and the program starts the power down sequence.

As soon as the Pi is halted, the GPIO-27, which was programmed with our gpio-poweroff overlay is activated and since is it active high, it pulls the on-off input of the LM2596 high, cutting the power to the Pi. Pressing the button again starts the whole sequence once more.

So what do we not get in comparison with the TLC start-stop controller? Well, we don't have a de-bounce for the button, but that is no issue with our design. We will also not have the option where the LTC kills the power if the gpio-poweroff is not activated for some reason. I don't think this is a real possibility, but a small price to pay for the simplicity.
The last item that will be missed at first glance is the fact that with the LTC chip, you needed to push the button for a selectable period (I used 4 seconds) before the shutdown would be activated. With this hardware design, it is instantaneous, so make sure you don't accidentially push the button, or add some code to the daemon program to make a longer period.

I believe that this is a very nice and simple solution, with one caveat, you need to use a 12V DC wall-wart and a DC-DC Buck converter because that also acts as the power switch. Of course you can use a 5V1 wall-wart, and use a P-channel MOSFET again to switch the power, but then we're back to needing SMD parts once more.

Here is the Python program with a few minor changes:

Code: Select all

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets installed by a /etc/init.d program to run as a daemon.
#               It monitors a button press. If the user presses the button, we
#               Halt the Pi, by executing the poweroff command.
#
#               The power to the Pi will be cut when the Pi has reached the
#               poweroff state (Halt).
#               To activate a gpio pin with the poweroff state, the
#               /boot/config.txt file needs to have :
#               dtoverlay=gpio-poweroff,gpiopin=27
#
# Author:      Paul Versteeg
#
# Created:     15-06-2015, revised on 4-12-2015
# Copyright:   (c) Paul 2015
# Licence:     <your licence>
#-------------------------------------------------------------------------------

import RPi.GPIO as GPIO
import subprocess
from time import sleep

DEBUG = True

GPIO.setmode(GPIO.BCM) # use GPIO numbering
GPIO.setwarnings(False)

# I use the following two GPIO pins because they are next to each other,
# and I can use a two pin header to connect the switch logic to the Pi.
INT = 17  # GPIO-17 button interrupt to shutdown procedure
KILL = 27 # GPIO-27 /KILL : this pin is programmed in /boot/config.txt

# use a weak pull_up to create a high
GPIO.setup(INT, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def main():

    subprocess.call(['echo "starting daemon"'], shell=True)
    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)

        if DEBUG: print "edge detected on /INT - shutdown"

        subprocess.call(['poweroff'], shell=True, \
            stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        sleep(10) # to avoid hammering the interrupt
    if DEBUG : gpio(cleanup) # a shutdown will reset the gpio's too

if __name__ == '__main__':
    main()
The daemon controlling shell script does not change, you can use the one that I published in the first post.

I have created a dedicated HOW-TO post that has more information if you decide to build this. viewtopic.php?f=91&t=129357

Enjoy!
Last edited by paulv on Fri Jan 01, 2016 2:53 pm, edited 5 times in total.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with 1 Start-Stop Button (again)

Thu Dec 17, 2015 7:54 am

The above method with no active parts except for the cheap DC-DC Buck converter has two caveats that may bother some of you.

When the whole chain is without power and the 12V DC is turned on for the first time, the Pi will get power and boot completely.

Only after the Pi has been stopped by the button, it will get powerless and can be started with the button again.

Second, when you execute e reboot (sudo reboot), the Pi will do a shutdown and will become powerless. You have to manually press the button to restart the Pi.

I just wanted you to be aware.

Enjoy the holidays!

PS I have added a detailed HOW-TO in the beginners section : viewtopic.php?f=91&t=129357
Last edited by paulv on Fri Dec 18, 2015 8:39 am, edited 3 times in total.

User avatar
karrika
Posts: 1317
Joined: Mon Oct 19, 2015 6:21 am
Location: Finland

Re: Pi supply with 1 Start-Stop Button (again)

Thu Dec 17, 2015 8:28 am

Thank you paulv.

In the Hardware->Other Projects->weather station we are planning to add a rotary encoder with push switch to create an on/off button for PiZero.

Many cheap LCD panels use 12V for input so I modified your circuit a bit to get both 12V and 5V out of it. The PiZero consumption is very low so perhaps a 7805 would be a good solution for the 5V.
RotaryCap2.png
RotaryCap2.png (16.12 KiB) Viewed 7269 times
RotaryCap.png
RotaryCap.png (43.19 KiB) Viewed 7269 times
I plan to use the push button also for normal set functionality. A long push would be identified in software and that would activate a "shutdown now" on the RPi. In this way I don't need to waste GPIO lines. I actually need all the GPIO's for some other purposes.

Comments?

dan412
Posts: 6
Joined: Sun Dec 20, 2015 8:12 pm
Location: Brooklyn, NY

Re: Pi supply with 1 Start-Stop Button (again)

Mon Jan 18, 2016 5:11 am

Which type of push button switch can you use for this? Any recommendations?

Thanks.

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with 1 Start-Stop Button (again)

Mon Jan 18, 2016 6:28 pm

I use these. They also exist with longer button, which are easier if they need to go through a lid or panel.
http://www.amazon.com/6x6x4-5mm-Momenta ... ush+button

paulv
Posts: 564
Joined: Tue Jan 15, 2013 12:10 pm
Location: Netherlands

Re: Pi supply with 1 Start-Stop Button (again)

Wed Apr 27, 2016 10:40 am

I have designed yet another version of this supply based on the design above, but this time with a Li-ion based UPS. It's even more compact and still easy to build.

Have a look here :
viewtopic.php?f=37&t=145954&p=962385#p962385

Enjoy!

User avatar
MD500_Pilot
Posts: 57
Joined: Sun Jun 05, 2016 4:12 am
Contact: Website

Re: Pi supply with 1 Start-Stop Button (again)

Sun Jun 05, 2016 5:50 am

I use these guys:

http://lowpowerlab.com/atxraspi/

Cheap and Easy ($14)

Or you can add a UPS and a power controller to your Pi:
http://lowpowerlab.com/mightyboost/
I fly because it releases my mind from the tyranny of petty things....

Return to “Automation, sensing and robotics”