Internet backup power

About the time I am wrapping up in the office, the sun has risen and people are getting into work on the East Coast of the United States. I leave early enough to get home so I can make teleconferences that start at 9 Eastern. I’d have quite a phone bill if I dialed direct, but fortunately, I can make most of these calls over the internet. Although Madagascar is a developing country, we have fiber to the house. The upstream connection isn’t entirely stable, but it is up most of the time, and throughput is surprisingly high. However, I still run into problems when the power blinks off.

The ISP has adequate backup power to maintain the connection, at least for some time, so this is my problem in the house. The house too has a backup generator, but there’s a couple seconds to shift over from mains to generator and vice versa. In that time, the houses’s router goes down and takes a minute or so to reboot every time power switches over. What’s needed is a UPS, but the smallest computer UPS here runs around $150. Also, it seems like a UPS is massive overkill when the problem is just to keep a router happy for about a minute.

I have a good stash of small lead acid batteries for portable ham radio use, and it occurred to me that I could probably use a couple in backup power supply projects. At this point, I’ve come up with two designs built of materials squirreled away in my garage. Schematics are below the cut for anyone interested.

IMG_20150426_100203First, I characterized the router: this is a linksys E2500V3 router, rated 12V and 1A. It plugs into a wall wart labeled “Hon Kwang” 12V, 1A.  With no load, the wall wart measured 11.97V. After plugging it into the router, the router goes through a boot process and initially draws 200mA and voltage is 11.82V. After about a minute, power draw goes up — maybe the radios being activated? At 350mA draw, the voltage dips to 11.65V and remains there. So, looks like it’s consuming a bit over 4 watts. Popping the top, there are a lot of surface mount components and a few large chips. With the wall adapter, these larger chips get pretty hot, but not too hot to touch (I have a VOM with thermal probe on order — not here yet, so the fingertip method for now).

First Attempt

Version 1 and 2 started with an 18volt laptop power adapter, with an LM317 serving to bring the voltage down towards the 13.8 needed to charge a lead acid battery. That’s a big drop, and the LM317 got pretty hot under load, so I decided to use a 16V laptop adapter (I have a big box of old adapters) — still enough overhead for the LM317 and less waste heat. I used a website to calculate the resistor values needed to bias the LM317 to put out about 14.1 volts (to yield 13.8 after Schottky diode drop) and then Hippy’s Happy Resistance Calculator to find a good combination for the non-standard value.

Perfboard layout
Perfboard layout

The core of the design uses diodes to OR together the output of the linear regulator and the battery — whichever is bigger wins, with the other sources being suppressed through a reverse-biased diode. The battery trickle charges through a current limiting resistor. Various sources (1, 2, 3) recommend trickle charging at something like C/100 to C/500. My battery is a 2.9Ah. I figured that it should never get below 11.8V — since power switchovers happen at most 2-3 times a day and last a minute. Taking worse case, say 10V, from 13.8V, so 4V difference, and aiming for about 5 mA trickle current, I went with a 680 ohm resistor (yielding 5.9 mA). This may be too low a value, as it takes quite a while to charge.

There are Schottky rather than standard rectifier diodes at both battery and main output for the sake of efficiency; more important for the battery than the wall-supplied power, though. In addition, there’s a  diode that isolates the charging battery. In principle, this one could be eliminated if protected the LM317 with a clamping diode. The LM317 will be damaged by back voltage, for example if wall power died and the unit ran from battery. A regular diode “backwards” across the LM317 would limit the regulator to 0.7V reverse current. However, I have no idea how the laptop adapter would like that, so I just side-stepped the whole issue by blocking the reverse pathway at the battery.

In the box.
In the box.

That’s enough to make a functional back-up, but it lacks any sort of status readout. An LED is easy enough to add, but it would also be nice to have an audio indication of when the unit is drawing on battery power and when the battery is approaching the point that further discharge will begin to harm it. The easiest solution was to add a microcontroller; not a big one, just one that could read some voltages and sound a piezo buzzer. I have a sleeve of ATTINY45 chips, so that’s what I used.

The ATTINY would fry at 13V, so I used a 5V regulator. If I’d though about it (more), I should have put the LED on the 5V circuit as well — it would have consumed less power at the same current as on the 13V side.

