Home and Links
 Your PC and Security
 Server NAS
 DVD making
 Raspberry Pi
 PIC projects
 Other projects
 Next >>

Building a Power Bank UPS for the Raspberry Pi


Notes on using a Power Bank

You can now (mid 2016) purchase (very) cheap 'Power Bank' rechargeable battery packs which contain of one or more Lithium Polymer batteries (type 18650) with at least 700mA USB output.

Whilst these are designed to 'recharge' your smart phone / tablet, all you need to do is add a mains USB power-block (and, perhaps, a few diodes = see note 1). A basic 1200mAh UPS can now be built for less than £2.50 (£1 PowerBank, 50p diodes etc., £1 cable) !

The basic UPS depends on diodes for 'switching' between 'main' and PowerBank, which is fine when you can adjust the 'main' voltage - as you can when using PoE. In other circumstances you may need to use the MOSFET based 'switch over' circuit (shown below = the MOSFET is used because the USB 'mains charger' might deliver lower voltage than the Power Bank - expand the "(+) MOSFET switch" link below for an explanation of how it works)

1) A 'high end' PowerBank that supports 'pass-through charging' or as here) a 'Duo-Charge function' has an internal 'switch over' circuit, so you plug your 5v power block into the Power Bank input and power the Pi from the Power Bank output.
2) I have seen 'DIY' LiPO battery based UPS designs. Most use overpriced 'model aircraft' LiPO batteries and expect you to build your own 'charging' circuit. Don't be tempted - a standard small 'Power Bank' (with it's internal recharging circuit) will be come in at 1/10th the cost (even if you have to build your own MOSFET switch over circuit)
3) Power Bank capacity claims (especially on eBay) are not what they seem. The mAh specified is for the (3.2v) LiPO BATTERY INSIDE THE UNIT = to 'convert' that to USB power voltage (5.0v) you multiply by (3.2/5.0 =) 0.64. Further, every PowerBank must 'cut-off' when it gets to about 10% capacity remaining (as the internal charger can't recharge a completely flat LiPo cell).
So, when your single cell '2200mAh' unit arrives, you will discover it's actually 'rated' at no more than 1200mAh (and the dual cell '5,200 mAh' unit is really 2400mAh) :-) ).
Of course even a 1200mAh unit will keep the average Pi running for more than an hour (one user reports 3 Hrs out of a "5200 mAh" (2400mAh) unit, so you can expect over 90 mins for the 1200mAh, even with the camera connected to the Pi) = see below for Pi power usage.
Note, ALL the Power Banks I have ever found in UK retail shops turn out to be 'Made in China' = so don't waste your money on some 'branded' item (unless you like paying for very expensive sticky labels :-) )

A basic Power Bank UPS for the Pi Zero + camera can be assembled for less than £2. To see how I did this, click below :-

(+) diode switch

NOTE. When using a USB mains power-block that can't exceed the Power Bank voltage, the MOSFET switch (shown above) is needed. For an explanation of how this works, see below :-

(-) MOSFET switch

How the MOSFET switch works
A simple 'diode switch' passes whichever of 'mains' or PowerBank voltage is the higher. This is fine when we can guarantee (or set) the 'mains' voltage (when present) to be higher than the PowerBank voltage.

However, a USB mains power block voltage is fixed and (whilst adequate) might well be less than the PowerBank voltage. This would mean Vmains is applied to the PowerBank at the same time as VpwrBank is passing power to the Pi - and since a low cost PowerBank does not support 'output' at the same time as 'input', at the very least you can expect it to 'glitch off'.

We thus have to 'detect' Vmains and immediately 'switch off' VpwrBank.

This makes for quite a complex circuit, because it's not just a matter of 'switching' whenever Vmains is present - instead we have to detect when Vmains is high enough to actually run the Pi.

To control the PowerBank output, a (p-channel) MOSFET can be used. To enable the PowerBank the MOSFET Gate has to be driven toward 0v and to disable it, toward 5v. So if we wire Vmains input to the Gate, when the mains is absent (near 0v) the PowerBank will be switched in, and when Vmains is present (near 5v) the PowerBank will be switched out.

Whilst this 'switches to Vmains' when the USB power-block is plugged in, there is a problem when the USB power block is unplugged.

The MOSFET only 'switches on' when it's Gate voltage falls below the Gate-Source 'Vthreshold' voltage. With the Vthreshold of a typical 1A p-channel MOSFET - the NDS356AP - of about 1.6v, this means the PowerBank will only 'turn on' when Vmains drops below VpwrBank-1.6v i.e about 3.4v. Since Vpi will be Vmains-.3, by the time the MOSFET turns on, Vpi will have reached about 3.1v .... and the Pi will have crashed long ago.

So it will be necessary to 'detect' when Vmains is 'too low' to power the Pi, and use that 'detect' to switch between Vmains and VpwrBank. The simplest way to 'detect' voltages is to use a Zener diode. If the Pi needs at least 4.75v and the (dual) Schottky diode voltage drop is .3v, then the 'detect too low' voltage will be 4.75 + 0.3 = 5.05v, so using a 5.1v Zener is 'safe'. 

To drive the MOSFET we can't just wire Vmains+Zener to the Gete. Instead an OpAmp is used to do the compare and amplify the difference (so the MOSFET Gate is switched rapidly from 0v to 5v). Since the OpAmp is powered from Vmains, we need to tie the MOSFET Gate to Gnd (so it turns on when the OpAmp is un-powered)

Photo: ../Raspberry_Pi/photos/PwrBank-MOSFET-switchover-cct.gif
If the USB mains power block is unplugged when switch SW-1 is closed, the OpAmp will be un-powered and it's output 'hi-impedance', so R4 will keep the MOSFET Gate close to GND. This means the MOSFET will pass VpwrBank to Vpi.

When the USB mains power block is plugged in (irrespective of the SW-1 state) the OpAmp will power up. So long as Vmains is less than 5.1v, the Zener will not conduct. This means the '+' input of the OpAmp will be held to Gnd via R3. The '-' input, however, will rise to a voltage above Gnd set by the voltage divider formed by R1 and R2 (the actual voltage will be approx. Vmains/1000, however even this small voltage should be enough for the OpAmp to drive it's output to Gnd, and thus keep the MOSFET 'on').

When Vmains exceeds 5.1v, the Zener starts to conduct and the '+' input rises to Vmains-5.1. When the '+' input (Vmains-5.1) rises above the '-' input (Vmains/1000) = which will happen when Vmains exceeds about 5.11v = the OpAmp output will swing from Gnd to Vmains and the MOSFET will turn off.

With the MOSFET 'off', Vpi will be driven to Vmains-0.3 i.e. at least 5.11-0.3 = 4.81v. So long as Vmains stays above 5.1v Vpi will follow Vmains-0.3.

If the mains power block is unplugged, when Vmains falls below 5.11v the OpAmp output will swing from Vmains to Gnd and the MOSFET will turn on again. The Pi will remain powered by Vmains or the PowerBank until SW-1 is opened.

1) You will, of coure, require an OpAmp that works down to at least 3v (eg. LM358) or the MCP6001 (which operates down to 1.8v)
2) The MOSFET D-S 'body diode' has a Vf of about 2v, so can be ignored so long as Vpi is never more than 2v above PowerBank.

