Uninterruptible Supplies

Need a power supply with battery backup that shuts off your PI or similar gracefully when the battery gets low and starts up when the battery  recovers? I’m sure most of us who play with the likes of Raspberry Pis and other SBC boards as well as other devices, have looked at uninterruptible supplies at one point or another – I often use the little battery powered chargers you get for phones – and until recently I thought that was a universal solution – having powered my Pi in Spain for many months now on one of these and we get awful power cuts there – no problem.

08 May 2017: This is an update to the original article as I found a problem and came up with a solution!

Lithium chargingThen someone pointed out to me that SOME of these units turn off the output when charging. Well of course they don’t ADVERTISE that – so that’s a problem if you’re looking for cheap options.

So today I received from BANGGOOD a cheap circuit board that promises to do the job. As it happened my one and only Lithium in a torch left in storage over the winter had kicked the bucket… but I remembered I had a spare but dead battery pack for one of my laptops – and proceeded to open it up. Sure enough half a dozen Lithiums in there.

I took one out that looked clean – dead. Put it on my big power supply for a couple of minutes at 2 amps – it looks ok.  So now I’ve attached it to this little board and plugged the board into a decent USB supply. one of the little chips is getting a little hot as it pumps power into the board but I think it will be fine (update once the battery ha some charge this cooled off – then running a FriendlyArm SBC nothing at all got warm).   So after it had been on for a while to give the battery a chance to get some power, I plugged an SBC into it’s output. You can see all of this in the picture above. Worked a treat and the little board settled into a blue-flashing-light mode after a few seconds.   I repeatedly disconnected the incoming power – no difference, the SBC just kept going.

So if you want something new to play with…. and you’re aware of the dangers of going within 100 miles of Lithium batteries – you might want to give this a shot. At £2.37 inc shipping – you can’t really go wrong provided you give it a damn good test before doing anything serious with it.  I think I may make a little bank of them with the remaining Lithium batteries from my power pack.


I’ve had a look to see if I could identify the bits – the battery charging chip is easy and monitoring (2 of them) but can I hell figure out what is used to provide the 5v out boost.

But there is a catch! I only just discovered this...  If the battery is TOTALLY DEAD and you start things up - nothing happens - the battery doesn't charge - the load does not come on. Disconnect the load for a short time and the battery immediately starts to charge and you can now reconnect the load.

This got me to thinking about a solution and in keeping with the ultra-low costs above, here is my ultra-low cost solution..

An Arduino Nano with relay (total cost around £3 from AliExpress) can control the output of the unit via the relay. The analog input (A0) can attach to the battery while the NANO is driven by the output of the unit.  The ultra-low current consumption of the NANO when the relay is off should prevent issues....  so what is needed is this - when the power gets low, this unit should alert the Pi to shut down..  it does this with a GPIO bit (D11 in this case) and optionally via serial - a nice way to do this would be to output a JSON package.

Here is an example of testing the little board from power up..


So the output message is init - and stays that way - the power is seen to be adequate - and a timer starts - after X seconds the relay turns on. Next, the battery is seen to be low - a timer starts and a warning GPIO bit is turned on  - the Pi could depending on your software, choose to use either the serial signal or the digital input - and shuts itself down.  In my case, serial was out so a simple port input was used instead.

The output from the NANO then goes low and stays low until the battery voltage rises significantly. You HAVE to have "hysteresis" in there - i.e. a difference between ON trigger voltage and OFF trigger voltage as the battery is going to recover a little the instant you turn off the load.

Now then there is an important point here - the NANO in this case is running on 5v... and the Pi will be 3v3 - so in the case of the WARNING OUTPUT and the SERIAL OUTPUT if you use one, the other or both you will need LEVEL SHIFTING - the simplest being a pair of resistors - that is a resistor from the NANO output to the Pi and a resistor from that PI pin to ground - as you don't want 5v going into the Pi - the maths is simple - I'd make the first resistor say, 10k and the second (grounded) resistor, say 15k.  In my case I just used a simple serial 1K resistor to limit current.

