Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

battery powered pi, implement real power button?

Tue Jan 01, 2019 5:00 pm

Not sure whether I'm in the right board, feel free to move it whereever it belongs.

I already did a battery powered project with an arduino, that is working great with a mosfet giving the power supply to the arduino board. A pushbutton momentarily gives positive battery voltage to the gate, powering up the arduino. At the moment the arduino starts, it is holding the gate voltage by an output pin by itself. The mechanical pushbutton is also read by another input pin, to enable a software shutdown by releasing the output pin. Also the software releases the pin, if the project does not recognize any user activity for some time. I took the idea and the dimensioning etc from here.

Somehow with the whole boot and shutdown procedure of raspberry it's a different picture. I found out, that the lower GPIO pins (I checked GPIO4) are at high state almost immediately after USB is connected. I could use that to keep the mosfet gate positive and keep the raspberry powered if the user releases the power push button.

Somehow the pin even stays in high state after the raspberry pi has been shut down from console. I was quite surprised by that, hoped there will be no further voltage on the gpio after shutdown.

So how could I release the mosfet gate and steal the voltage from the raspberry only AFTER it is safe to do so, by a gpio pin? From this "power-LED" tutorial I heard that the serial pin will be low after save shutdown, but I'm not keen on messing around sending serial signals to my power mosfet which is then enpowering the raspberry pi itself. Especially as it seems to change state quite often in the boot process.

The same author has also a power button tutorial, but this only triggers a shutdown from software, I want a hard cut of my power supply to prevent deep discharge of the batteries. Also I have some electronics in front of the raspberry pi (voltage regulation and similar) that will consume energy, if I do not also disconnect them from the battery. A normal switch to cut the 5V from the raspberry pi is no option, because I want a safe shutdown, no sudden death.

I heard there is a way to overwrite the default behavior of pin state at bootup, is there also a way to specifiy the behavior AFTER shutdown?

Posts: 349
Joined: Fri Jan 01, 2016 10:27 pm

Re: battery powered pi, implement real power button?

Tue Jan 01, 2019 10:53 pm

Here’s what I did for my Compute Module design:

I have a Supercap backup power supply that allows graceful shutdown. In your case, this would be your batteries.
I use a separate micro controller to monitor the state of the supercap charge. Once they are fully charged, I enable my 3.3v and 5v switching power supplies to power the CM. In your case, this might just be turning on your mosfet. When the power fails, the micro controller starts to toggle an input on the CM, and a monitor daemon on the CM looks for that toggle, and then issues a “sudo shutdown -h now” command. Once the shutdown is done, the activity light stay on (as opposed to flashing with disc activity). The micro controller starts to monitor the Activity LED, once a shutdown is requested, and when the LED stays on for 2 full seconds, it disables the 3.3v and 5v power supplies, shutting the CM down.

This has worked very well for me.

User avatar
Posts: 4665
Joined: Tue Jun 20, 2017 12:16 am
Location: Australia

Re: battery powered pi, implement real power button?

Wed Jan 02, 2019 1:10 am

Is it not possible to select logout > shut down from the menu?
If you use this option, you can then just pull the plug or switch an ordinary switch.

Is there something preventing physical access to the Pi?

Rose tinted glasses are difficult to see through.

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Thu Jan 03, 2019 2:24 am

My project is a music jukebox for a small kid. The pi sits very sheltered in a wooden case and there is no GUI to call shutdowns from. Only software that plays music depending on GPIO button pushs...

@rzusman: Wow, that sounds like a little bit more than minimum necessary parts. I have a similar project with a 230V power supply and I also want to use caps to allow safe shutdown, but I would not consider another microcontroller for that...

In the meantime I solved most of my doubts for the battery driven project. Got me some more parts from the local electronic dealer. This is how I plan the stuff right now. Most parts are already tested, but the thing was not tried as a whole:

