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

Raspberry Pi UPS: Software

Raspberry Pi auto shutdown software

To control the safe shutdown circuit we setup two GPIO pins. One output GPIO 19 that is set high when the RPi is running and one input GPIO 17 to receive the power fail signal. The running output holds the relay normally energised so that the battery is connected in circuit ready for the power to fail.

When the power fail input goes high the software initiates a system shutdown. When the shutdown is complete and it's safe to turn off the running output goes low, the battery drops out and if there is no 12V supply present the whole system will power down.

Download the Python script "" and copy it to "/home/pi/safe_shutdown"

To setup the GPIO and start the safe shutdown script by editing the file /etc/rc.local

rc.local GPIO setting rc.local GPIO settings

After you reboot your Pi the safeshutdownmanager will be running in the background ready to shutdown the system when the power fails.

sudo reboot

Don't forget to connect GPIO 19 to the RUN input of the safe shutdown circuit and GPIO 17 to the power fail output.

To verify that the script is running and protecting your Pi type the following command:

ps -aux |grep safeshutdownmanager

If the script is running it will be printed along with it's Process id (PID):

root 2214 0.0 0.7 6540 3556 ? S Mar13 0:11 python /home/pi/safe_shutdown/

In the above example the script is running with PID 2214. If necessary it can be stopped with the following command:

sudo kill 2214

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

Comments (66)

Topic: Raspberry Pi UPS
Full StarFull StarFull StarFull StarHalf Star 4.5/5 (15)
Full StarFull StarFull StarFull StarEmpty Star
shadin (Bangladesh) says...
Nice article. I read it several times but could not find the details regarding the running pin GPIO 19.
25th January 2022 3:50am
Full StarFull StarFull StarEmpty StarEmpty Star
Antonio (UK) says...

I need some help with trying to get a Raspberry pie UPS but that can also supply 12v to keep a router a live.

can you suggest anything?