The ATTINY’s job is to monitor “mains” voltage and battery voltage. Voltages higher than 5V would harm the chip, so I read these values from voltage dividers that quarter the actual voltage. The ATTINY’s fuses were burnt to clock it at 8 Mhz, and it loops constantly, reading voltages via its ADCs. When battery exceeds mains, it beeps. If the battery voltage is less than 11.8 volts, it beeps “LO” in morse code to let you know it is getting desperate.

/* Program burned to ATTINY 45 on jjw1_275 */

//#define SERIAL_DEBUG

#define adcMainsPin 1 // Analog 1, i.e, physical pin 7
#define adcBatteryPin 2 // Analog 2, i.e., physical pin 3
#define buzzerPin 0 // Digital 0, i.e., physical pin 5
#define adcScaleFactor .01968
// pin 8 vcc
// pin 4 ground
// pin 1 (low reset) tied high
// pin 2, 6 n.c.


void setup() {
   #ifdef SERIAL_DEBUG
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  #endif
}

void loop() {
  int mainsADC; 
  int batteryADC;
  
  mainsADC = analogRead(adcMainsPin);
  #ifdef SERIAL_DEBUG
    Serial.print("Mains reading: ");
    Serial.print(mainsADC);
    Serial.print(" for a voltage of ");
    Serial.println(mainsADC * adcScaleFactor);
  #endif
  if (mainsADC < 615) { // if mains < 12V 
      batteryADC = analogRead(adcBatteryPin);
      #ifdef SERIAL_DEBUG
        Serial.print("Battery reading: ");
        Serial.print(batteryADC);
        Serial.print(" for a voltage of ");
        Serial.println(batteryADC * adcScaleFactor);
        Serial.println();
      #endif
      if (batteryADC < 605) {
        low(); // battery < 11.8V,deep discharge
      } else {
        chirp(); // on back up power, all okay
      }
  }
   delay(1000);              // wait for a second 
}

void chirp() {
  dit(2000);
}

void low () { //"LO" -- note 4000 hz is resonance for buzzer
  dit(4000);
  dah(4000);
  dit(4000);
  dit(4000);
  delay(300);
  dah(4000);
  dah(4000);
  dah(4000);
}

void dit(int freq) {
 tone(buzzerPin,freq);
 delay(100);
 noTone(buzzerPin);
 delay(30);
}

void dah(int freq) {
 tone(buzzerPin,freq);
 delay(300);
 noTone(buzzerPin);
 delay(30); 
}

I constructed the device on perf board. I paid some attention to heat and stuck a small heat sink on the LM317 and left room between it and surrounding electrolytic capacitors for good measure. For the incoming power connection, I chose an adapter that cannot accidentally plug into the router (16V would probably cook the router). For output, I used a powerpole connector at one end and the router barrel connector at the other. Since all my other power systems, batteries, etc., use Anderson powerpoles, it seemed natural to do so on the router. In a pinch, I could run the router off any battery or 12V power supply in the shack. Similarly, I used powerpole input for the battery connection.

When connected up in this manner, the LM317 is only slightly warm. The chips in the router do not seem to be hotter than they were running off the native wall wart.

Version 3: With microcontroller
Version 3: With microcontroller

Of note, when I actually put this design into action, it not only behaved as expected, but ran the router for four hours. It would have gone longer, but I couldn’t put up with the beeping. So, it’s good to know that in a pinch, if power didn’t come back up from the generator, I could go quite a while even on the smallest lead-acid battery in the house, and no doubt days if not weeks on an auto battery.

Second Attempt

The second design, euphemistically known as version 6, was an attempt to improve both efficiency and to simplify the design by omitting a microcontroller. I do believe it’s more efficient, but not necessarily simpler — the part count has gone up, but on the other hand, there’s nothing that requires programming.

The chief selling point of this design is that the battery does not need to pay the price in voltage drop through a diode. A p-channel mosfet serves as a blocking diode when on mains voltage, but when on battery power, the mosfet’s gate potential is made negative relative to source, which lowers the drain-source resistance to within a hair of nothing, i.e., the mosfet is “on”. As a result, the useful time on battery is extended.  The main trick here is to use the intrinsic (not always drawn, but very real) diode in the mosfet to block current towards the battery; this means that the mosfet’s drain is on the battery side; this may seem backwards, since most of the time, batteries connect to the source and a load connects to the drain. This still works, though, because there is a positive charge on the drain and, one diode drop lower in potential, on the source as well. All that matters is that the gate be more negative than the threshold value below the source for the mosfet to turn on. The one that I used came off a computer motherboard in a prior life and had no problem handling either power or voltage. I turned on its gate hard by pulling it down to ground, about ten volts below the source potential.

