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

Raspberry Pi UPS: Safe Shutdown

Power Fail & Safe Shutdown

The first parts of this article describe a simple uninterruptable power supply using a DC/DC converter, NiMh rechargeable batteries and a simple trickle charging circuit. We can take this a step further now by adding a little circuitry to allow your Raspberry Pi to detect that the mains power has failed and to shut off the battery power after a safe shutdown has been performed.

The diagram below is the final circuit of the complete UPS and safe shutdown unit. You might notice that some of the component numbering has changed from the earlier circuits on previous pages.

Full UPS circuit with safe shutdown UPS with safe shutdown

D1, Z1, R1, R2, T1 and R3 detect a failure of the main 12V supply. Transistor T1 is normally turned on when the 12V is present so the voltage delivered to the Raspberry Pi GPIO pin is normally low or logical zero value. When the 12V drops out T1 is turned off and the GPIO pin goes high or logical '1'.

The 5.6V Zener diode Z1 together with the LED D1 and resistors R1 and R2 set the switching voltage at the base of T1. when the 12V input drops to about 8-9V T1 will switch off. If this voltage was set any lower then it would take too long for the input voltage to decay to the switching voltage.

When T1 switches off T2 turns on and illuminates the LED D4. This serves as an indication that power failure has been detected.

The battery of the UPS is connected to the main input supply through the relay contacts RL1 and isolated from the 12V supply by the diodes D2 and D3. The relay is kept energised by transistor T3 which is turned on by a GPIO pin from the Raspberry Pi.

When the 12V power fails the Raspberry Pi senses it on it's GPIO. Using software detailed on a later page the computer senses the power fail and initiates an orderly system shutdown. When the shutdown is complete the "Run" signal from the computer drops to a low value turning T3 off. The relay drops out and the battery power supply is disconnected. The 5V supply then collapses and all is quiet.

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.