3rd December 2020 11:28am
Matthew (US) says...
Hello, I need some help with trying to get a Raspberry pie UPS but that can also supply 12v to keep a router a live. can you suggest anything? Thanks If I understand you correctly, you want a UPS to provide both 5v for your Raspberry Pi & 12v for your router. I have seen dual voltage buck converters on the market. If you don’t want to go that route, you could buy a pair of single voltage buck converters and use them in parallel from the same battery-protected circuit. But to do so, ... Read More
6th December 2020 6:01pm
Christopher J Lafky (US) says...
I am planning on making this same design but instead using normal Nimh cells from an rc battery pack. This wouldn't change anything right? I mean I technically wouldnt need to trickle charge anymore probably but that doesn't matter much to me
4th August 2020 3:01pm
Christopher J Lafky (US) says...
Also... what is LK1??
4th August 2020 3:24pm
Matthew (US) says...
LK1 is an option in case you want to change the design. Closing that circuit would close the relay as soon as 5V is provided, and would hold the relay closed until the batteries are depleted. In that case, the Run pin should be disconnected, as it could cause harm to your Raspberry Pi (the GPIO pins use 3.3v, so sending 5V to it could damage the pin). The schematic is drawn with LK1 normally open, and it’s designed to sense a power failure and shut down the Raspberry Pi when that happens, ... Read More
4th August 2020 10:03pm
Matthew (US) says...
Btw, Nuno asked the same question about LK1 below. You can read Steve’s reply to Nuno, as he described what LK1 is much better than I did.
4th August 2020 10:12pm
Matthew (US) says...
I’m in the process of building this UPS design and want to pass along some advice. I ran into trouble with the 1N5404 diodes I ordered from Digi-key. The leads on the 1N5404 have a diameter between 1.2-1.3mm. This makes them too large for some perfboards. I’m looking at the RL207 diodes instead. They can handle 2A instead of the 3A that the 1N5404 can handle, but even 9.6VDC, you shouldn’t use more than 2A with an ~85% efficient 5VDC buck converter. I’ll try to post a review once I get ... Read More
3rd June 2019 2:33am
Matthew (US) says...
I am not an engineer nor an electrician, so please correct me if I am wrong. This is why I feel safe using a 2A diode instead of a 3A diode: 1) Simple argument: If the fuse is 2A, it should blow if the current exceeds that, and the diode should not exceed 2A for any longer than it takes the fuse to blow. 2) Mathematical argument: I am assuming the buck converter is about 75% efficient (lowball estimate, reasonable for a buck converter). My main supply will be ~24VDC rather than 12VDC, so I am ... Read More
4th June 2019 1:03am
Matthew (US) says...
So I posted lots of photos of my UPS on Imgur. The link is here. In use Finished with the soldering Modified schematic I really invested a lot of time on this and learned a lot about electronics along the way. This has been an immensely enjoyable process, and I want to say thank you to Steve for his original design and software. I modified the software in some places. I used GPIO 14 and 15 instead of GPIO 17 and 19. So my /etc/rc.local file reads like this: # Pin 14 sends a Hi signal until ... Read More
13th July 2019 4:15am
Steve says...
This is awesome Mathew. I'm pleased that you made it work for you and managed to modify the circuit to do what you need. That's what engineering is all about Smile
13th July 2019 6:26pm
Matthew (US) says...
The first script is a simple bash script which executes a python program to turn the pin on or off. I named the first script "" and installed it in ~/safe_shutdown/ just as Steve suggested. Then I added this line to crontab: @reboot /home/pi/safe_shutdown/ >/dev/null 2>&1 Here's "": #!/bin/bash # Manages the fast charging circuit. # The script will check every minute how long the Pi has been running, # and after 2 hours, it will ... Read More
19th July 2019 1:28pm
Matthew (US) says...
The second script needs to be named "" and installed to "~/safe_shutdown/" in order to make the above script work. If you choose to name this script something else, you'll need to modify the bash script to match. import sys import RPi.GPIO as GPIO GPIO.setwarnings(False) #var1 = sys.argv[1] charge_pin = int(sys.argv[1]) #GPIO pin is passed from calling program or command line pin_on_off = int(sys.argv[2]) #State of pin is passed from calling program, ... Read More
19th July 2019 1:38pm
Matthew (US) says...
Just realized there were some typos. I had named my variables with an underscore, but then used variables without the underscore in the code. I corrected the code by changing the variable names. I also added a few comments to help clarify the code. #!/bin/bash # Manages the fast charging circuit. # The script will check every minute how long the Pi has been running, # and after 2 hours, it will turn off the GPIO pin to disable the # high current charging circuit. High current charge circuit is ... Read More
13th November 2019 12:46am
Matthew says...
The UPS I built is fully compatible with the new Raspberry Pi 4B. I have a pair of 2GB models, and they both work with this UPS. I had to add some 5v rail headers to my design, but I can run two RPi 4B computers on the same UPS. Many lithium battery driven solutions on the market can’t provide the necessary amperage to drive one RPi 4B, let alone two. This design has proven very robust and effective. We had some power outages during the winter, but I haven’t had a single corrupt microSD ... Read More
15th May 2020 8:40pm
Jana (Malaysia) says...
Miuzei Raspberry Pi 3B+ Battery Pack Expansion Board, UPS Battery Pack with Acrylic Case for Raspberry Pi 3B+, 3B, 2B. Hi, i am looking to purchase the above product. do you guys ship to Malaysia? if yes, how do i place the order?
22nd April 2019 2:49am
Full StarFull StarFull StarFull StarFull Star
Matthew (US) says...
Hey Steve, thank you for the design and all the wonderful information. As a novice, I am curious what changes would need to be made if I used a 14.6vdc or 19.5vdc power supply instead of the 12vdc one? I have a transformer which outputs 14.6vdc@2.4A (~35W) as well as an old laptop power supply (19.5vdc @4.62A). I’d like to power my RPi and a small amplifier from the same power supply, if possible, but I’m concerned about overcharging the batteries with this setup. You say the diodes (D7 and ... Read More
6th January 2019 1:45pm
lia says...
Hi Steve,
your scheme is really helpful for me. Thanks a lot.
But, i want to ask. what should i do, if i want to connect with 4 raspis all at once? should i just change fuse to be 8 A?
Thank you, I hope you will answer my question.
5th November 2018 5:15pm
Matthew (US) says...
I’m not an engineer, but I did study this circuit extensively when I built my own UPS and I have quite a bit of electrical experience on aircraft, so I hope I can provide some insight. You’d need to consider several things if you are using 4 Raspberry Pis, due to an increase in amperage. You’d need at least a 5A/8A/12A (Pi0-1/2-3/4) buck converter and a 12V 5A power supply (minimum). If you want to use a power supply with a higher voltage, you could reduce the amperage, but that might ... Read More
24th August 2019 8:55pm
Full StarFull StarFull StarFull StarFull Star
Mike (UK) says...
Thanks Steve for publishing this, very useful and just what I was looking for. I have several Pis powering projectors which are powered on/off with a time switch. No issues so far but I'm sure one day I'll get a call at early o'clock because something refused to re-boot.
5th July 2018 7:46pm
Dhanalakshmi (India) says...
If we will use 3.7V 1000mAh NIMH rechargeable battery for raspberry pi(5V/2A) with 7 inch display(5V/2A) instead of battery used in this circuit
5th June 2018 12:54pm
Vontux (US) says...
Is the 'Raspberry Pi UPS: Trickle Charger' article no longer available? The article doesn't seem to include any info different from the 'very simple' version.
27th June 2017 10:59pm
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
lia says...
Hi Nuno,
did you try this circuit?
6th November 2018 12:52am

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. *
Click here for a free subscription to "HDE" our site newsletter.