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

HOW-TO Start/Stop/Powerdown Pi with 1 Button

Fri Dec 18, 2015 8:38 am

The Raspberry Pi does not have a start/stop button like a PC, and can be finicky with the 5V power level, especially if you use (power hungry) WIFI dongles or Disk drives connected directly to the USB ports.

There are several commercially available solutions on the market and there are quite a number of solutions created by Forum members. I have several designs published as well (just search for paulv). Unfortunately, many of these solutions can be expensive, or use parts most (beginner) forum members do not have, are hard to get, or cannot handle, like tiny, tiny SMD parts.

In the course of a lot of experimenting and trying many things out, I found a very simple and inexpensive solution to the challenge of providing a solid 5V to the Pi Module, and by adding a single button that will allow you to start the Pi, stop it (executing a powerdown command) and then making the Pi powerless.

All it takes are a momentary push button, a few resistors, one capacitor, a Zener diode and optionally an LED. Most of you will have these parts in your stash already, otherwise they are commonly available parts you can get for a few dollars or Euros. The only other components you need is a DC-DC Buck converter, and a little box to put things in. You also need one USB micro cable that has solid power wires. Lastly, you need a DC wall wart that supplies anything from 7V to 40V with a minimum of 1 Amp, better is 2 or more. If you are like me, you will have several of these supplies somewhere. They can come from old routers, USB hubs, modems, even from printers or laptops. These supplies were designed to provide a steady, reliable voltage and current and will most likely have all sorts of filters and safety measures, unlike many phone or pad chargers.

[EDIT: if you have a 5V supply or use batteries, read on down to a specific post for that situation]

We will use a DC-DC Buck converter to reduce the voltage from the supply you have to a steady and precise 5V for the Pi. The Buck converter will also be used to switch the power, and it is important that the converter is based on the LM2596-ADJ chip. Please check that when your ordering this little board. They can be purchased for as little as a few Dollars or Euros, depending on the delivery time you prefer. Here is a picture of such a Buck converter :
Buck Converter.jpg
Buck Converter.jpg (19.63 KiB) Viewed 23126 times
If you are planning to use an old inkjet printer supply, chances are they supply 38V DC, so look for a Buck converter that can have an input voltage of 40V. The LM2598 can handle this voltage, but the input cap may not. Many input capacitors are rated for 35V, be aware!

You'll also need a really good USB cable to avoid power losses in the cable itself. I personally use several of these:
USB Power Cable.jpg
USB Power Cable.jpg (17.26 KiB) Viewed 23126 times
Search for "micro USB power cable DC barrel"

Here is the schematic we're working from:
Start_Stop-3V3.png
Start_Stop-3V3.png (28.67 KiB) Viewed 22388 times
[EDIT: To get more reliable result with various tolerances, R2 and C1 have been changed. This is reflected in the updated schematic and the text of this post]

For the sake of this how-to, we will assume you have a very common 12V DC wall wart or supply. To connect that to our little power board, you can either get yourself a matching DC power barrel connector, or just cut the cable and solder the leads (check for the proper polarity!) directly to the Buck converter.

Before you do anything else, connect your DC supply to the Buck converter and adjust the output level of the Buck converter to a precise 5.1V, or 5V1. Do not omit this step right here right now, or you may fry your Pi.

The Buck converter now needs to be modified so we can used it also as a power switch. This is a little tricky, but not difficult. You need to lift pin 5 of the LM2596 chip from the solder pad on the board. This pin turns the output on or off and we need to attach a small wire to it. This pin is normally connected to ground. Make sure you add some fresh solder and/or solder paste to this pin, so the solder is more fluid when you heat it. You can use a small pair of tweezers, a pen knife or even a little wire looped around the pin to lift the pin while you're heating it. Don't pull on it hard, because you will most likely lift the PCB pad and track with it. If all else fails, you can also cut the pin with a fine cutter, as long as there is enough pin left to solder a wire to it. You can test success by applying power to the board again and connect the pin to something with a voltage that is above 1.5V. This input pin has Logic TTL specifications. However, according to the specification, you can connect it to the input. If you were successful the output should have no voltage, if there is power, you may still have a solder bridge. The chip seems to have an internal pull-down resistor, but according to the TI spec, you should not let it float, but if you do, it does not do anything. If all is well, solder a thin piece of wire to the pin going directly to the chip. To test it once more, attach the other end of the wire to ground, and you should have an output voltage again. Connect the wire to the input and the output should be gone again. This was the hard part.

To supply power to the Pi, you can again use a DC barrel connector, or cut the DC barrel from the USB cable, strip it and solder the leads directly onto the output solder pads of the Buck converter. Make sure you connect the wires correctly to the Buck converter! The other end with the micro USB connector goes to the Pi to power it.