OLED display on controller

What we have here is an AliExpress OLED display costing just a couple of pounds, a cheap supply as mentioned above – running on a single cell and running on an Arduino NANO.

For the OLED, the Adafruit library uses WAY too much RAM but this little number doesn’t.. https://github.com/greiman/SSD1306Ascii

Here’s a link to the little OLED display -  on a cheap Chinese Nano it uses pins A4 and A5 and you need to use a couple of pull-ups to 5v for the i2c. I used 2k2 as they were handy  - https://goo.gl/nXjpxd (another – possibly slightly cheaper https://goo.gl/PO0GiO)

Code here https://bitbucket.org/snippets/scargill/XLeLE

This unit also lets you set the max and min voltages and this is stored in EEPROM. I’m using 3.7 and 3.1 by default. Pins to ground for the controls are D5 for set, D6 for up and D7 for down.

And here’s a diagram kindly provided by Antonio, aka MrShark (on this blog comments) who’s been closely following this project! If you’re on a PC you can expand these images by clicking on them. Power supply is just the nearest available and does not represent the current supply you see in the photo above.

As for getting the Pi or whatever controller you are using to listen to port inputs – if you’ve a genuine Pi that should be no problem – I was using a NEO2 – not quite so simple until it all started to sink in – read this entry for more about accessing GPIO - http://tech.scargill.net/gpio-the-hard-way/


Disclaimer: if this blows up your Pi – or worse – blows up – it is on your head! If you don’t understand the above – don’t do it. Make sure you have everything working and polarity is correct before attaching anything expensive.

Incidentally if you want to roll your own and find the above  text-only library for those OLED displays a bit limiting, the Adafruit lib is no good as it takes WAY too much RAM. The u8glib library on the other hand uses <400 bytes of RAM and has full graphics capability – here it is – I ran their standard test and it ran no problems – https://goo.gl/e2yVNM – if on the other hand you want speed, stick with the one I’ve used.


