Personal cloud on Raspberry Pi and development of an uninterruptible power supply device for it

Idea
For several years I used services like Dropbox and Yandex Disk, I decided that I need to make my own storage. I chose between the X86 server on Windows / Ubuntu, Raspberry Pi, Odroid, CubieBoard, etc. I stopped at the Raspberry Pi, because it is the cheapest solution and the instructions for it are already fully written - and I'm new to Linux, the presence of step-by-step manuals is important to me.
On the software side, I chose between OwnCloud and BittorentSync. I tried both and chose the second - I liked the decentralization and the ability to store data in an unreliable place encrypted (more on that later). I also wanted to solder something of my own, and I decided to make an uninterruptible power supply on a lead-acid battery.
It was assumed that Pi will be an always-on device, while BittorentSync will work on a working computer and home, a small part of the data is also synchronized with the phone. As a result, information should be stored in three places: one of them is always on, computers are encrypted by Truecrypt, and data is encrypted on Pi using BittorentSync.
Configure BittorentSync
I will not describe how to configure it and connect a hard drive to the Pi . I’m just telling you about a feature that I have not found in any of these tutorials. As you know, sharing occurs by copying the key (secret in BittorentSync terminology). There are Read only and Read write keys. But there is one encrypted key ( encrypted secret ). If you share this key, the machine will be able to download the data, but each file will be encrypted with the key obtained from this encrypted secret. The problem is that this third type of keys cannot be generated in the usual way, first you need to ask them for an API key , then shamanit with config files, and then the web server at 127.0.0.1 will generate three types of keys at once.
Circuit design
I can draw a diode bridge, but this article is still partially about me. When developing the scheme, I relied on a thorough modeling of all processes and did not count anything, as taught in institutes, twisted everything in a simulator so as not to be mistaken. I read dozens of articles with battery and UPS charging schemes and, to my surprise, found that most of them simply did not work, my modeling of these schemes shows that they are incorrect. Moreover, in the comments they write "thank you, everything is ok." It seems that no one checked them. As a result, I checked them all and collected a mutant from a dozen schemes that seemed to work in a simulator ...
Electric circuit
The whole circuit for convenience is divided into 3 parts: UPS with battery, automatic shutdown circuit, output.
Part 1

Here, the voltage from the laptop power supply is 19V to the left, the working range is 16-30V. The output voltage regulator LM317 has 13.6V. Why 13.6? - that’s why .
Resistors R4, R3, RV3 are tuned so that it turns out 13.6V. It was possible to precisely calculate the resistances and dispense with a tuning resistor, but I decided not to count for sure, but just tighten it up later.
Capacitors C1 and C7 are used to filter power to the LM317. In principle, one could do without them, because the output of the laptop adapter has its own filters, and the battery is not sensitive to voltage peaks for charge. But I decided to leave them, as the datasheets recommend.
The R_LIM resistor is needed to limit the current through the LM317 and through the battery, without it the LM317 will overheat.
Three diodes BD437 are needed so that the high voltage of the power supply is not poured into the battery and that the battery does not try to charge itself through LM317. Although I deceived, these are not diodes, but transistors, in which the base is closed to the collector - that is, they perform the function of diodes. What is better than a diode? When choosing components, I proceeded from those that are available in the store and for which there is a model in the simulation program. It turned out that the smallest voltage drop under load from these parts - these transistors, and not diodes, not even Schottky. Minimize voltage drop is necessary to increase efficiency. If, for example, the voltage drop across the diode is 1V, and a current of 0.5A flows through it, then we are wasting half a joule per second in vain.
Part 2

Lead-acid batteries have the following peculiarity: if you continue to suck out energy from them when it is almost over, they will lose their service life. Therefore, when the battery voltage drops to 12.2, the load must be turned off. This circuit is a comparator, which compares the battery voltage with some reference, and decides when to block the load current.
The scheme works as follows. The battery voltage goes to the Zener diode 11.5V and to the input of the reference voltage source AD680. Resistors R6, R5, RV2 form a voltage divider, such that it is close to the battery voltage minus the Zener voltage of the diode (11.5V). The comparator outputs 0V if its negative input is greater, and> 12V if the positive is greater. The resistor with an RV2 twist is tuned so that this shutdown occurs just when the power drops below 12.2V. The pull-up resistor R9 is necessary for the normal operation of the LM393 comparator, as it is written in the datasheet. Capacitors C5 and C4 are needed to filter these sensitive analog signals before the comparator.
Everything would be fine, but when I simulated this circuit, the following problem appeared. Under load, the voltage sags a bit. When it drops below 12.2V, the load is disconnected and subsidence disappears. As a result, the voltage rises slightly. This is enough to make it go back beyond the threshold of operation, and the circuit switches on the load again. From this, the voltage sags again, and the circuit turns it off. A terrible thing happens: in the vicinity of the threshold, the load turns on and off at high speed, no one will like it.
To solve this problem, a resistor RV1 was added. It adds hysteresis to this comparator. The point is that it adds a little voltage to the input when the output is positive. After disconnecting the load, this “help” also disappears, and the voltage needs to grow more to return. Imagine that in one direction you need to step over the curb, and back you can slide down an inclined road without a curb.
Part 3