I highly recommend you to power your Pi through the micro USB connector and not through the P1 connector. By doing that you will disable the over-voltage protection of the Pi. When you're working with 5+ voltages anywhere near your Pi, you don't want to take that risk.

Now for the rest of the logic. The design is almost completely independent of whatever you have in your stash of wall warts. You can use just about any voltage between 7V and 40V, just make sure it has the proper wattage or amount of Amps (1 minimum, 2 or more is better).
The voltage coming from the supply goes through a little circuit to reduce the power to a 3V3 level that the Pi GPIO pins can handle safely. We use R8 to create a bias current of 5mA for the 3.3V zener diode, and this is the only component that needs to change based on your supply voltage. For anything from 10 to 15 V, you can use the 1K5 value. The formula for other voltages is in the diagram.

If you only use one particular wall-wart supply, and will not regularly change to other wall-warts (promise to yourself in writing!) that have other voltages, AND you don't have a 3V3 Zener diode, you can use a voltage divider instead. The Zener needs a bias current of about 5mA to produce 3V3. The circuit itself can use less (somewhere between 0.5 and 2mA), and at the same time, is even more safe for the GPIO pins. If you use a 12V DC wall-wart, the total resister value can be 12K to give a current of 1mA (12V / 1mA = 12K Ohm). You can use a 8K2 resistor in place of the 1K5 and use a 3K3 resistor in place of the Zener diode. This will produce a voltage that is about 3V4, which is close enough, and safe. However, BEFORE you connect your precious Pi, measure the voltage at the junction of the two resistors and verify it is indeed somewhere around 3V4, but not more than 4V. If you use another wall-wart voltage, or other resistors, you can use this calculator if you need it : http://www.raltron.com/cust/tools/voltage_divider.asp Using the calculator, plug in your supply voltage, the 3V4 as output voltage and the value of R2, which is 3K3. You'll get a value for R1, just round it to the closest resistor value. You don't need to be ultra precise, just make sure the voltage at the junction is above 3V2 and below 4V0.

The charge resistor R2 and the capacitor C1 need to provide at least about 2V at their junction when fully loaded. This limits the value range of R2, it can't be too high otherwise the voltage will not get high enough. This voltage level is important to pull the Enable pin of the LM2596 high, which will turn the output off.

When you press the button, the voltage across the capacitor and the Enable pin, through R3, a 10K resistor, will go to zero, and start to ramp up when you release the button again, because of R2. The ramp-up needs to be a little longer than it takes for the Pi to boot and to load the overlay, which will activate a low on GPIO-27, the KILL_PWR output. This active low will drive the voltage at the Enable pin close to ground through R6, another 10K resistor. So the KILL_PWR driven by the overlay will take over from the capacitor charge circuit and keep the power on from then onward. (the voltage at the Enable pin needs to be below 1.3V for the LM2596 to turn on). R3 and R6 need to be of the same value, allowing a tug of war for the action at the Enable pin, but not disturb the circuit on the other end so to speak.

This is what I mean with the tug of war at the Enable pin. During the boot process, when GPIO-27 is forced low through the overlay action, the Enable pin will be at about 200mV, because of the pull upwards through R5 and R3 towards 3V3. This is a pull up of about 160K, and will not be strong enough to lift the Enable pin towards a shut-off of the LM2596. It only causes the pin to go to about 200mV. Pressing the button during this period in the boot process will only make the level of the Enable pin to go more towards ground and as such will have no effect.

Towards the very end of the booting process however, our Python program gets loaded, and that will start to watch out for a button press to signal a powerdown request. To make the program recognize this button press, it sets the GPIO pull-up feature on the PI_SHUT_DOWN input. However, this will only add another 50K pull towards the 3V3 level at the button end, which lifts the Enable pin a little higher (to about 500mV) through R5, the 10K resistor. That will still have no effect on the LM2596 output because it is still nowhere near the 1V3 cut-off level.
At the button end however, the internal gpio 50K pull-up, through the protection resistor R9, will force about a 1V level at that junction, such that a push of the button will be recognized by the Python program correctly as a negative edge and a powerdown will start. At the end of that process, GPIO-27 will go high forcing the Enable pin way above the 1V3 level, and the power to the Pi will be cut.
A subsequent press of the button will restart the whole process and since the Pi is powerless, there is no effect on the Enable pin through Either the KILL_PWR signal or the PI_SHUT_DWN pin, so we really start the whole process from scratch again.

In normal situations, with a Pi Model B rev 2, it takes about 3.6 seconds from power-up until the overlay is loaded.

NOTE
I found that after the latest firmware updates, the activation time went from 3.5 sec to about 5 sec. Depending on the tolerances of R2 and C1, you may want to increase either or both to create an 8 sec. window to be safer from further changes.

