This has to have been solved before, but I can't find it via google or searching on this board.
I want to run a pi off a LiPo battery, but when the pi is off, I don't want it draining the battery. I'm looking for a configuration that allows for the following:
1) when the pi is off, no power is drawn from the battery
2) pressing a momentary switch will power on the pi
3) pressing the same momentary switch will trigger a python (or other script) to issue the sudo shutdown -h now command to power down the pi
4) once the pi is powered down, it stops drawing power from the battery
Is this even possible? Does anyone know of a solution?
I think one of the problems is that everybody has slightly different expectations of what an on/off switch should do.
I have been working on making something like this for a project I am busy with, where one of the requirements I have is to be able to just press a button to turn the Pi on or off.
I have got to a design which I soldered up on veroboard this week, and which is working (it isn't perfect).
This is what the finished product looks like:
You can find some more details on my blog at http://www.rehmet.alt.za/blog
The latest schematic on my blog is not the final circuit. I have changed a couple of resistors, and I still need to update the schematic and post it.
The idea of how it works is that when you push the switch it does 2 things, firstly: charging a capacitor, which gradually discharges into a Schmitt trigger inverter and which also switches a GPIO input to high. The Schmitt trigger inverter's output is held low long enough for the P-channel MOSFET to power the Pi until the serial console becomes active. I also use the serial console TX line to charge the capacitor (via one of the BJTs), so as long as the Pi is booted, power stays on.
I also have an interrupt handler, which triggers on the falling edge of the GPIO line which is switched by the push-button. When I get an interrupt, I send a signal to systemd to shut down and power off the Pi.
When the serial console becomes quiescent at the end of a power off, the output of the inverter into the MOSFET goes high, and power it cut.
After power off, the current drain of the whole setup drops to less than 0.1mA, which I am happy with. Hopefully that is low enough to satisfy your needs. I think a Pi Zero when powered off draws in the region of 22mA.