[UPDATE] I have designed a new circuit for embedded applications and servers that need to be on 24x7, that is easier to build and more simple. It is for a large part based on the discovery process described below, but the link shows a complete HOW-TO using the latest developments for the Pi.
viewtopic.php?f=37&t=132201
My goal is to provide the means for a controlled shutdown of the Pi when there is a power failure.
There have been a few posts with similar topics, but not quite the same. At least none that I could find.
As we all fear, a sudden loss of power could possibly cause a corrupted SD card.
Because I did not have the proper means at the time, I must admit that while running my web controlled thermostat program for more than 6 months now, I've had to take the power away from the Pi to get it to do a restart several times. I've done that during testing and development as well, and I've never had a problem. (lucky me?)
But, as they say, I'd rather be safe than sorry, and my luck may be running out, so here we go.
[update] while working on this project, I did manage to corrupt the filesystem on my memory stick. I didn't use the SD card, only use that for booting, but still... I guess my luck ran out, and it proves you really need this kind of protection!
I put together a few elements of different designs and applications to come-up with a circuit I hope will work. One word of caution, I'm no longer a designer, and I have also not built the circuit yet. The purpose of this post is to solicit comments and improvements and come to a solid usable solution.
Below is the proposed circuit:
There are actually 4 building blocks, let me try to explain them one by one.
Block 1: The circuit around R2, R3 and R4 is put together to create a falling edge on a GPIO input when the 5V supply is no longer there. The software will get an interrupt, and we can wait for a second to see if it was just a glitch, or if we need to do a full shutdown. This period will take about 5-7 seconds, so we need to provide emergency power for that long.
Block 2: The charging circuit for the NiCd. (IC2, T1, D4, R1, D6 G1, C2 and C5). I found this circuit as a NiCd charger for multiple cells, but modified it somewhat. It works as follows. The LM317 is in constant-current mode, supplying a charging current to the AA Ni-Cad cell G1, through R1. D6 is there to block the discharge when there is no 5 V supply. When a cell is connected, the voltage drop over R1 causes T1 to be biased through D4. This results in about a 50uA current from the Adjust pin of the 317 to the AA cell, starting the constant-current mode. The charging current I is determined by R1. I have found that the charging current should be 1/32 of the capacity for top-off or 1/64 for maintenance (trickle-charge). Also 1/10 is safe to use. R1 is calculated as follows: (1,25V + Vsat {=0.1V}) / charging current I. For a 2200mAh cell, the maintenance charging current is 2200/64 = 34 mA, which makes R1 to be 39 Ohm, 1/4W. C2 and C5 take care of smooth charging and transients.
Block 3:The Step-Up DC-DC convertor around IC1. I selected the LT1073 because it works with a single 1.25 cell, and provides a 600mA continuous current, and a peak of well over 1A. It comes in a pre-configured 5 V output version, which is perfect.
Block4: is the power switch. Q1, a HEXFET will start to conduct when the gate goes low, caused by the absence of the 5 V. It then feeds the stepped-up 5V to the Pi. D2 blocks that towards the left. With a 2200 mAh cell, we should have plenty of time to perform a full shutdown, if not we can select a higher capacity (2700mAh) cell, or even go to two cells.
When the power comes back on, the Pi should do a normal wake-up from it's beauty sleep, perform a short boot and the Ni-Cd will start to recharge.
At least, that's the theory. I look forward to your comments and suggestions.
Note : I always try to use through hole parts, which determined the parts I've used. There are more elegant and better solutions, but I can't really use them.