To be on the safe side for future changes (nope!) in the boot process, the R2 and C1 values will create a period of about 5 seconds before the power gets cut again. This time is highly dependent on the values of the components, and especially electrolytes (C1) can have tolerances of 20-30%. It is no problem to extend the period to more than 5 seconds, just don't go too close to 3.6 seconds because the Pi may end up without power during the boot process.

To connect the two GPIO pins to our little circuit, I used an old cable from a PC reset or power switch (proper reuse!). They have a nice two-pin connector with the proper spacing, so you can directly push it on the P1 connector of the Pi. I recommend you use GPIO-17 and GPIO-27 because they are next to each other on the P1 connector, and are general purpose pins with no surprises. You can use any GPIO pin you like, just make sure you change the following settings to reflect that. I do recommend you stay away from all the special GPIO pins, to avoid nasty surprises.

How you connect the other parts together depends on your skills and available perf. or prototype board. It should not be hard to connect this all together.

If you want to have an indication that your wall wart is plugged in, I suggest you add R1 and the LED.

When you have finished the hardware, don't connect it yet to the Pi. We need to prepare it first. Boot the Pi and get yourself to the prompt.

There are three things we need to install on the Pi in terms of software.
First open up an editor to modify the configuration file:

Code: Select all

sudo nano /boot/config.txt
Go to the very end and add this line:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=27
Save the results and close the file. This statement will tell the OS to treat GPIO-27 differently. Shortly in the boot sequence, this pin will be set active high, and the pin will be driven low while not activated. We use this pin to cut the power to the Pi after it has completed the power-down (or shutdown) sequence.

Now we need to add a program that will listen to the button press that signals a stop. We will use GPIO-17 to listen to the button press. In order to do that reliably, we will activate the GPIO pull-up that will allow the program to register a falling edge.

Here is that Python program:

Code: Select all

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets activated at the end of the boot process by
#               cron. (@ reboot sudo python /home/pi/shutdown_daemon.py)
#               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 then 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 18-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 button interrupt to shutdown procedure
KILL = 27   # GPIO-27 /KILL : this pin is programmed in /boot/config.txt and
            # cannot be used by any other program

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

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()
Start your editor again :

Code: Select all

nano shutdown_daemon.py
And cut and past the program, or type it in. Use Ctrl-X to save and exit the editor. This program uses an interrupt, so there will be no wasted processing power to speak of.

We will use the cron scheduler to start this Python program at boot time.
Start the crontab editor:

Code: Select all

crontab -e
Scroll to the end and start a new line with:

Code: Select all

@reboot sudo python /home/pi/shutdown_daemon.py
and use Ctrl-X to save the file and exit the editor.

Now it's time to test what you've done.

Code: Select all

sudo reboot
When the Pi has booted again, execute this :

Code: Select all

ps ax
and have a look if you see something like this:
2152 ? Ss 0:00 /usr/sbin/cron
2155 ? S 0:00 /USR/SBIN/CRON
2158 ? Ss 0:00 /bin/sh -c sudo python /home/pi/shutdown_daemon.py
2159 ? Ss 0:00 /usr/bin/dbus-daemon --system
2161 ? S 0:00 sudo python /home/pi/shutdown_daemon.py
2166 ? S 0:00 python /home/pi/shutdown_daemon.py
This will tell you that Cron has indeed installed the Python program, and it is running as root.
Shutdown the Pi to make it powerless :

Code: Select all

sudo poweroff
Disconnect the wall-wart from the mains such that everything is powerless. Also disconnect the Pi from the power source you have been using up to now.
Now it is time to connect the Pi to your new supply and also connect the two GPIO pins on the Pi to your hardware. Check if everything is correct, and plug the wall-wart back into the mains.

The Pi should boot normally. If you only get a momentary power to the Pi, you either made a mistake with the hardware, so check the wiring, or you may have a capacitor that is too far out from the value that I specified. You should be aware that electrolytes can be off by as much as 50%. If in doubt, measure the capacity, or use another one. You can also add a capacitor in parallel to C1 to increase the total capacitance value.

[Look for a post below for some trouble shooting tips if you have a problem with the hardware at this point.]

If your Pi booted up correctly, it's time to test the stop functionality. If you have a monitor connected, you don't need to log in, just press the button and the screen will tell you that the shutdown happened. If you log in through SSH, you'll get a message that the Pi will be halted.

As soon as the Pi is halted, the power will be cut, and you can restart the Pi by pressing the button again.

