A single Li-ion cell is happy only inside a narrow voltage window: charge it past 4.2 V and it degrades fast, discharge it below 3.0 V and it may never recover. A voltmeter tells you the voltage, but a window comparator watches it for you — two comparators, one for each edge, wired together so a green LED means "inside the window" and a red LED means "outside". This guide builds that circuit around the LM393 dual comparator, works out both thresholds and the hysteresis that stops the LEDs flickering at the edges, and shows the one mistake that leaves the green LED almost dark.
Figure 1: (a) The window comparator — U1A watches the 4.2 V top edge, U1B watches the 3.0 V bottom edge, and the wired-AND output lights green only when both agree the cell is inside the window. (b) The transfer chart: the output is high inside the window and low outside, with hysteresis at both edges. (LM393 figures are from the onsemi datasheet, LM393/D.)
How the window works
The LM393 is a dual comparator with open-collector outputs, which is exactly what this circuit wants: two outputs can be tied together with a single pull-up resistor, and the common node goes high only when both outputs are off. The battery voltage is first scaled down by a sense divider, R1 = 6.8 kΩ and R2 = 10 kΩ, so the comparators work with a smaller, safer voltage. The divider ratio is 10 / (6.8 + 10) = 0.59524, so the sense voltage is 4.2 × 0.59524 = 2.500 V at the top of the window and 3.0 × 0.59524 = 1.786 V at the bottom. The high comparator (U1A) compares the sense voltage against a 2.5 V reference and pulls its output low when the battery goes over 4.2 V. The low comparator (U1B) compares the sense voltage against a 1.786 V reference and pulls its output low when the battery falls under 3.0 V. Both references come from the 5 V rail: 2.5 V from a 10 kΩ / 10 kΩ divider (5 × 10 / 20 = 2.5 V) and 1.786 V from an 18 kΩ / 10 kΩ divider (5 × 10 / 28 = 1.786 V). The red LED runs from +5 V through 330 Ω to the common node, so it lights whenever either comparator pulls the node low. The green LED is driven by a BC547 transistor (Q1) from the same node, so it lights only when the node is high — both comparators off, cell inside the window. All four comparator inputs stay below 2.56 V, comfortably inside the LM393's common-mode range of 0 to Vcc − 1.5 V = 3.5 V on a 5 V supply.
Worked example — thresholds and hysteresis
Without hysteresis the LEDs would flicker madly as the battery voltage sat exactly on a threshold, because a few millivolts of noise would flip the comparators back and forth. Each comparator gets a 1 MΩ feedback resistor from its output to its non-inverting (+) input, which separates the trip point from the reset point. Work it out properly, and two details matter. First, the LM393's output low is not 0 V — its saturation voltage Vsat is 250 mV typical at 4 mA (400 mV maximum), so the output swings between 0.25 V and 5 V, a swing of 4.75 V, not 5 V. Second, the hysteresis band is not centred on the nominal threshold. On the high side, the reference node sees a source resistance of 10 kΩ || 10 kΩ = 5 kΩ. With the output high, the feedback pulls the reference node up to (2.5 × 1 MΩ + 5 × 5 kΩ) / 1.005 MΩ = 2.51244 V; with the output low it is pulled down to (2.5 × 1 MΩ + 0.25 × 5 kΩ) / 1.005 MΩ = 2.48881 V. That is a 23.6 mV band at the reference node, or 23.6 mV / 0.59524 = 39.7 mV at the battery. So the high side trips at 2.51244 / 0.59524 = 4.221 V on a rising battery and resets at 2.48881 / 0.59524 = 4.181 V on a falling battery — the band is 4.18–4.22 V, not 4.20 V ± 20 mV. On the low side the feedback goes to the sense node, whose source resistance is 6.8 kΩ || 10 kΩ = 4.05 kΩ. Solving the same node equation against the 1.786 V reference gives a falling trip at 2.978 V and a rising reset at 3.010 V, a 32.3 mV band. The effective window is therefore 2.98–4.22 V. Note the over-voltage trip sits 21 mV above the 4.20 V charge limit — fine for a monitor, since the charger itself still stops at 4.20 V; the comparator is the alarm, not the charger.
The green LED needs a transistor
Here is the trap in the obvious version of this circuit. If you hang the green LED straight from the wired-AND node to ground through 330 Ω, the current has to come through the 10 kΩ pull-up resistor: (5 − 2.0) / (10,000 + 330) = 0.29 mA — the LED barely glows. The fix is Q1: the node drives the BC547 base through 10 kΩ (base current (5 − 0.7) / 10 kΩ = 0.43 mA), and the transistor switches the LED from the 5 V rail, so the green LED gets (5 − 2.0) / 330 = 9.09 mA. The red LED gets (5 − 1.8 − 0.25) / 330 = 8.94 mA, and the output transistor of the LM393 also sinks the pull-up current (5 − 0.25) / 10 kΩ = 0.475 mA, for a total sink of 9.41 mA — well under the LM393's 20 mA maximum output sink current. With the green LED on, the whole circuit draws about 10.6 mA: the LED dominates, the dividers and the LM393's own 0.4 mA supply current are a small part of it.
Errors that move the edges
Three small errors move the thresholds, and the biggest is the one beginners skip. The LM393's input bias current is 25 nA typical and 250 nA maximum at 25 °C, and it flows through the 4.05 kΩ source resistance of the sense node: 25 nA × 4.05 kΩ = 0.10 mV at the sense node, or 0.17 mV at the battery; at the 250 nA maximum that is 1.01 mV at the sense node, or 1.70 mV at the battery. But the input offset voltage is 5 mV maximum for the plain LM393, and that appears directly at the comparator input: 5 mV / 0.59524 = 8.4 mV at the battery — five times the worst bias-current error. The divider itself is a steady drain on the cell: 4.2 V / 16.8 kΩ = 250 µA, which is 250 µA × 24 h = 6 mAh per day, or 0.23% of a 2600 mAh cell per day. Scaling the divider to 68 kΩ / 100 kΩ cuts that to 25 µA (0.6 mAh/day, 0.023%/day), but the source resistance grows tenfold to 40.5 kΩ, so the worst-case bias-current error grows to 250 nA × 40.5 kΩ / 0.59524 = 17.0 mV at the battery. For a monitor that guards a 1.2 V window, the 6.8 kΩ / 10 kΩ divider with its 250 µA drain is the better trade; the high-value divider only wins if the cell sits unmonitored for months. If you need tighter edges than ±8 mV, use the LM393A (2 mV maximum offset) or trim the reference dividers.
Second example — 3.8 V and 2.9 V
Check the circuit at two real battery voltages. At 3.8 V the sense voltage is 3.8 × 0.59524 = 2.262 V. The high comparator sees 2.262 V against its 2.512 V reference node (output high, so the node is at its upper value) — sense below reference, output stays high. The low comparator sees 2.262 V against 1.786 V — sense above reference, output stays high. Both outputs high, the common node is pulled to 5 V, green on, red off: the cell is inside the window, as it should be. At 2.9 V the sense voltage is 2.9 × 0.59524 = 1.726 V. The low comparator now sees 1.726 V against 1.786 V — sense below reference, so its output pulls low, the common node falls to 0.25 V, the red LED lights and Q1 turns the green LED off. (For completeness, at 4.3 V the sense voltage is 2.560 V, above the high comparator's 2.512 V node, so the high side trips and the red LED lights again.) The circuit reads the cell correctly at all three points, with about 30–40 mV of hysteresis keeping the changeover clean.
Key points to remember
- Worked example: sense divider 6.8 kΩ / 10 kΩ gives ratio 0.59524 — 2.500 V at 4.2 V, 1.786 V at 3.0 V. References from 5 V: 10 kΩ / 10 kΩ = 2.5 V, 18 kΩ / 10 kΩ = 1.786 V. Open-collector outputs wired-AND with one 10 kΩ pull-up: the node is high only inside the window.
- Hysteresis (Rf = 1 MΩ): high side trips at 4.221 V rising and resets at 4.181 V falling (39.7 mV band); low side trips at 2.978 V falling and resets at 3.010 V rising (32.3 mV band). The band is not centred on the nominal threshold, and Vsat = 250 mV (not 0 V) sets the real output swing of 4.75 V.
- LEDs: green 9.09 mA only through the BC547 switch — straight off the pull-up node it would get 0.29 mA. Red 8.94 mA; the LM393 sinks 9.41 mA total, under its 20 mA maximum. Base current 0.43 mA.
- Errors: bias current 25 nA typ / 250 nA max gives at most 1.70 mV at the battery with the 6.8 kΩ / 10 kΩ divider, but the 5 mV maximum offset voltage gives 8.4 mV — offset dominates. Divider drain 250 µA = 6 mAh/day (0.23%/day of 2600 mAh); 68 kΩ / 100 kΩ cuts it to 25 µA but pushes the bias error to 17.0 mV.
- Checks: 3.8 V → Vsense 2.262 V, both outputs high, green on. 2.9 V → Vsense 1.726 V, low side trips, red on. 4.3 V → Vsense 2.560 V, high side trips, red on. LM393 (onsemi LM393/D): supply 2–36 V, common-mode 0 to Vcc − 1.5 V, Vsat 250 mV typ at 4 mA, sink 20 mA max.