This note last modified: 9th Oct 2016 06:42.


Interestingly, you can now find some cheap 'solar' equipped units (such as this 30000mAh-Solar-Charger-Battery-Power-Bank), however with the solar cells delivering a typical 1.5 Watts in bright sunlight with a Pi system consuming 5 Watts you will get 10 mins of use per half hour of solar charging - although it MIGHT just be up to running a Pi Zero + camera (1.3 watts))

Some Power Banks come with silly 'power buttons'. This can be an indication that they won't 'turn on' untill you press the button, so I generally avoid these types, however this RS branded unit which does have a 'power button' has been tested and works just fine. Another one known to work as a UPS is the LogiLink PA0064 Mobile Power Bank 2200mAh.

It's possible you may find a PowerBank that has to be 'switched on' or refuses to keep running at low current, however even the unit I found in the £1 shop (ITP Product Code H-28662) automatically turned on when I plugged in a 'load' (a USB memory stick) and kept 'running' even though the load current was < 1mA, only turning off a few seconds after the load was removed

Shutting down the Pi

The Pi needs to shut itself down before power is totally lost, ESPECIALLY if it's likely to be 'writing' to the SDHC card (as it will be in the case of a CCTV application when Ethernet comms is lost). For more details see :-

(+) Auto shut down

Next page :- SuperCap Uninterruptable Power Supply - (UPS)