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.
Latest update 23/07/2017
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 Lithium batteries 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.
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)
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 - https://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.
Since I came up with the circuit we’ve seen several other supplies and I bought one of those WEMOS supplies- well, not surprisingly, as no-one is doing anything original, this one fell WAY short of claims- check out this video and thanks to Antonio for pointing me to this – https://www.youtube.com/watch?v=aND0j2Y2IkM
I still think my clumsy NANO solution is a good one – now that there is an ESP8266 with OLED display built in – this might be a better way (still needs a relay and the ESP takes rather a lot of current)… on the other hand I’ve recently had a situation back in the UK where a PI has TWICE developed an SD issue that can’t be sorted by rebooting and needs to be sorted by power cycling – possibly the ESP+OLED solution could be used to handle the battery as above and act as a watchdog. With up to 8 lines of tiny text on the OLED there’s plenty of room for info.