All articles

    The Comparator: An ON/OFF Decision from an Analog Signal

    The comparator explained: comparing a voltage with a reference and outputting HIGH or LOW, an op-amp as a comparator vs the dedicated LM393 and its open-collector output, the input voltage range, chattering at the threshold and fixing it with hysteresis including the resistor calculation, the window comparator, and examples with a light sensor and a low-battery cut-off.

    Updated 5 min read

    Want an LED to switch on when it gets dark, or a fan to start when the temperature crosses a threshold, without a microcontroller? A comparator does that: it takes an analog signal and decides whether it's above or below a set level. It's the simplest "decision" a circuit can make, and it's hidden inside many things, from the 555 timer to sensor modules.

    When the signal crosses the reference voltage, the output goes HIGH
    When the signal crosses the reference voltage, the output goes HIGH

    The idea

    Two inputs:

    • + (non-inverting) and − (inverting).
    • If + is higher than − → the output is HIGH.
    • If + is lower than − → the output is LOW.

    Put the reference voltage (Vref) on one input, from a voltage divider or a potentiometer, and the signal on the other. If you want the opposite (output HIGH when the signal falls), just swap the two inputs.

    An op-amp or a comparator chip?

    An op-amp without feedback works as a comparator, but:

    • It isn't designed for it, so it's slower and its output sometimes doesn't reach the supply rails.
    • Some op-amps behave strangely when their two inputs are far apart, which happens all the time in comparison.
    • For anything serious, use a comparator chip such as the LM393 (two inside) or the LM339 (four).

    In numbers: the LM393 switches in about a microsecond, while an LM358 used as a comparator can take tens of microseconds. For slow sensors it makes no difference, but for detecting pulses or zero crossings it does.

    LM393: watch the output

    The output is open-collector: it can only pull LOW, and can't drive HIGH by itself.

    • You need a pull-up resistor (say 10k) from the output to the voltage you want.
    • The advantage: you can pull up to 3.3 V while the chip runs on 5 or 12 V, so the output suits an ESP32 directly.
    • And you can tie several comparator outputs to the same line with one resistor: any of them can pull it low (wired-OR).

    The input voltage range

    This catches many people out: the LM393's inputs must stay about 1.5 V below its supply. So on a 5 V supply, any signal above about 3.5 V won't be compared correctly. On the other hand, the inputs work fine down to zero. So if the signal can reach the supply, scale it down with a voltage divider first, and scale Vref by the same ratio.

    The problem: chattering at the threshold

    If the signal is very close to the threshold and has a little noise on it, the output will chatter rapidly between HIGH and LOW. The LED flickers and the relay clicks.

    Without hysteresis the output chatters as the signal crosses the threshold; with it, it switches once in each direction
    Without hysteresis the output chatters as the signal crosses the threshold; with it, it switches once in each direction

    The fix: hysteresis

    A positive feedback resistor from the output to the + input (a large resistor, say 1M):

    • It makes the threshold shift slightly depending on the output state.
    • There are now two thresholds: one for switching on and a lower one for switching off.
    • The signal must cross that gap for the output to change, and the chattering disappears.

    How do you calculate the gap?

    If Vref comes from a voltage divider on the + input, and the feedback resistor is Rf, the gap between the two thresholds is roughly:

    hysteresis width ≈ supply × (Rth ÷ Rf)

    where Rth is the divider's equivalent resistance (the two resistors in parallel). Example: a 10 kΩ / 10 kΩ divider on 5 V → Rth = 5 kΩ, and Rf = 100 kΩ → gap ≈ 5 × 5 ÷ 100 = 0.25 V around the 2.5 V point. Want a smaller gap? Increase Rf. The rule: make the gap larger than the noise on the signal, and small enough not to spoil the accuracy of the decision. (And the pull-up resistor must be small compared with Rf, so the calculation stays close.)

    The same idea is done in code with if (one threshold for on and one for off). See the watering example in soil moisture sensors, and the LED example in light and temperature sensors.

    Example: an LED that lights in the dark

    • An LDR with a resistor as a voltage divider → the signal.
    • A potentiometer → Vref, to set the threshold.
    • LM393 → the output with a pull-up → a transistor drives the LED.
    • Add hysteresis so dusk doesn't cause flicker.

    Many ready-made sensor modules (sound, light and gas) carry an LM393 and a potentiometer, which is exactly where the digital D0 output comes from. And note that most of them have no hysteresis, which is why D0 chatters when the sensor is near its threshold.

    Example: disconnecting the load when the battery runs down

    A 12 V lead-acid battery shouldn't go below about 11.5 V. A voltage divider scales the battery voltage to about 2.4 V at 11.5 V, and it's compared with a fixed 2.5 V reference from a TL431 chip or a zener. When the battery drops below the threshold, the output flips and a MOSFET disconnects the load. Hysteresis matters a lot here: when the load is disconnected, the battery voltage rises a little, so without it the load would cut out and come straight back. Make it reconnect at 12.5 V, for example.

    The window comparator

    Two comparators together: one checks whether the signal is above a level, the other whether it's below another level. Their open-collector outputs are tied to the same line. The result: the output is HIGH only when the signal is inside the window. Useful when you want to know a voltage or temperature is "in the normal range", such as an alert if the temperature leaves 20–30 °C.

    The comparator inside a microcontroller

    The Uno contains an analog comparator (on pins 6 and 7) that can trigger an interrupt when a signal crosses a level, without reading the ADC repeatedly. In ordinary code, analogRead with an if does the same job. The external chip wins when you need speed, no microcontroller, or voltages above 5 V.

    FAQ

    The output is always LOW and never changes? Most likely the pull-up resistor on the LM393's output is missing. Or the signal is above the input range (close to the supply).

    The output is HIGH when it should be LOW? The inputs are swapped, or Vref is wrong. Measure the voltage on both inputs with a multimeter and compare them yourself.

    Circuits Nerd
    © 2026 Circuits Nerd. All rights reserved.