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

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 "safeshutdownmanager.py" 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/safeshutdownmanager.py

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 (44)

Topic: Raspberry Pi UPS
Full StarFull StarFull StarFull StarHalf Star 4.5/5 (11)
Facebookdel.icio.usStumbleUponDiggGoogle+Twitter
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 ?

Thanks.
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.

Steve
3rd June 2016 8:42am
Craig (UK) says...
Thanks!
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.

Steve
3rd June 2016 8:39am
Craig (UK) says...
Excellent.
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?
Thanks,
Borut
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?

Thanks,

Neil.
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.

Steve
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.

Steve
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.

Steve
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?

Regards,
Arthur
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.

Steve
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...
Hi.

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...
Hi,

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 ?

Regards,
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.

Steve
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 ?

//P
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?
Thanks
Andrijan
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.

Thanks!
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.

Steve
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.

Steve
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.
nuno
30th May 2015 2:03pm
Full StarFull StarFull StarFull StarFull Star
nuno (Portugal) says...
hi

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 .
nuno
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!
regards
nuno
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
Captcha
Refresh
 
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.