Note quite as built yet. Note the QRPme pads, which allowed me to use the salvage NDS9340A powertrench p-channel mosfet and LM324, both likely scavenged from a motherboard.
Note quite as built yet- but at least I know it works. Note the QRPme pads, which allowed me to use the salvage NDS9340A powertrench p-channel mosfet and LM324 from roadkill equipment.

The other twist in this design was to come up with features equivalent to the microcontroller circuit, particularly the audio alarms. I couldn’t figure out how to make the circuit beep in morse code, but I think I came up with a reasonable solution. I started with the venerable LM324 quad op amp.

Of the four op amps in the package, the most important one served as a comparator, providing fast switching between mains and battery. Both voltage sources were again read through voltage splitters to make sure the voltage fell in the middle of the common mode input specification. When battery voltage is higher than wall (i.e., power failure), output 1 goes high, turns on the n-channel mosfet, which in turn pulls down the gate of the p-channel mosfet turning it on. It’s just as important to turn it off, though, so the gate is also pulled up to the source voltage via resistor. That way, when wall power returns, the op amp output goes low, and the n-channel mosfet stops conducting, the p-channel mosfet will also turn off immediately. This prevents the battery from seeing the mains voltage without a current-limiting resistor.

The other op amps are involved with the audio alarms. Two are set up as oscillators [4, 5, 6], producing square wave output at half duty cycle; this is somewhat like rolling my own 555 timer. Timing is controlled by charge/discharge of a capacitor through a resistor. I set one up for one second, and the other to oscillate at 4000 hz, the resonant frequency of the piezo buzzer selected. The predicted values need some tweaking in application; I ended up with more like 3000 Hz than 4000 Hz, so I substituted a potentiometer and tuned the oscillator to 4000 Hz on an oscilloscope with frequency readout. I could also have done it “by ear” using audio spectrum analyzers available for android phones. The value for the resistor in the piezo driver circuit is taken from the manufacturer’s website.

For the buzzer to buzz, the 4000 Hz wave drives a n-channel mosfet on and off.  To beep, this drive is modulated by the 1 second oscillator, again through an n-channel mosfet. Finally, main comparator that determines whether the backup supply is on mains or battery also drives an n-channel mosfet. The three mosfets are arranged in series. For the buzzer to actuate, all of them must be on:  battery versus wall – high, one second timer – high, and 4000 Hz oscillator -high.

Finally, to indicate a lower battery stage (again, picking 11.8V), the output voltage is compared to a 5 volt standard. The last of the four LM324 op amps function as a comparator, and reads the supply output voltage through a divider set up such that 11.8V output would put the center point of the divider at 4.95 volts, which is about what the 78L05 puts out. Why a 78L05 and not a zener? Efficiency. All the zeners in my junque box had zener current ratings of around 20mA. Particularly when working at 13V, that power consumption was not attractive.

When the battery falls to 11.8V, this last comparator will also activate an n-channel mosfet. This continuous tone oscillator is “OR’d” into the speaker driver via diodes. Switching to battery alone triggers a one second duration beep, but a low battery is indicated by a constant tone. That’s a lot of 2n7000 n-channel mosfets – I count four, but at about seven cents each from suppliers like kitsandparts.com, I’m not too worried. I suppose one could use some other package of multiple mosfets in a single package or substitute bipolars, but I had the 2n7000’s and they seemed like a logical choice since I think of them as individual logic gates. In fact, the driver circuit for the buzzer could be viewed as a two input OR gate (low battery signal and one-second clock oscillator) and a three input AND gate (the output from the OR gate, the buzzer enable comparator output, and the 4000 Hz tone).

One consideration in either design would be to beef up the 5V side such that it also provides an output, e.g., for USB charging. Care would need to be taken, though, not to allow the load to drag down the 5V regulator output or the threshold for low battery, which is measured against the 5V standard, could go down.

Design number 6A: no battery voltage loss against a diode; also, no microcontroller.
Design number 6A: no battery voltage loss against a diode; also, no microcontroller.

Leave a Reply

Your email address will not be published. Required fields are marked *