Everything is simple here. Power output for the load, filtering capacitors, the main transistor-key, and of course the LED. A device without an LED is a waste of money.
Work modeling

These pictures show 3 graphics:
- change in battery voltage set by a sine wave from 12 to 12.6V
- 2 voltage lines at the input of the comparator (red flat reference voltage, green - output after Zener diode and divider)
- current through the load (on or off)
It is seen that when the voltage drops to 12.2V, the transistor closes, and the current through the load stops flowing. Then, after the voltage increases by more than 12.3V (this difference can be controlled by a twist resistor), the transistor opens again, thereby forming a hysteresis. The time scale in these graphs does not matter, it was only necessary to test at what voltages the on-off occurs.
Printed circuit board
Such a simple board can also be assembled on the knee, but I wanted to get the experience of ordering a beautiful board from the factory. Another solution would be to find a friend of the Jedi Laser Iron, but could not find. Quick googling found a company in China that agreed to make me a batch of 5 pieces for $ 10 + $ 4 shipping. Good price, I thought and ordered. After that, for the sake of interest, I sent a request to one popular Zelenograd office, they announced a price of 3500 rubles. Thanks for the offer, I thought. The Chinese worked quickly, after 2 days already sent, after 2 weeks the mail delivered.
It was - it

was

It became

here, an inquiring eye will notice that instead of a single microcircuit, a device with 3 legs - it was I who inattentively did not check the packaging of the device upon purchase. Still, again by inattention, I did not check the diameter of the holes, and I had to fix the board like this in a curved side to the bolts. On the right in the photo is a DC-DC converter on the XL4015 chip . I chose it instead of the most popular LM2596 because of its high efficiency and maximum power value. It can also be seen that in 3 places, instead of the usual resistor, there are two of them. This is because I did not have a suitable denomination and I had to make such a “the beast with two backs”.
Testing
From the first time, of course, it did not work. It turned out that in CAD, the standard pinout of the legs for the tuning resistor does not match the real one. I had to re-solder them. Then it did not work due to the fact that the Zener diode had too much voltage and crushed the signal, and I soldered to the other one 1 volt less.
And now, hysteresis in my comparator, it works! When you turn off the power from the outlet, the Raspberry Pi continues to work and distributes BittorentSync! In every way, she tormented her, tore it out of the socket, pulled the contacts, the board worked stably and did not hang.
I decided to look at the transition processes with an oscilloscope.
Here is power on and off without load (Raspberry Pi is not connected)


The green line is the output of the DC-DC converter, it should be 5V. The yellow line is 19V power from a laptop adapter. It is seen that when turning off, there is no oscillation, and when turning on, there is a small peak.
Here is turning on and off the power with the load (Raspberry Pi and the hard drive are connected and working).


Turning off as usual is good, but when turned on, a peak of 740mV appears. In theory, this is not scary, the Pi and the disk have their own filtering capacitors, they will survive 5V + 740mV for 300μs.
It is recommended to connect power to the Pi not via USB, but through GPIO connectors 1 and 3. Then the power goes around the self-healing fuse, otherwise there will not be enough current to the hard drive.
Pi has a known problem of rebooting and hard drive failure. This problem is treated by soldering an additional capacitor of 200-300 μF over the existing capacitor to USB. Here's an article explaining how. I did not do this, because it worked already.
Price
Raspberry Pi - 1600r
Printed circuit boards 10 + $ 4
Components for a 500r board
USB hard drive - there was already a
DC-DC converter - $ 4.75
Voltage indicators - $ 8
Battery 12V, 4.5 Ah - 480r
Case -
250r Total about 3800r
Weaknesses and future development
When the boards arrived, I discovered several jambs. Basically - not the most successful arrangement of elements. In CAD, it seemed that everything was perfect, and when I picked them up, I realized that the capacitors could be moved there, that circuit was rotated like that, etc. Next time I will print out the circuit to feel with my hands before production.
The LM317 chip should be replaced in a good way with a DC-DC converter, for example LM2596, for greater efficiency and a higher charge current.
The values of all capacitors could be corrected to reduce the peak at startup.
Now I plan to connect a relay to the Pi and turn on the light through the web server, but I will not write about this article.
That's all.