This is what is happening:
  1. At the very right there is a terminal for charging the battery.
  2. Next to the right is a pack of 2 LiIon cells giving around 7.4V with nearly 3Ah.
  3. The batteries sit behind a fuse, as the charging jacks can be shortened and shortened LiIons can lead to severe fire...
  4. Fuse can also be used as a sort of real power switch (after opening the housing), e.g when device will be left idle for months, or needs repair...
  5. As long as the push button is not pressed, the two MOSFETs are not activated and the whole thing is using more or less zero power.
  6. As the pushbutton is pressed, the upper LD1117V33 passes 3.3V to the left MOSFET gate (I took care to find one that is fully open at 3V)
  7. The MOSFET is now allowing the 5V to reach the raspberry pi and it is booting. At the very moment it starts to boot, GPIO5 takes care that the gate is still given 3.3V even when the start button is released again.
  8. The second LD1117V33 and the diode in front of the first LD1117V33 are required to allow to check later button presses with GPIO4 it goes high with every button press, but the 3.3V of GPIO5 can not pull it high due to the diode.
  9. The two diodes in the power supply in front of the 5V regulator are taking care that low voltage of the LiIon cells leads to 4.6V (or less) at the raspberry. This was calculated with the exact spec of all the parts and will need adjustment when using other parts with other dropout voltages. The idea is, that a python script polls the throttled bit every now and then and if the pi has activated a low voltage flag, the system will shutdown. This will prevent deep discharge of the cells, at least more or less, as the device will shutdown with each subsequent try to boot it again (as long as the voltage of the cells is below 3.2 each).
  10. The second MOSFET at the right is giving power to the small PA, which is driving two 4 ohm speakers. When using an amp with a enable or mute input, the MOSFET wouldn't be required, but my small PA platine only has a power input.
In addition to the scetch above, 9 more GPIO buttons are connected to control the audio playing.
smallAmpCircuitDontUse.png (148.25 KiB) Viewed 5337 times
Last edited by mangiari on Tue Jan 08, 2019 4:25 pm, edited 1 time in total.

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Fri Jan 04, 2019 12:48 am

Puuh, that 3.3V / 5V stuff with the raspberry pi is really driving me nuts. The 3.3V voltage regulator with it's ground connected to the somehow floating ground of the pi's power supply does all kind of funky things.

I first thought my MOSFET might be burnt or something, but in the end when I removed the 3.3V regulator, the MOSFET worked fine.

Now I replaced the regulator by another bunch of diodes, but I ran into another problem:

The GPIO5 which I asked in the boot/config.txt to be pulled low after shutdown, is doing this indeed. But at the moment the MOSFET disconnects the ground from the raspberry, also this low pulling is gone and the GPIO5 is making my MOSFET connect the pi again. Seems like that lower 8 GPIOs are really hard wired connected to 3.3V, when no active pulldown is applied.

Weird stuff, don't know how to solve that for now. Maybe I have to search for another pin, that is reliable low, when the pi is disconnected from the ground.

Posts: 349
Joined: Fri Jan 01, 2016 10:27 pm

Re: battery powered pi, implement real power button?

Fri Jan 04, 2019 12:52 am

Don’t dismiss using a separate micro controller as a supervisor so fast...

You can get a tiny micro for a buck or so, and it can handle all the logic.

User avatar
Posts: 6616
Joined: Wed Aug 28, 2013 3:31 am

Re: battery powered pi, implement real power button?

Fri Jan 04, 2019 3:28 am

You can get a tiny micro for a buck or so, and it can handle all the logic.
And if it has i2c it can pretend to be a real time clock ;)
Most of the newer micros have 32khz xtal options.
The Pi can program it to alarm wake the Pi.
I'm dancing on Rainbows.
Raspberries are not Apples or Oranges

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Sat Jan 05, 2019 11:14 pm

Yes well, it sounds ok, but then I have no clue about that simple microcontrollers. I did some arduino projects, but you don't get them for a bug. And I don't have that much space in my device.

I can't believe that there is no simple way with discrete implementation. After having trouble with the circuit I now found the real problem: The GPIO pins share the potential of the 5V input, as long as there is no ground connected. Even switching to higher GPIO numbers did not make any difference. They all seem to be on the same potential as the 5V rail, when no ground is connected. This cancels the idea of driving a N-channel MOSFET with the GPIO pin.

Next I tried aroung with power switching bipolar transistors, but they have very low DC gain, meaning I have to waste a lot of energy on the switching, to make sure there will be enough current allowed without voltage drops.

Next try will be to drive my MOSFET with a small bipolar transistor. And I already spend so much time, I could have payed someone to build the project :oops: :?

Posts: 349
Joined: Fri Jan 01, 2016 10:27 pm

Re: battery powered pi, implement real power button?

Sun Jan 06, 2019 12:11 am

You need to re-think your circuit.
Having the 5v regulator powered all the time is going to kill your battery. That regulator has something like 5mA or quiescent current.

Also, you need to be careful about low-side switching. Once you pull the ground, your device naturally floats to Vcc, since the transistors inside don’t have infinite resistance.

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Sun Jan 06, 2019 2:17 pm

