Building a UPS for raspberry pi (Long post)

Greetings!

First i want to apologize for my English and I hope you understand me well enough to help me with some coding advice.
I have been around the forum for a couple of years and done some smaller project with PicAxe but nothing really big.

Half a year ago i started building two home servers for my dad and me based on the raspberry pi model B.
The plan was to make them syncronize with each other so if either of our houses burned down we still got the family album intact.
After a couple of months with research an a lot of trial and error I got the syncronisation working. Using "bit-torrent sync", the two servers will find each other when they are connected to the internet and syncronize to each other.

Since the place where my father lives is ridden with poweroutages
i thought it would be a good idea to build a UPS for the servers and it is here PicAxe are about to enter the scene.

After some thinking (and a lot of coffee) i stipulated some functional behaviour that i wanted to achive and some possible solution on how to
achieve it.

-1. If power goes down the UPS should send a shutdown signal to the Raspberry PI and wait until shutdown is complete.

Using diode mixing with the powersupply and battery the battery takes over instantly when power fails. Using a optocoupler to detect that
the main power is gone. The battery is connected via a pulse on/pulse off-relay controlled by a picaxe 14M2 so it can be completly disconnected by a
pulseout 5000,relay_off_pin.
On the RPI side I found some code to use interrupts with RPI to invoke a shutdown command.

-2. When shutdown is complete the UPS must disconnect the battery supply to conserve power.

When a RPI is shut down it still draw a lot of power and needs to disconnect the supply to save power.Since the RPI is shut down
it can´t communicate any more, but the TX-pin on the RPI goes from high to low whent the RPI is done shutting down.
This can be detected by the PicAxe who then can disconnect the battery. This is done by the relay. The RPI TX-pin have some "chatter" at
the start of boot who cant be disabled. This needs a solution.

-3. When power returns, the RPI shall start booting and the battery supply shall be reconnected and ready for new power outages.

Since the PicAxe also is powered from the main supply it will start from the beginning of its program when power comes back. This makes us
able to reconnect the battery to the supply line and then power up the RPI again.


-4. The UPS should detect if the RPI crashes or hangs during boot, shutdown or when it is running and then try a reboot by toggling power to the RPI.

I installed a script on the raspberry PI who toggles a pin 1 second high and then 9 second low to indicate running condition. Im thinking there is
a possibillity that the RPI may freeze up in both the high state and the low state but im not shure how i can detect both conditions without running
into trouble.



-5. If rebooting fails more than tree times the UPS should refuse to power up the RPI until i get a chance to watch the boot myself.
To detect this I could measure the time it takes before the toggling of the Pin that indicates running starts.



-6. The UPS should give clear visual or audio feedback on whats happening at all times.

This I guess can be done by keeping a flag-register of some sort, that is acted upon in the different operation modes. I have designed
The board so i can connect 3 LEDs to indicate: "Running on batteries"(Yellow), "Error" (RED) and A green led connected to the signal-line for
the "Running" signal



