Click here for a free subscription to "HDE" our site newsletter.

Raspberry Pi UPS: A Very Simple Solution

A battery, 2 diodes and a Buck converter

Batteries and diodes Battery and diodes

As a freelance electronics engineer I get to see a lot of applications for small and inexpensive microcomputers like the Raspberry Pi and the Beaglebone. In fact this new breed of smart hardware is perfect for a large number of projects that previously might have been done with discrete components or not been done at all because of the cost of the hardware.

The availability of cheap computer power on a tiny board is a huge step forward but it's all too easy to neglect some important considerations when designing with them. The Raspberry Pi is a great learning tool that can be highjacked to create real products solving real problems but there are some important challenges that need to be addressed before you can use them safely in unattended situations.

Computers need to be shut down in an orderly fashion

Anyone who has ever used a Windows, Linux or OSx machine know the drill. Shutdown first then turn off the power. If you aren't aware of why this needs to be done then just stop for a moment and consider what might happen to your computer if it were to be turned off while it was in the process of writing to a file. If it happens to be an important system file then it may well refuse to boot up the next time you turn it on. The Raspberry Pi and Beaglebone and other similar machines are no different. Switch off the power without shutting down at your peril!

Some of my clients solve the problem by simply providing general purpose UPSs to keep all of their equipment alive during power outages. Some ignore the problem altogether thinking that it probably won't happen very often and if it does then they will simply re-program the SD card. They have considered the risks and decided that a UPS is more trouble than it's worth. Some people are starting to use one of the "Smart Raspberry Pi UPSs" that are slowly becoming available.

In this article series I'm going to concentrate on developing a simple "dumb" UPS and safe shutdown circuit. Hopefully it will be easier to understand and build and perhaps cheaper than one of the smart versions. At a later date I might even develop a smart UPS that you can build out of an Arduino or something.

A very simple Raspberry Pi UPS

There are two basic ways to solve the problem. You can either switch to battery power when there is a mains power outage or you can detect power fail and shutdown while running on batteries for a short time.

I'm going to be posting a series of articles here over the coming weeks looking at aspects of uninterruptable power supplies and power failure detection and I'm going to start with the simplest solution that I know of.

Very simple UPS circuit The simplest UPS I can think of

The circuit above is in fact more complicated than it needs to be because resistor R1 and LED D3 are not strictly necessary. They are there only to indicate that the 5V output is present.