Yes thanks for the tips. The 5V regulator is needed, because the 7.2V of the battery pack can become more than 8V when charging and less than 6.5V when battery gets low. This is far too much spread for any other solution. I can live with the raspi between 4.6 and 5.6V but not more.
But as you can see, the ground of the 5V regulator shared the ground of the raspi and it is not able to draw current when the MOSFET is inactive. But it has another problem: The voltage at the output rises to the input voltage, when ground is floating. I would have hoped that it is more like decreasing to zero, but than how should it know what zero is ;)

On the other hand I don't believe a high voltage at the positive rail of the raspi is a problem, otherwise mine would have died already long ago.

I think I found a solution that might work now. I checked all GPIO pins with the multimeter when raspi is powered off (no ground connected, but some 5V at the positive rail) and all GPIO pins share the same potential like the 5V pin does (which I use to inject the power supply). Even the ground pins go to that 5V. That knowing I understood my mistake: I should use the difference between GPIO pin and internal ground of the raspi, rather than the difference between the GIPO pin and some external ground. Then a bipolar transistor should not let any relevant current flowing.

I will test this tonight and when I finally have a working circuit, I will update the thread of course.

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Tue Jan 08, 2019 4:29 pm

So all tries with MOSFET and bipolar transistors ended up in the same dead end...
I updated the circuit image to avoid people trying to use it (or parts of it).

In the end I found a pretty simple and elegant way of working around that ground and high pin problems: Put the raspberry in a nutshell and only communicate outside this shell using optocouplers. This makes all the 3,3V voltage regulators unnecessary and solves all other problems. For the raspberry pi it's just drying LEDs as done in every beginner tutorial, for the outside circuit it's just transistors, (emitter - collector paths) with no potential trouble any more.

I will have to test my new circuit on the bread board and when it is stable, I post an updated image of the much less complicated circuit.

Posts: 40
Joined: Sun Mar 11, 2018 4:45 pm

Re: battery powered pi, implement real power button?

Sun Jan 20, 2019 6:24 am

So, now finally I got a working circuit:
smallAmpCircuit.png (97.08 KiB) Viewed 5111 times
I had to take optocouplers, to get rid of the 3.3V vs. 5V problems and also the common ground trouble with the MOSFET, described in the comments above.

This is how it works:
In the top left corner is the L4940V5, a low drop 5V regulator. It will assure the raspberry pi never sees more than 5V, no matter what voltage comes from the batteries, even when they are charged, what can be easily 8V. It has a very low voltage drop, so together with the 2 diodes I managed to achieve a low voltage bit in the raspberry pi, at the battery level I do not want to discharge the LiIon-cells further. Software will make sure the raspberry is shutting down, whenever low voltage is occurring.

The 5V go directly to the power 5V of the raspberry, the ground connection of the raspberry pi is disconnected from battery ground by the left MOSFET M1. As soon as the power switch is triggered, M1 gets conductive and the raspberry pi will start. C1 will keep the gate of M1 active for some seconds, to assure that even with a very short press of the button, the raspberry pi gets enough time to keep the M1 gate activated by GPIO19 via the optocoupler O1. A GPIO lower to 8, which will activate as soon as the raspberry pi has power, was not possible, because they switch state before shutdown.

As long as GPIO19 is high M1 is allowing the raspberry pi to have power. M2 is activated whenever there is playback in a similar way.

The second optocoupler O2 is optional. It checks the button state, so that the same pushbutton can be used to initiate a shutdown from software. This is what the diode D3 is used for, to assure that GPIO6 is only getting a high state when the button is actually pushed, not all the time because of GPIO19.

R1 needs to be choosen so that O1 is allowing enough current through the phototransistor.
Optocouplers O1 and O2 need to be choosen so that 3.3V and the little current GPIO can provide, is enough to activate the phototransistor.
R2 is not that relevant, could be anything between 3K and 100K probably
R3 needs to make sure that O2 is activated enough for raspberry pi to detect a high state, could be larger probably
R4 keeps the gate of M1 active and is chosen that large to avoid to much current drawn from the battery
R5 is the pulldown resistor for M1 and is that large for the same reason
The MOSFETs have to be chosen that they allow enough current at 3.3V gate voltage. It's not easy to find cheap ones that do this well.

With my multimeter, that states that is can detect down to 0.01mA I could not detect any current going into the circuit, after the raspberry pi was shutdown from software.

Posts: 121
Joined: Tue Aug 01, 2017 6:51 am
Location: Charleston, SC, USA

Re: battery powered pi, implement real power button?

Wed Aug 14, 2019 6:01 pm

Hi Mangiari

Sorry for not responding sooner, I've not received notifications for some reason..

Have a look at YouTube suppiups for a UPS that I've designed and made. You can also contact and I'll get you setup.


Return to “HATs and other add-ons”