Op-Amp Comparator: Turn an Analog Signal into a Clean Digital Output

Author: Abloom Instruction Date: November 17, 2026

An op-amp amplifier works in its linear region and gives you a bigger copy of the input. A comparator does something simpler and more decisive: it compares two voltages and answers with one of two levels — high or low. There is no in-between. That makes it the right part whenever an analog signal has to become a digital decision: a light sensor that switches a lamp on at dusk, a battery-low warning, a thermostat, or a zero-crossing detector. The circuit below uses the LM393, a dual comparator that costs almost nothing and is built for exactly this job.

Schematic of an LM393 comparator with an LDR divider on the non-inverting input, a 2.5 volt reference divider on the inverting input, a 4.7 kilo-ohm pull-up and a 100 kilo-ohm feedback resistor, beside a plot showing the output switching at 2.5 volts, with hysteresis splitting the thresholds to 2.39 and 2.61 volts

Figure 1: An LM393 comparator (a) — the LDR divider feeds the + input and a 10 kΩ / 10 kΩ divider sets the 2.5 V reference on the − input. The output switches at 2.5 V (b), and the 100 kΩ feedback resistor splits that into two thresholds, 2.61 V rising and 2.39 V falling. (Schematic, not to scale.)

How a comparator decides

The rule is one line: if the voltage on the + input is higher than the voltage on the − input, the output goes high; if it is lower, the output goes low. There is no feedback holding the inputs together the way an amplifier does, so the comparator runs open-loop and the output slams from one rail to the other. The LM393 has an open-collector output — its output transistor can only pull the output down to ground. To get a high level you add a pull-up resistor to the supply, here 4.7 kΩ to +5 V. When the transistor is off, the resistor pulls the output up to 5 V; when it is on, the output sits at the transistor's saturation voltage, about 0.2 V.

A dedicated comparator is better than a general-purpose op-amp used open-loop. A 741 as a comparator has to slew its whole output swing at 0.5 V/µs, so a 5 V swing takes 10 µs, and then it needs extra time to recover from saturation. The LM393 answers in about 1.3 µs, and its output is made to drive logic directly.

Worked example — a light detector that switches at dusk

Feed the + input from a divider made of an LDR and a 10 kΩ resistor, and set the reference with two 10 kΩ resistors from the 5 V supply:

Vref = 5 × 10 / (10 + 10) = 2.5 V

The sense voltage follows the light. In bright light the LDR is about 1 kΩ:

Vsense = 5 × 10 / (1 + 10) = 4.55 V

In the dark it is about 100 kΩ:

Vsense = 5 × 10 / (100 + 10) = 0.45 V

So in bright light Vsense (4.55 V) is above Vref (2.5 V) and the output is 5 V; in the dark it is below and the output is 0.2 V. The switch happens when the two are equal, which is when the LDR is 10 kΩ — the same value as the fixed resistor. When the output is low, the pull-up resistor carries (5 − 0.2) / 4700 = 1.02 mA, well inside the LM393's roughly 16 mA sinking ability.

Hysteresis — stopping the chatter at the threshold

Right at the threshold, a flicker of noise makes Vsense cross 2.5 V many times a second, and the output chatters with it. The cure is the 100 kΩ feedback resistor Rf from the output back to the + input. It pulls the + input slightly toward the output, so the threshold moves with the output state: higher when the output is high, lower when it is low. The gap between the two thresholds is set by the resistance the + input sees — at the threshold the LDR is 10 kΩ, so that resistance is 10 kΩ in parallel with 10 kΩ, or 5 kΩ:

Band = (5 − 0.2) × 5 / (5 + 100) = 4.8 × 5 / 105 = 0.23 V

So the output switches up at 2.5 + 0.115 = 2.61 V and back down at 2.5 − 0.115 = 2.39 V. In light terms, the LDR must fall to 9.13 kΩ to turn the output on, and rise back to 10.96 kΩ to turn it off — a small dead band that noise cannot cross. If you want a wider band, use a smaller Rf: 47 kΩ gives about 0.46 V.

Designing for another threshold — a battery-low warning

The same circuit watches a 12 V battery. Scale the battery down with a divider — 33 kΩ and 10 kΩ — and compare against the same 2.5 V reference. At 12 V the divider gives 12 × 10 / 43 = 2.79 V, above the reference, so the output stays high. At 10.5 V it gives 10.5 × 10 / 43 = 2.44 V, below the reference, so the output drops to 0.2 V. The exact trip point is where the divider equals 2.5 V: 2.5 × 43 / 10 = 10.75 V. Wire a red LED with a 330 Ω resistor from +5 V to the output pin and it lights when the battery falls below 10.75 V, drawing (5 − 2 − 0.2) / 330 = 8.5 mA.

Key points to remember

  • A comparator answers high when V+ is above V− and low when it is below — no linear region, no gain calculation, just a decision.
  • The LM393 has an open-collector output, so it needs a pull-up resistor (4.7 kΩ here); its low level is about 0.2 V and it can sink about 16 mA.
  • Worked example: a 10 kΩ / 10 kΩ divider sets Vref = 2.5 V; an LDR divider gives 4.55 V in bright light and 0.45 V in the dark, switching when the LDR is 10 kΩ; the pull-up carries 1.02 mA when the output is low.
  • A 100 kΩ feedback resistor adds 0.23 V of hysteresis — the output switches up at 2.61 V and down at 2.39 V — so noise near the threshold cannot make it chatter.
  • A 741 used as a comparator needs 10 µs just to slew 5 V; the LM393 answers in about 1.3 µs.
  • Design rule: a 33 kΩ / 10 kΩ divider against the same 2.5 V reference makes a battery-low warning that trips at 10.75 V, and a 330 Ω LED resistor gives 8.5 mA of warning current.