The DC/DC device is a 'Buck' converter which supplies 5V at up to 3A (I haven't tried it at a full 3A) for the Raspberry Pi if either the 12V or 9V supply is available. The 3A 1N5404 diodes D1 and D2 prevent the 12V supply from overcharging the battery and the battery from discharging into the 12V power supply when the mains fails.

This is as simple as it can get and may be more than adequate for your situation but there are three features that you might expect to find on a commercially available UPS that are not catered for in this circuit. Power fail detect, auto shutdown and battery charging. It doesn't do either of those but it will continue to run your Raspberry Pi for a time when the mains power fails which could be long enough to avoid the Pi experiencing any unexpected poweroffs.

I would suggest that you use rechargeable batteries for this. Shutdown your Pi once a week and recharge or replace the batteries. I used 8 AA size NiMH batteries to give me 9.6V which works well. With a capacity of 2900mAh and assuming your Pi consumes around 350mA they should run a Pi for up to 8 hours. I would assume that time would be more like 4 hours to be on the safe side though.

Connection from Buck converter to RPi Connecting Buck DC/DC converter to RPi

To connect from the 5V Out terminals on the Buck converter you can use a standard USB A to micro USB B connector and cut off the A connector. I bought one from a pound shop and it works fine. Strip back the insulation and use just the red and black wires. These are the power connections in the cable. Solder the black wire to Out- and the red wire to Out+.

In following articles I will be looking at charging the battery, detecting the power failure and allowing the computer to turn off the power when it has completed it's shutdown procedure.

Now subscribe to our newsletter and don't miss a thing

Comments (43)

Topic: Raspberry Pi UPS
Full StarFull StarFull StarFull StarHalf Star 4.5/5 (11)
And (Italy) says...
What about battery voltage is under cut-off nimh limit?
Thank you
21st June 2016 12:14pm
And (Italy) says...
What about battery voltage is under cut-off nimh limit?
Thank you
21st June 2016 12:12pm
Full StarFull StarFull StarFull StarEmpty Star
And (Italy) says...
What about battery voltage is under cut-off nimh limit?
Thank you
21st June 2016 12:05pm
Craig (UK) says...
Sorry, me again.

Would this also work on a 5V 2A input ?

3rd June 2016 8:20am
Steve says...
No, the power supply must be higher than the battery voltage which must be more than about 7v.

3rd June 2016 8:42am
Craig (UK) says...
3rd June 2016 9:40am
Craig (UK) says...
Hi Steve,

Thanks for the information.

Will this circuit automatically power on the Pi as soon as the 12V is applied ?
2nd June 2016 5:46pm
Steve says...
Hi Craig,

Provided that the Pi is fully powered down after the battery is disconnected then it will restart normally when the 12v is restored.

If the power is restored during the shutdown process and before the battery is disconnected then it will not restart. You will need to remove the 12v for a few seconds then restore it. This is an irritation which is hard to get around with a simple circuit unfortunately.

3rd June 2016 8:39am
Craig (UK) says...
Thank you again!
3rd June 2016 9:41am
Borut says...
Hi Steve,

nice article. I read it several times but could not find the details regarding the running pin (GPIO 19). Where is this pin set as output and who is controlling it?
27th May 2016 2:24pm
Full StarFull StarFull StarFull StarFull Star
Neil (UK) says...
Perhaps I have missed it in the article, but the specs of the relay don't appear to be given...

Am I right in thinking that a standard SPDT/DPDT 12V signal relay (e.g. Omron G5V-1 12V series, non-latching, 1A) would suffice?


16th May 2016 8:23am
Steve says...
Hi Neil,

The relay needs to be a 5v type because it is powered from the 5v output.

16th May 2016 10:40am
Full StarFull StarFull StarFull StarFull Star
Kallu Mama says...
The schematic seems to show the positive of the battery connected to ground. Does it mean that the positive has to connect to 0V of the 12V supply? Or am I missing something?
5th April 2016 6:07am
Steve says...
Hi Kallu,

Whoops it looks like I got the symbol for the battery upside down. The negative side of the battery must go to ground and the positive side to the fuse. Sorry about that.

5th April 2016 8:59am
Full StarFull StarFull StarFull StarFull Star
marc (US) says...
How do you know that all system log files have finish being written to the SD card by the time the "RUN" GPIO goes low (thus disconnecting the battery). On point for this circuit it to prevent corruption of the SD card, which can happen if the power is removed in the middle of file write operation. Can the GPIO go low before all writes are done?
3rd April 2016 2:57am
Steve says...
Hi Marc,

The 'Run' signal is sent low when the system finally enters the Halt state after all shutdown activity has ceased. This does rely on the operating system behaving in this way however. If you want to be absolutely certain then you could add a small delay with a capacitor across R8 and a resistor in series with the base of T3. You will need to experiment with values to get the timing right.

5th April 2016 9:08am
eric says...
Nice article! Back to basic.
I would suggest to include another option: let the RPi know what the status of the battery is, or: add a similar circuitry as implemented for the 12VDC status. So pass a signal to the RPi "Battery_OK" . If this signal is going low it is really time to power down and deactivate the relay avoiding a complete failure of the batteries. This allows for even bigger battery packs.
28th March 2016 2:17pm
eric says...
Looking at some graphs depicting the voltage of a battery until it is depleted I realised that you need a fairly accurate (temperature compensated?) detection circuit because of the millivolt range we need to trigger the battery OK signal.
3rd April 2016 8:50am
Arthur (Austria) says...
Hi Steve,

thanks a lot for your article! Could you tell me, how much power the 12V power supply should be able to deliver, if I need 2A (pi with pi-display) on the other end? At least 2A, I assume. Or does it need more?

20th March 2016 2:41am
Steve says...
Hi Arthur,

It depends entirely on the rating of the 12A supply that you use. If you need 2A for the display then add another 1A or so for the Pi and stuff. You should get a 12V supply rated at 3A or more.

Note that the battery does not maintain the 12v supply so your display will go blank when the mains fails.

5th April 2016 9:22am
Full StarFull StarFull StarFull StarEmpty Star
gary camp (US) says...
It is always good to have backup methods so thanks for the article.

Note that (according to me friend, who is a Linux nut) Tiny Core Linux is the only version that needs no proper shutdown so it can tolerate power fail. If true, it would save a lot of work, though I would still consider an UPS method just to be safe. Especially if it was cheap and easy.

Thanks for the info.
16th March 2016 7:05pm
Full StarFull StarFull StarFull StarFull Star
Antonio Modesto (Brazil) says...

Thanks for posting this. Just one question: How hard is it to modify this circuit to notify the pi when the power source state changes?
14th March 2016 12:36pm
Full StarFull StarFull StarFull StarFull Star
Mango (Germany) says...
Great Article, Thanks!
This is a simple and understandable ups for my raspberry.
2nd March 2016 10:18am
Wossname (Norway) says...
Hia. Wouldn't it be easier to use a Powerbank, and just implement the circuitry & software for car mains shutdown "on the side"

A modern powerbank charges and maintains itself automatically, and is very cheap.
29th February 2016 10:43am
Steve says...
I have considered using a Powerbank and I do use a few of them as convenient test supplies. I keep coming up against some problems however. First off a Powerbank will often charge at a rate lower than it is capable of delivering to the equipment. So lets say your Pi and peripherals need 2A and your Powerbank will only charge at 1A then it will eventually run out of juice. Then there is the problem of detecting that the mains power has failed and shutting the Pi down as a result. This requires ... Read More
29th February 2016 11:41am
Davy (France) says...
Hi, it looks great project for my Pi Smile
I wonder what to modify to make it work as a UPS for my 12V picoPSU powered NAS ?
The aim would be to hard shutdown the NAS when power drop while using an external 12V battery.
To be as simple as possible, it would be acceptable if the circuit don't deal with battery charging.
Could you give some advices about the simplest way to achieve this ?
Many thanks
14th January 2016 9:20am
Davy (France) says...
Sorry, I realized that I wasn't clear in my previous message. PicoPSU is powered by a 12V AC/DC converter so the battery would only be used for the shutdown process. So my question is : how to ground motherboard PWRBTN when AC/DC fails and only for a short time (not to force shutdown) ?
14th January 2016 9:58am
Darkodo (France) says...

In Raspberry Pi UPS Trickle Charger version, I would like to know what happens when the battery have finished to charge ?
Does the charge is stopped to prevent any overcharge ?

29th December 2015 10:58am
Full StarFull StarFull StarFull StarFull Star
Patrik (Sweden) says...
In the first version, could you use standard batteries ? (Nonrechargeable)
And would they lose charge over time ( even if main power is always on ? )
10th December 2015 11:48am
Steve (UK) says...
Hi Patrik,

Yes you can use standard non-rechargeable batteries provided that you remove the diode D9 to disable the charging circuit.

It is very important that you do not attempt to charge a non-rechargable battery as this may cause catastrophic damage.

12th December 2015 5:15pm
Patrik (Sweden) says...
Thanks for your reply !
I was thinking of the first version of the ups, what prevents the battery from discharging while on main power ?
And should minus on the battery connect to 0V ?
Is ground the same as 0V in this setup ?

15th December 2015 12:47pm
Full StarFull StarFull StarFull StarFull Star
Andrijan (Macedonia) says...
Hi Steve,
Excellent explanation and functionality.
Maybe you can rearrange with power bank with 18650 batteries as they already have charger on them.
What do you think?
11th November 2015 1:39pm
Steve (UK) says...
Hi Andrijan, I considered using a power bank for the reason you state. The major problem to be solved with battery backup solutions is the switch over from external to battery when the power fails. A power bank outputs 5v and cannot be connected directly to the main incoming 5v without damaging something. It can't be isolated with a diode either as this would drop an unacceptable 0.7v. This means that the two supplies would need to be monitored accurately and switched very very quickly. It ... Read More
12th November 2015 12:10am
Full StarFull StarFull StarFull StarEmpty Star
Oleksii (Ukraine) says...
Hello, Steve! Thanks for this scheme that you provide! But could you please describe, because I didn't get how relay will know that it needs to switch to buttery and back to 12V? Or you provide it by software? When raspberry received failure signal it send high signal to run pin? I just thinking how to move relay in this scheme, that it will turn on battery when 12V is gone and turn off when it is back. And also do not discharge battery when raspberry is not working. Also think about some ... Read More
17th August 2015 11:39am
Steve (UK) says...
Hi Oleksii, The relay is held energised by the 'Run' signal all the time from the Raspberry Pi which means that the battery circuit is switched in whenever the software is running on the Pi. It does not switch as the 12v fails. The battery is isolated from the external supply by the diodes D2 and D3 and because the external 12v is greater than the 9.6v battery no current will flow from the battery until the external supply is removed. So under normal operation the external supply is ... Read More
18th August 2015 10:49am
Oleksii (Ukraine) says...
Thanks! Now it's clear for me :)

Maybe you may also help me to calculate needed value for R10?
Because I found and buy 2 batteries with 4V and 1.3mAh. I plan to connect it series(ony by one).

And I didn't get with wich formula you calculte value for R10.

19th August 2015 2:56pm
Steve (UK) says...
Hi Oleksii,

Be careful. NiMH batteries can be trickle charged in the way that I show but some other types of batteries can not. In particular do not use lithium batteries in this circuit. It will not work correctly and could even be dangerous.

Stick to Nickel Metal Hydryde batteries.

19th August 2015 3:05pm
nuno (Portugal) says...
hi Steve

sorry for asking again, but if he have the 2 pins short (a link) we are not going to have problems with the 5v and the GPIO 19?
29th May 2015 3:26pm
Steve (UK) says...
Hi nuno,

Yes. If you use the circuit as an Uninterruptable Power Supply (UPS) you must disconnect GPIO pin 19 from the Run input otherwise when GPIO 19 goes low it will be driving into a short circuit and potentially damage the Pi.

It is unlikely that you will want to switch modes very often. If you do then I would be interested to know why.

30th May 2015 12:36am
nuno (Portugal) says...
hi Steve

thanks for the information.
i am not interested to switch between the two modes. i will do only with the disconnection of battery at the end. it is only interesting to keep the battery on if you have for example an arduino with the RPI.

thanks again.
30th May 2015 2:03pm
Full StarFull StarFull StarFull StarFull Star
nuno (Portugal) says...

very nice and helpful project. i just have one question concerning the schematic for the safe shutdown-between the r9 and 5v who have a LK1 UPS and 2 white circles with a gap between them. what it means? sorry if it is a silly questions but my knowledge is limited and i can not figure out the meaning.
thanks .
29th May 2015 8:15am
Steve (UK) says...
Hi nuno, There are no silly questions so don't worry. The 2 circles are meant to represent 2 pins which can either have a wire link between them or not. If there is no link then the circuit relies on the 'Run' signal to hold the battery connected and ready to take over if the mains fails. When the 'Run' signal goes low the battery drops out and power to the computer dies. If there is a link effectively shorting R9 to 5v then the relay will be held in permanently. This means ... Read More
29th May 2015 12:59pm
nuno (Portugal) says...
thank you for the explanation.
very nice feature!
i will build this circuit and test in my rpi.
thanks again!
29th May 2015 1:31pm

Add Comment

* Required information
(will not be published)
Bold Italic Underline Strike Superscript Subscript Code PHP Code Quote Insert line Bullet list Numeric list Link Email Image Video
Smile Sad Huh Laugh Mad Tongue Crying Grin Wink Scared Cool Sleep Blush Unsure Shocked
Enter code:
Notify me of new comments via email.
Remember my form details on this computer.
I have read and understand the privacy policy. *
I have read and agree to the terms and conditions. *
Powered by Commentics
Click here for a free subscription to "HDE" our site newsletter.