After this i designed a PCB in eagle and got it fabricated by OSH-park and the initial testing proved that every sub-function worked. (The eagle
schematic is attached as a .zip file but it is not wery tidy.

The following pinout was used:

C.5 Serial inn and a switch (Disconnect need to be issued)
C.4 Relay reset coil (Battery disconnected)
C.3 Low when main power is present. (No interrupt)
C.2 Relay set coil (Battery connected)
C.1 "RPI power control". Controls a 5V 3 A switchmode powersupply. Raspberry PI gets power when this is set high.
C.0 picaxe input<------------------------ RPI GPIO 24. Toggles pin on for 1 second then 9 seconds off
|
|------|>|-----GND
LED

B.0 Serout maybe connect Piezo to this at later point.
B.1 Connected to Yellow LED (Indicate running on batteries)
B.2 Connected to Red LED (Indicate error)
B.3 Picaxe output ----------------------> RPI GPIO 18. RPI running a script that detects when this goes high and initialize a shutdown.
B.4 Picaxe input <---------------------- RPI GPIO 14. When RPI is shut down this goes low.
B.5 A switch. High when pressed.


Then when i was about to start programming and make a fully working UPS, one of the great miracles of life happend. Got me a beatiful son and the
backupserver was placed on the shelf until further notice. Now i have some spare time again and want to see if its possible to finish the project.
I have found that i need help with a lot of things but first i need to brake it down in manageable pieces. So if you could help me with this i would be
forever grateful.
 

Attachments

geoff07

Senior Member
Personally, I would start with a state diagram. This would show all the stable states the UPS can be in, and what events (signals) cause it to move to a different state. That way you can be sure that all your functions can be accommodated and you have all the hardware controls and signals available.
Code:
[FONT=courier new]States might be e.g. 
running normally, possible events: power fail, send shutdown -> waiting for shutdown signal
                                   switch off -> dormant
waiting for shutdown signal: signal received -> dormant
                             signal not received, report error -> error state

or whatever[/FONT]
After that, programming is relatively simple as you will have completely designed the program and desk-checked it
 

lbenson

Senior Member
This is similar to a project I have been contemplating to provide a less power-hungry UPS for my internet modem and router and some picaxe monitors. In my area, the power can go off while the internet connection is still alive. I haven't gotten much further than mulling over it, though.

Note that there is no need to shut off the picaxe from the battery (if that is what you are contemplating). Picaxe power draw, for a project like this, if well-designed, is negligible. See here for a battery backup mains monitoring picaxe which has been running constantly for 7 and a half years on the same 3 AAA batteries:

http://www.picaxeforum.co.uk/showthread.php?8353-Low-Power-Battery-Backup-Reference-Design&highlight=battery+backup+reference
 

hippy

Technical Support
Staff member
I would agree with geoff07; this is a state machine so that approach is the best to use.

It is a project which I have also been considering but have not really gone beyond thinking about it. I was considering a more UPS-like solution which kept the system up and running through power outages rather than just being a safe shutdown system as here. In practice though there's probably not a lot in it.

Either way it is desirable to have some means to hold the main power off or hold the Pi in reset until both mains power and battery power can be supplied otherwise you can end up with the mains coming on and going off again before you have supplied battery power. That could end up with the Pi running part way through booting and potentially corrupting its SD Card which is what the scheme is trying to avoid.

You will likely need some means of cutting mains power or forcing a reset anyway for the case where mains fails, the Pi is going into shutdown, but mains is restored before it does. In that case you won't be able to rely on the mains coming back on to force a Pi reboot.

That's also handy for adding a reboot button which allows the Pi to be rebooted after instructing it to shut down without having to physically power cycle. Better than a Pi reset button which can corrupt things while it's still running.

Depending on what battery backup supply you have you may also need to monitor its state or know how long mains has been on or how long it has been charging for. The last thing you want to do is allow the Pi to boot if there is not the backup power to see it through a subsequent safe shutdown.

As can be seen it can get slightly complicated, needs an eye for "what could go wrong" detail, but a robust safe shutdown or UPS solution should be achievable. I am not convinced any of the existing designs for Pi UPS and safe shutdown systems have considered all the details so a DIY PICAXE control system is well worth considering.
 
Thank you all for helping. I`m starting to write a state diagram tomorrow and see how it turns out.
Some clarification and coments about some points that was mentioned:


Either way it is desirable to have some means to hold the main power off or hold the Pi in reset until both mains power and battery power can be supplied otherwise you can end up with the mains coming on and going off again before you have supplied battery power. That could end up with the Pi running part way through booting and potentially corrupting its SD Card which is what the scheme is trying to avoid.
The picaxe is powered by its own 78L05 voltage regulator. I`m using a PTN78060wah smps (http://www.ti.com/product/PTN78060W/description) to power the raspberry pi and a harddrive. This module have a inhibit pin who turns on/off the 5V to the rasperry pi and harddrive (pin C.1). This makes me able to reset the pi the hard way (if nesecery) or connect/disconnect the battery without turning the server system on.


Depending on what battery backup supply you have you may also need to monitor its state or know how long mains has been on or how long it has been charging for. The last thing you want to do is allow the Pi to boot if there is not the backup power to see it through a subsequent safe shutdown.
First I was thinking aaa-batteries and just replace them after a set number of shutdowns. I have tested them and they are capable of supplying enough current. There are little space in the enclosure im using, so aa-batteries are to big. So i got some small 3-cell lipo-batteries with 450 mah and it seems they are a perfect fit. Then i can use the balancing plug to charge it and measure the voltage to see if it needs charging. Can also count how many times it have run on batteries and give a warning when it needs charging.

It is a project which I have also been considering but have not really gone beyond thinking about it. I was considering a more UPS-like solution which kept the system up and running through power outages rather than just being a safe shutdown system as here. In practice though there's probably not a lot in it.
Would of course wait a couple of seconds (10-20 s?) before starting the shutdown so smaller powerouts don`t interfere with the normal operation.

This turns out to be rather exiting even though im moving forward with baby-steps.

Here is a photo of the work done so far.

20150704_185010.jpgsch.jpg
 
Last edited:

lbenson

Senior Member
Very impressive. Note that if you decide to run the picaxe from batteries, 78L05 is pretty hungry. You might want to consider LP2950 for TO-92 size or LM2940 for TO-220.

Please keep us posted.
 
Greetings again!

Things move along slowly but I have made a draft of the flowchart now. I would be very happy if you could take a look at it and give some feedback on it.

Regards!
 

Attachments

fernando_g

Senior Member
Very impressive. Note that if you decide to run the picaxe from batteries, 78L05 is pretty hungry. You might want to consider LP2950 for TO-92 size or LM2940 for TO-220.

Please keep us posted.
For battery-powered Picaxe projects, I've used the LP2951-5 micropower LDO regulator, and I've been very satisfied with its performance.
It is identical to the LP2950, but it comes in an 8 pin package, which provides an ERROR signal whenever the output voltage drops out of regulation to safely reset the Picaxe. and a SHUTDOWN pin to fully disable the regulator.

On the flowchart....I'm am not a flowchart expert, but your drawing does not really show how the program should flow.

EDIT: On your schematic, you'll be losing a large percentage of your supply voltage with D1. I understand the need to protect against reverse polarity, but better use a schottky diode instead. 1N5820
 
Last edited:

techElder

Well-known member
... I understand the need to protect against reverse polarity ...
I don't! If a technician can't polarize the power connections to get the project powered correctly ... then don't build the project! :)
 
If you look at D1 AND D4 you might get a clue of the reason its there. Even clearer when i tell you that the battery supply is 11.5V and the main powersupply is 12 V. Hint: its about AND.

Regards.
 
Top