64 thoughts on “Uninterruptible Supplies

  1. I was looking for a cheap solution for a long time 🙂
    I order from this module and will try
    Thanks for the sharing

    1. There's a 4056 and several other chips on the board. The 4056 is presumably the one that handles charging - I'm not sure what handles voltage conversion to bring the output up to 5v - whatever it is, it does a good job.

  2. Would this be a reasonable option for solar charging?
    I'm looking to power a project with a couple of 18650 cells, with a solar panel to charge them, but I was running into the "no output while charging" situation you described.

    1. I see no reason why not but there is a pair of input pads - I'm wondering if they'd be better suited to variable input voltage than the usb input. Perhaps someone familiar with the 4056 could comment?

      1. I'll rephrase that - it seems like the chip is expecting something around 5v input... so you'd need an extra stage - a switching downconvertor - mind you, they're only around £1.50 - I've seen them handle an amp output - taking in anything from 5v to 30v - might check the same supplier. Each of these stages of course reduces efficiency but if you have a big enough solar panel (as, funnily enough I do - I've a couple of 12v (generating up to 18v) 40w panels... I dare say this board and one of those step down boards together would do the job for solar.

          1. Agreed on that tiny solar cell. I have one from Ray-o-Vac, made for outdoor trail cameras, that I want to use. Just took it outside with my meter and it did a steady 11v at 105mA, so with a step-down converter, I should be in good shape.

  3. Does the 'Mini USB To USB A Power Supply Module' have a status pin that you can connect up to one of the GPIO pin's on the SBC and shut it down after x seconds when it is on battery, like a normal ups?

  4. I have something similar for my projects for recharging but I've found I can power my project at the same time I'm charging although the device is battery powered and the mains just tops it up.

    I'm using to power my remote temperature display so that I just keep it on top of a QI wireless charger pad and it will run for around 4 days when removed. (I'm sure I could get it to run longer if I revisit the code)

  5. I'm still searching for my perfect Pi UPS. For something so simple, it seems many people want to make it really complex. I think I'll end up running one of them from one of the little switchmode "buck" regulators that you can get for a few dollars from China. I can then diode or a 9v power supply and a pack of 8 x 1.2v NiMH batteries onto the input to the regulator.

    I came across this project - I'd be interested to see if anyone else is using one? https://pi.gate.ac.uk/pages/mopi.html

    1. The most likely cause here is that you did not install as user Pi and/or you were not sitting in the /home/pi/.node-red directory when you installed it.

      The folder should be in /home/pi/.node-red/node-modules folder. Just delete the folder and try again.

  6. This is a great little board. It's been working well for me for the last 3 weeks.
    I noticed one potentially troubling behaviour, though:
    I have a bank of three 18650 cells connected in parallel, with an Arduino and sensor shield.
    I left it running overnight to drain, which it did. The Arduino, of course, shut down.
    I connected it to power to charge, which it did, however, the power to the Arduino did not restart until the Arduino was disconnected and reconnected.
    I need to do some more testing to see if I can reproduce it.

      1. Wired up another module, this time to a single 18650 and attached my Arduino and shield (about 1.8 A load) and let it run down.
        When I plugged in charging power, once again the Arduino did not come back to life automatically. I let it charge for a bit and it still did not. I pressed the Arduino's reset button and this didn't work, but disconnecting and reconnecting the USB *did* let it restart.

  7. Thank you for posting about this little power supply board, Peter. Just what I was looking for...

    Cheerful regards, Mike

  8. An outstanding share! I've just forwarded this
    onto a friend who has been doing a little research on this.
    And he in fact ordered me dinner simply because I found it for him...
    lol. So allow me to reword this.... Thanks for the meal!!

    But yeah, thanx for spending time to discuss this issue
    here on your web page.

    1. Thanks Michal - that link actually seems to have a nice charger... but for one thing - they say 2 amps output (that would be at 5v) - but at the same time say 0.5amps charge current at 5-8v.

      Hmmm.... no warning signal for the Pi to shut down either...

      The more I read about these things the more I'm inclined to get the PCB design package out 🙂

      Nice and compact though...

  9. According to my humble opinion, the solution with lithiums lacks the elegance i learned to love in your blog.
    My recommendation is the use of supercharge-capacitors with these chinese 5V step-up convertres as described in
    "https://www.stall.biz/project/usv-fuer-homematic-ccu2-und-raspberry-mit-wirkungsvollem-ueberspannungsschutz" .
    I think google translator will offer the clever solution decribed in there ( as it´s unfortunately written in german language ) .
    And´it´s in my opionion a clever straight forward solution. Any raspberry, ESP .. only needs to spend one I/O pin with 2 resitors as voltage dividers to detect voltage failure. And the software needs ( or even has to try ) to send the last MQTT power failure message and start the controlled shutdown of system.

    1. Hi Klaus

      I've had a preliminary play with supercapacitors and buck convertors with no joy. There are no doubt elegant ways but none of them seem to be particulary cheap. There is little point in getting one of these £20 boards then spending £40 on backed-up power supply for it... hence I'm hoping discussing in here will provide at some point one or more cost-effective solutions for hobbyists. My little test board is actually working a treat - the reason I've gone quiet is I can't get the serial ports to work on the FriendlyArm NEO2 🙂 Indeed I've just written to them to ask the point of providing a board with so much IO and no drivers to make use of it.

  10. did my best with (the beast) Fritzing, was my first design (under indications of Peter about some errors and later additions)... now i understand why so many hate it (crashed 2 times, with partial recovery on restart...) and prefer Eagle or others... but this is the only i know with a breadboard view for easier understanding (not everybody knows real electronic components symbols...)

    note that the serial connection is not reported on schematic, in case you use it you better add an other limiting resistor on the line which goes from the TX of the arduino to the RX of the SBC you're using, as most of them are 3.3V and not 5V... no need for reverse sbc tx-->arduino rx as it's not used and anyway the arduino can detect a 3.3v signal as HIGH anyway, no problem here... but, as said, not used in this case...

  11. I considered https://www.tindie.com/products/xorbit/lifepo4weredpi3/ because it does say in the description:
    -An auto-boot feature to maximize uptime by making the Raspberry Pi run whenever there is sufficient battery power or when the USB input voltage is present.
    -An auto shutdown feature that makes it possible to automatically shut down the Raspberry Pi when the USB input voltage has been removed for a programmable amount of time.
    But yes a bit more expensive....

    1. But thanks Scott for bringing this into the discussion. I noted they state 1.33 amps. Is that really going to be enough for the Rasperry Pi 3 and can it be used with other boards? Often manufacturers of these boards state you should have a 2 amps power supply.

      So - one or two options out there, but we keep looking. Basically there is nothing stopping anyone with PCB skills taking what we have here now and making a new PCB to do the job... but I would like to see something that remains pretty low cost, now I'm used to it I do like the idea of the display.. and I'd like to see the ability to handle 2 amps. Some useful comments on that Tindie links about batteries!

      Something that is REALLY nice about that Tindie project - is the SIZE.. the board is tiny...

      1. Pete, I would also not buy the finished product and yes, I did see the limit which I think comes from the LiFePo battery he is using and not from the MIC2876 boost regulator with a 4.8A switch. Add a good heat-sink and a LiFePO4 26650 or a LiFePO4 32600P battery and you are better off. I am also looking for a better solution so I am thinking of hacking this one.
        In https://hackaday.io/project/9461-lifepo4weredpi he mentions he made a board to use the TI TPC61235P, which is a significant step-up to the MIC2876. I will ask if he will sell a few boards.

        1. I see what he's done - well, having gotten to grips with the monitoring side now - all I need is a very basic charger/buck convertor to provide the 5v... pref at 2 amps....

  12. So - monitoring and control cracked - what is now needed is the simplest possible circuit for a charger and boost-to-5v circuit able to simultaneously charge and power the load - but able to power up to 2amps - and pref with a logic control of the output to save having to use a relay.....

    Thoughts anyone.....

  13. I did think of a better way to do the status if one didn't want the LCD. Instead of using 1 light.. use 2... red and green - so red is standby, red flashing is considering coming on - green is on - green flashing is warning the micro it is about to shut down.... I think that's better and means in general use, no flashing lights.

      1. I would be inclined to use a serial RGB LED for that.. but I think folk are more likely to have red and green LEDs lying around. All my serial RGBs are SMT. Anyway the software is simple enough to do any of those based on a flag.

  14. I've found this page providing a nice comparison of various power supply solutions available for RPI UPS.
    Most offer finished products, however some are open source. You can review what has been tried.

    The TPS2113A autoswitching power multiplexer, also looks like a very helpful little IC. It allows to "switch seamlessly between two power sources of 2.8 V to 5.5 V while blocking reverse current into either source."

    1. Hey thanks for that Michal - the LIPO versions of these still seem to think 1 amp is enough (or less in some cases).... designs must be before Pi3 (though most of the boards I get these days recommend 2 amp supplies). I'll check all the links.

    1. But you see... THAT is the problem - these things are 10 a penny and are usually based on similar chips - but the specs are so vague you can't really tell and being Chinese your chances of getting a dialog with the supplier are slim. Two questions always to try and get answered - CAN it supply full power AND charge at the same time - and WHAT happens if the battery is FLAT - will it start up with a load connected - many will fail one or both of these challenges - presumably just due to lack of thought in the design process.

      So this looks good but the display is little more than a gimmick as it doesn't do anything. I'm wrestling with the idea of getting a little board made or encouraging someone to get one made - with my little OLED - maybe something that could pin onto the back of the Nano - and I only say THAT because the NANO is so cheap it is hardly worth duplicating. Actually there's a 4 line version of that OLED which is even smaller which would INDEED mount on the back of a Nano... I'm wondering how much degradation I'd get by using a P channel MOSFET to supply power to the Pi...mainly as it would be 10x smaller than a relay, not for any other reason.

      1. I wish we had schematics for some of these - on that board in question I can see the charge controller - and I can see what looks like 3 mosfets - but I can't see the bit that provides the output - surely that must have a chip.

      2. I ordered one. Testing and reporting in... 45 days? 🙂

        I am currently using a nice powerbank. One of those that keep powering the Raspi even when you plug and unplug it from the charger.

        I has been working flawlessly for more than a year, but it is a bit expensive for 'massive' deployments.

  15. Had a similar problem for a car dashcam. My car has start/stop and cuts power to the cigarette lighter socket during this. This was causing the camera to sometimes stop and not restart.

    I got 2 £1 usb powerbanks, and took the battery out of one, and wired it to the battery of the other powerbank, ie share the battery.

    One powerbank is now able to supply power via usb, and the other can be used to supply a charge at the same time, whereas they often cut output during charging.

    Cheap, and works well for me.

    1. Hi Paul

      So I'm looking at this on the English language site - https://goo.gl/KM6Q0i

      Around a tenner.... Not sure what Tesla's batteries have to do with anything as he doesn't exactly sell them in your typical Chinese outlet... however... "the output can reach 2 amps" - does that mean it can PEAK at 2 amps or that it will handle 2 amps... no sign of any heat dissipating arrangements. 0.5 amps charging current - they all seem to be tarred with this brush... so let me do the maths - starting with a flat battery.... (mains went off overnight....) and another power cut is coming along - you need 4 hours charge to get 1 hour out at 2 amps. I guess that's being picky but I do wonder why that limit.

      5v to 8v input is good - that means at a pinch you might be able to supplement things with solar input... that would be good. I'm assuming this then is merely a power supply - and not any kind of monitoring or having any kind of alert system which basically puts it on a par (though a lot neater) with solutions out there but POSSIBLY with that advantage of greater output.

      Would LOVE to hear from someone who can give this a good hammering and check the 2 amps claim, check if it will start up from dead etc.

  16. Searching for a different thing I bumped into these cheap UPS/Powerpack hats.

    Description (UPS board) says:

    - Can be charged and supplied power to raspberry pi at the same time.
    - With 4 LED power indicator;
    - Provides an example code to beginner to demonstrate how to demonstrate battery capacity displays the current battery level in the raspberry pi
    current program, it can detect the current battery, voltage etc.
    -- Applicatoin scene 1: You can program to control automatic backup of data at low power and safe shutdown.
    -- Applicatoin scene 2: Remind users to charge at low battery levels. etc.)
    - Removable battery, user can choose a more larger capacity lithium battery pack (NOTE: voltage must be 3.7V).
    - With safety protection circuit to prevent overcharge and over discharge.

    UPS hat: https://www.banggood.com/Geekworm-UPS-HAT-Board-2500mAh-Lithium-Battery-For-Raspberry-Pi-3-Model-B-Pi-2B-B-A-p-1105260.html

    Powerpack hat: https://www.banggood.com/V1_0-Lithium-Battery-Expansion-Board-For-Cellphone-Raspberry-Pi-3-Model-B-Pi-2B-B-p-1059297.html

    Comments are a bit scary, however 🙂

    1. the second one seems the one that Peter tested and was rubbish... you can find in other blog posts a photo where Andreas Spiess added a capacitor to try to not have it power down the pi, but even so is not ideal for rpi3...

      for both: none of them arrive at 3A, they talk about 2A or 1.8A, and NOT saying if it's continuos or only peak usage...

      a good ups have to have these features:
      1) 3A stable, or at least 2A stable and 3A peak...
      2) can be charged WHILE using it, without causing a reboot...
      3) startup the pi EVEN if battery is low, if charging power is back... many of these will not work if battery is flat and load is connected...

Leave a Reply

Your email address will not be published. Required fields are marked *