If everything is working you can now tweak the power supplied to the Pi by measuring the voltage on the Pi PCB (between TP1 and TP2 for classic Pi's or between P1-1 and P1-3) with a DMM. You can adjust the voltage to a precise 5.0V by adjusting the Buck supply and compensate for any losses in connectors and cables.

There are two caveats you need to be aware off with this simple but effective solution. One is that the first time when the power is supplied to the Pi (the wall-wart is plugged into the mains), the Pi will boot automatically. After that first time, the button will function normally.

The second caveat is that you cannot execute a software reboot (sudo reboot), because the Pi will become powerless and you'll have to manually press the button to power it up again.

I hope that this description will be complete enough for you to build this missing functionality for your Pi. Believe me, if you try and test things often on your Pi, you'll be grateful to have this capability.

Enjoy and happy holidays!

PS Here is some more information if you're interested : viewtopic.php?f=37&t=128019
Last edited by paulv on Sat Jun 04, 2016 5:46 am, edited 19 times in total.

FM81
Posts: 518
Joined: Wed Apr 17, 2013 4:33 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Fri Dec 18, 2015 9:27 am

Thanks, very nice ...

One question and one correction:
paulv wrote:... Go to the very end and add this line:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=27
Save the results and close the file.
This overlay is included in the latest Raspbian?
paulv wrote:... you may have a capacitor that is too far out from the 22uF that I specified. ... You can also put 2 x 47uF in parallel to get to the same value.
You have to put 2x 47µF in serial to get ~22µF, in parallel you are near to 100µF!

Greetings, FM_81
A: What does the command 'cat /dev/urandom', can you tell me please?
B: Yeah, that's very simple: It feeds your cat with radioactive material!

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Fri Dec 18, 2015 10:45 am

FM81 wrote:Thanks, very nice ...

One question and one correction:
paulv wrote:... Go to the very end and add this line:

Code: Select all

dtoverlay=gpio-poweroff,gpiopin=27
Save the results and close the file.
This overlay is included in the latest Raspbian? Yes! Has been for a while.
paulv wrote:... you may have a capacitor that is too far out from the 22uF that I specified. ... You can also put 2 x 47uF in parallel to get to the same value.
You have to put 2x 47µF in serial to get ~22µF, in parallel you are near to 100µF! Oops, thanks. I corrected the post.

Greetings, FM_81

Milliways
Posts: 436
Joined: Fri Apr 25, 2014 12:18 am
Location: Sydney, Australia

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Tue Dec 29, 2015 1:00 am

This is a clever idea.

It would be better to use a Boost Buck DC-DC up down Converter similar to the following. http://www.ebay.com/itm/381374427358
This has a readily accessible enable pin, not requiring and modification, and enables operation from a lower voltage e.g. a LiPo cell.

Lots of people ask about on/off switches. These make sense if running off batteries, but the pawer saving if running off mains power would take several years to recover the cost of the circuit.

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Tue Dec 29, 2015 8:06 am

Hi Miliways,

Yes you are correct, these converters can be used too.
I actually have a couple of these converters (different design, same concept), and use them in other types of supplies for my Pi's too.

I had two reasons not to use this type in this particular post. This post started out with a 12V DC supply and evolved from there. Two, most (not all!) of these Buck-Boost (actually Boost-Buck) converters are very inefficient. They work by boosting the input voltage to close to 40V and then regulate the output down to whatever you need. With an output of 5V a lot of energy is turned into heat. I have published a fix for that, by modifying the boost output to a lower value (look for automatic power supply). However that fix was for a LM2577/XL6009-LM2596 combo version, and I have not figured out the circuit on the other types I have yet.

In any case, these Boost-Buck converters can be used with this start/stop circuit too. Just make sure the the enable pin of the converter is active low, and that the output is indeed switched off completely. (Some designs using the XL6009 do not disable the output. If used in the Boost mode, they disable the boost function, but are still passing the input voltage to the output.) More importantly, the XL6009E Enable pin is active high, so most likely you cannot use converters based on that chip with this design unless you modify it with a level convertor.

However, if you get a SEPIC type Buck-Boost converter, that is not using the XL6009E, chances are that the Enable pin is active low. If you get the non-SEPIC type that uses the LM2577-LM2596 combo or the XL6009-LM2596 combo, you can use the LM2596 Enable pin, which is active low, but chances are you need to lift that pin from the PCB. I'm working on yet another Pi supply with a LIPO battery as backup and that will go into details about the SEPIC Buck-Boost converter type. I'm waiting for some parts, so keep en eye out if you are interested.

As to your last comment, at least for me, it's not so much the power savings I'm after, but more the powerdown feature while experimenting. I prefer to have my Pi powerless when I fiddle with the gpio pins, or experiment with dt.blob files, overlays etc. Besides, there is a difference between a reboot or halt/reset, compared to a real poweroff-powerup sequence that many users may not be aware of. I really need that switch!

Thanks for contributing!

PS
Since I posted this reply earlier today, I wanted to verify what I said and quickly build a circuit with a SEPIC converter I was planning to use for a new design. Unfortunately, I found out that my start-stop circuit did not work with this converter. This particular converter uses the FP5139 in a SEPIC configuration and the start-stop circuit cannot drive the enable jumper pins without making modifications. Stay tuned to this post for an update.

At this moment, I can only recommend to use Boost-Buck converter circuits that use the LM2596 in the Buck portion, because the enable pin of that chip has been proven to work as a switch in my circuit.

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Wed Dec 30, 2015 2:32 pm

The above hint of using a Boost converter got me going on a post faster than I planned, but since I was already half way there, I decided to go all the way, so here it its...

So what if you don't have, or don't want to use a 10-40V DC supply and still want the Start-Stop functionality?

If you want to use battery cells or your trusted 5V DC wall-wart?

Well, there is a solution for that as well.

The answer to that challenge is to use a Boost converter. The problem is, many of those cannot be used. There are two reasons for that. The most obvious one is that of all the Boost converters I was able to find and have information on were using chips with an active high Enable pin, and we cannot use those without modifications to our circuit which would defeat the simplicity. The other problem with many Boost converters is that they will pass on a higher-than-intended input voltage straight to the output. If you look at the circuit below, you will see that the coil across the input and output is the reason. Besides the Boost converters that are based on the LM2577-ADJ do not even have an Enable pin, so they are out anyway. There is a "modern" replacement of the LM2577 available, the XL6009, and you will find many circuits that are based on that chip. This chip has an Enable pin, but as already mentioned, it is active high, and of no use. Besides, it has the same problem with higher inputs, because the coil will act as a short, even if the chip has been disabled. That's the problem with the XL6009 in this configuration, you can disable the chip, but the input voltage still goes to the output.

OK, but this chip can also be used in a SEPIC mode. (look here for details about SEPIC convertors : https://en.wikipedia.org/wiki/Single-en ... _converter The crucial element of this converter type is that the input is isolated from the output by a capacitor or a transformer. But, because of the active high Enable pin, still not usable.

I have also a SEPIC converter that is based on a FP5139, nice board but, unfortunately, it also has an active high Enable pin, so no good in this application either.

Luckily for us, there are also many converters available that are based on a combination of an LM2577 or an XL6009 for the Boost function and an LM2596-ADJ to regulate (Buck) the output. We already know that we can use the LM2596 as a power switch. Here is an example.
Boost-Buck.jpg
Boost-Buck.jpg (14.58 KiB) Viewed 22745 times
When you are trolling for one, look for the two large chips. The one on the left can be an LM2577-ADJ or an XL6009, and the one on the right must(!) be an LM2596-ADJ.

These boards work as follows. The input voltage, typically ranging from 3..30V DC, pump that up to about 32V, and the LM2596-ADJ is used to regulate that down (set by the trimmer) to get somewhere between 1.5..30V DC at the output. As I mentioned in the post above, that's inefficient if all you want is 5V.

So if we are going to use this board, how do we do that?

Quite simple. Apart from the fact that you need to pry loose the Enable pin (pin 5) of the LM2596, you also need to bring the output voltage of the Boost circuit down. Not only for efficiency reasons, but also for safety and reliability. Let's follow the schematic and I'll explain it.
BTW, I reverse engineered the schematics from the circuits I have. There may be other ones around with different values or circuit differences, but they are probably very close
Start_Stop 3-7Va.png
Start_Stop 3-7Va.png (41.19 KiB) Viewed 22360 times
[EDIT I changed this circuit from the original post a bit to avoid high voltage levels at the Pi GPIO pins. Adding two resistors is all it took. The text has been edited to reflect this. The start-stop circuit is now identical to the one in the first post]
XL6009-LM2596-mod.png
XL6009-LM2596-mod.png (24.19 KiB) Viewed 22598 times
The output of the XL6009, or the LM2577-ADJ can both be reduced by adding one resistor (RX) such that it is parallel to the 20K adjustment resistor. Both chips use exactly the same output setting. Using a 5K6 resistor in parallel, you get somewhere between 7.8 to 7.9V. It is relatively easy to solder this resistor directly on the leg of the chip on the one hand and to the solder pad of the large electrolyte on the other. You then need to solder a thin wire to this point and feed that to the resistor divider to create a 3V3 voltage level. This gives the Buck stage of the LM2596 enough headroom to produce a stable 5V, and we can use the same reduced and stable 3V3 voltage to power our magical start-stop circuit.

Look for a detailed description on how the start-stop circuit works in the first post.

So there you have it. The only important limitation for this circuit is that you should not feed more than 7V at the input, because, if you paid attention you already know, that higher voltage is passed on to the next stage AND your Pi! Yes you can add the same Zener based circuit of the design above, but let's face it. If you have a higher input voltage, why not use the circuit described in the first post?

Enjoy!
Last edited by paulv on Fri Jan 08, 2016 8:40 pm, edited 12 times in total.

deejayspinz
Posts: 5
Joined: Wed Dec 30, 2015 12:51 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Wed Dec 30, 2015 5:04 pm

OP thx for posting this. I am following along with this and have the same power supply in the original post. It also has the LM2596s chip and I found that even after separating the 5th pin from the board (far right when looking at the chip), the board still supplies output power. I double checked the datasheet and am certain I have the right pin (the 2596 and 2596s are the same). I am also sure that there are no bridges - the shrink wrap goes right up to the chip.

Anyone else experience this? I am wondering if it's a knock-off and the chip simply has pin 5 pulled down internally or something? Sorry for the poor image quality.

Image

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Wed Dec 30, 2015 7:04 pm

Deejayspinz, I mislead you in the original post. Sorry!
The LM2596 seems to have an internal pull-down, so you need to connect the pin to a voltage that is above 1.3V to disable the output. Just leaving it float is not recommended.
I fixed the text, thanks for bringing this up.
Last edited by paulv on Thu Dec 31, 2015 5:37 am, edited 1 time in total.

deejayspinz
Posts: 5
Joined: Wed Dec 30, 2015 12:51 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Wed Dec 30, 2015 8:14 pm

@paulv.. Perfect, thx.. I thought I had a few duds here (tested it on 2).. I'm wiring things up on a breadboard and will report back once done testing. Thanks for putting this together.. I'm also working on a modified Raspberry Pi case for 3D printing. It will hold the Pi 2, the buck converter, some room for the circuit + a barrel jack...

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Thu Dec 31, 2015 8:33 am

Thank you paulv. I bought a converter like this for my Atari Lynx. It runs on batteries and has a power on/off button. My hope is to get a little longer play time by allowing the Lynx to run even after batteries are below 5V. This is an interesting modification. I may do the same for my DMX light control system and run from batteries just to reduce the number of cables.

deejayspinz
Posts: 5
Joined: Wed Dec 30, 2015 12:51 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Thu Dec 31, 2015 1:04 pm

So, I put this circuit together and and having some strange results. When it's all hooked up, I have to hold the pushbutton for 3 or so seconds to get the Pi to start up. Once started pushing the switch (or holding it for several seconds) does not shut the Pi down.

Here are some measurements:
Pushbutton -0V (button open), 5.1V (button closed)
- Pin 27 1.9V (button open), 0V (button closed)
- Pin 17 2.2V (button open), 1.9V (button closed)

I am fairly certain that the circuit is correct. However, I did do one change. I did not have D2 (3v3 diode) so instead used a 3.3v LED with the 1K5 resistor. Since this lights up when powered, I did not bother with R1 and the LED as I figured they are redundant now. Is it possible this LED is causing an issue? What should the voltages at Pin 17 & 27 be?

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Thu Dec 31, 2015 2:49 pm

deejjayspinz,

Eliminating R1 and the LED from my proposed circuit is not the or a problem.

What will cause an issue is the charging voltage for the capacitor with your modification. Pressing the button for 3 seconds is an indication for that.
A very quick, momentary push should start the charging process and that should take about 4 seconds. The circuit looks simple, but because of the parts in play, and the "tricks" I use, you have to use the same parts, or experiment and find the root cause.

This is what you need to do to start fixing the boot issue. Do not connect the Pi to the converter. Use a DMM (you seem to have one) and hook it up to the output of the converter. Or connect another LED and a current limiting resistor to the output. Get ready to count seconds, and apply power to the converter through the wall-wart. There should be no power at the output. If there is, your connection to the Enable pin is not working. Check that again. If there is no power, push the button for a moment and start to count seconds. The converter should now have a voltage at the output, count until the voltage drops to zero. It should be around 4 seconds, a little more is no problem, less than 3.5 is.

If this does not work as advertised, you have a charging circuit that does not work and this is most likely because you used something else for the 3V3 Zener diode I use in my diagram. So first you need to find a better replacement for the Zener. Assuming you apply 12V DC from a wall-wart, you can make a resistor divider that results in about 3.3V at the junction. You can use the 1K5 resistor from the 12V down, and connect that to a 620 Ohm resistor to ground. Measure the voltage at the junction of the two, somewhere between 4 and 3.5V is OK. If not, or if you don't have these resistors, play with the DMM and your stash of resistors until you do. Use this for assistance if you need it : http://www.raltron.com/cust/tools/voltage_divider.asp The total value of the two resistors should not get above 50K, otherwise you'll introduce other problems. Try the keep the total current through the two resisters somewhere between 0.5 and 2mA if you can. With 12V, 1mA results in a total value of 12K. A split of an 8K2 (top) and a 3K3 resistor should result in about 3V3, which is perfect.

If the voltage at the junction is OK, connect the top of the charging resistor to the junction. Put your DMM across the capacitor, and apply power. The voltage should creep up slowly. Press the button. The voltage should go to 0V immediately.

If OK, you can now connect the top of the capacitor with the 10K resistor to the Enable pin of the LM2596. (just follow the diagram) Apply power again, because everything was powerless, the Enable pin is already low, and C1 will already charge, without you pressing the button. Press the button again momentarily to remove the voltage across C1 and verify that you still have a voltage (5V) at the output of the converter. Measure how many seconds the converter shows an output. If it is less than 4 seconds, increase the value of the charging resistor, put a 10K or 22K resistor in series and/or add some capacitance (put a 4.7 or 10uF cap parallel to C1), repeat until you have at least around a 5 second window of power.

If you got this far, and you are certain that the software modifications you made are correct, I'm pretty sure everything will work. If the stop button press does not work, follow each step in my description carefully and double check everything. It's not complicated, just make sure you don't make mistakes in typing etc. If you are new at Python, make sure the indentations are in the code, they are important! However, I'm pretty certain that your problem will have been solved by getting the proper voltages in place.

With this modification, you are no longer able to use different wall-warts with different voltages. You are stuck to a 12V DC wall-wart, or you need to redesign the voltage divider again.

Let me know how it it goes.
Success!
Last edited by paulv on Fri Jan 01, 2016 7:47 pm, edited 3 times in total.

deejayspinz
Posts: 5
Joined: Wed Dec 30, 2015 12:51 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Thu Dec 31, 2015 3:17 pm

@paulv, thx for the detailed instructions.. I think you right about the LED replacement for the 3v3 diode being the issue. When I do the check as you described below, pushing the switch, then releasing it, I see the voltage drop quickly from 5.1v to just near 0 in about 3 (seconds. It's below 0.5V at 3 seconds for sure). I guess the LED is stealing it away as the cap discharges? I'm going to head over to my local electronics store to see if I can find a 3v3 diode and add the power LED separately (like the circuit). Otherwise I'll try the voltage divider. Any suggestions on what type of diode? Or any Schotty 3v3 diode will do?

Check that again. If there is no power, push the button for a moment and start to count seconds. The converter should now have a voltage at the output, count until the voltage drops to zero. It should be around 4 seconds, a little more is no problem, less than 3.5 is.

deejayspinz
Posts: 5
Joined: Wed Dec 30, 2015 12:51 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Thu Dec 31, 2015 8:34 pm

Shaazzaaaam! It works! Putting the 3v3 diode where it was supposed to be did the trick. Thanks for all your help and excellent instructions @paulv.. I also re-ran the test sequence you described above and it the voltage dropped from 5v to 0v in about 5 seconds.

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Sat Jan 02, 2016 5:42 am

I have added a small additional feature to the Python program that listens to the button press to start a poweroff.

With the original program, any press of the button would result in a shutdown of the Pi.
Depending on how easy it is to accidentally press the button, I added a small change. With this, you need to deliberately press the button for 3 seconds before the power-down process will be started. Any button press shorter than 3 seconds will have no effect.

Here is the changed program:

Code: Select all

#!/usr/bin/env python2.7
#-------------------------------------------------------------------------------
# Name:         Shutdown Daemon
#
# Purpose:      This program gets activated at the end of the boot process by
#               cron. (@ reboot sudo python /home/pi/shutdown_daemon.py)
#               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 then 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 18-12-2015
# Copyright:   (c) Paul 2015
# Licence:     <your licence>
#-------------------------------------------------------------------------------

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

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 and
            # cannot be used by any other program

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

def main():

    while True:
        # set an interrupt on a falling edge and wait for it to happen
        GPIO.wait_for_edge(INT, GPIO.FALLING)
        # we got here because the button was pressed.
        # wait for 3 seconds to see if this was deliberate
        sleep(3)
        # check the button level again
        if GPIO.input(INT) == 0:
            # still pressed, is a serious request, shutdown Pi
            subprocess.call(['poweroff'], shell=True, \
                stdout=subprocess.PIPE, stderr=subprocess.PIPE)


if __name__ == '__main__':
    main()
Enjoy!

belese
Posts: 3
Joined: Mon Jul 11, 2016 7:20 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Jul 25, 2016 3:15 pm

Hi,

i've done this schema, (but i use a voltage divider instead of zener)
but as soon as the pi halt, it reboot automaticaly,
i've check all schema, and it seems correct to my side, but ...
after a short press, i've enable set to low for 5-6 secondes, if i plug to the pi, the dtoverlay seems ok, as power remain,
if i unplug the pi and replug, there no start(as it should), i need to push the button to restart and if i remove the kill_line from pi, the pi power is cut directly.

any idea what could be wrong?

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Jul 25, 2016 7:35 pm

Hi belese,

Using a voltage divider instead of a zener is fine. (look at the next diagram in the post where I did the same) From this, I assume that you use the first diagram of this post.

The fact that the RPi reboots automatically after the powerdown has been requested is not a function of my hardware or software, but of the OS. As an example, if you use systemd to start/stop processes, there is a good chance that the watchdog has been enabled, and this will force a reboot after typically 10 seconds. Normally you'll see a message towards the end of the powerdown/shutdown sequence to this effect.

In any case, the power should have been cut by the poweroff overlay and the hardware, so the reboot should not happen, even if systemd had activated the watchdog. You can try that without powering your RPi with the circuit. Just use something, like a voltmeter or an LED with a small resistor on the output of the DC-DC convertor, and use a small piece of wire to toggle the poweroff pin by hand. The power should go off after you apply power, and not use the poweroff signal. If you do, the power should stay on. If you then release the poweroff pin, the power should be cut immediately. Make sure this works before you start adding the RPi.

Because of the much changed boot process with Jessie since I wrote this post, it will take about 4.5 seconds or more before the poweroff overlay is loaded to continue the power on cycle. You may have to get a larger size charge capacitor (raise it from 47 to 68 or 100uF) Too large a value does not hurt, not enough does.

I do not really understand what you are trying to tell us in the other information. Assuming English is not your mother tongue, write it in your own language and I can try a translation program to figure out what you mean.

Unfortunately, I'm not in a position at the moment to run some tests with the hardware and the latest kernel version due to other activities.

The good news is, the hardware is very simple, and the setup is too. You should be able to make it work. If not I hope I can help.

Success!

belese
Posts: 3
Joined: Mon Jul 11, 2016 7:20 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Tue Jul 26, 2016 1:29 pm

Thanks for your fast answer,
it was of course an error from me :roll:

i didn'nt initialise the GPIO-17 as an input with a pull up during my test, that was the problem,
now everything is fine!

Thanks and really great explanation for a electonic newbie like me, i even uderstand what i've done :D

Belese

maranatha
Posts: 5
Joined: Tue Mar 08, 2016 8:02 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Sep 05, 2016 8:51 am

Hi

I succeeded in turning ON the RPi. RPi can do some task and commands itself to shutdown. How can I read the status of the RPi? Which GPIO pins of the RPi I should use to read the status? Do I need to use any other electrical component for safety?

I would appreciate any kind of comments relevant to this :idea: .

BR
Last edited by maranatha on Fri Sep 30, 2016 12:41 pm, edited 2 times in total.

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Sep 05, 2016 10:12 am

maranata,

I suggest you carefully read my post because it has the answer to your question.
Success!

maranatha
Posts: 5
Joined: Tue Mar 08, 2016 8:02 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Sep 05, 2016 1:29 pm

Hi paulv

Thanks for the reply. I actually followed the post. But it has been updated several times for different scenarios and it just got mixed up in my head. That is why I wrote my specific use-case in few paragraphs.

I was quite new to the DC-DC buck converter and all the hacking along with it. I was afraid I will make mistakes and destroy the RPi. I am using a 5V/2.1A battery source to power the RPi.

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Tue Sep 06, 2016 7:05 am

Just use the part that details the activation of the powerdown overlay to get a GPIO pin that signals the end of the halt/shutdown/powerdown sequence.

maranatha
Posts: 5
Joined: Tue Mar 08, 2016 8:02 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Fri Sep 09, 2016 11:54 am

Hi paulv

Thanks for the advice.
I just included the code to signal the shutdown of the pi in the /boot/config.txt file. It seems to work, in a sense that I read ~3.3v at GPIO27. The problem is the shutdown processes freezes and shows a kernel panic message. Do you have any idea what might be the cause :roll: ?

Thanks in advance for the help :)

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

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Sat Sep 10, 2016 7:33 am

Hi maranatha,

What you are seeing is the "normal" end of the shutdown (power down) process. In simple terms, the main process thread (0) gets terminated, and that "seems" to cause a kernel panic. Study the last statements on the console. It is OK. You can search this Forum for more information or use Google.

After you see this statement, you can take the power from the Pi or reset it with the P6 reset pin.

Enjoy!

Paul

maranatha
Posts: 5
Joined: Tue Mar 08, 2016 8:02 pm

Re: HOW-TO Start/Stop/Powerdown Pi with 1 Button

Mon Sep 12, 2016 11:49 am

Dear Paul

Thank you very much! I feel so relieved right now.

All the best!

Return to “Beginners”