JOVANA
Explore Library Glossary Getting Started Three Levels Fields How it works Mission
Join the mission
All guides

The Comparator: Deciding Which Is Bigger

An op-amp with no feedback at all sounds broken — its giant gain just slams the output hard against a rail. But that slam is exactly what you want when the only question is 'which voltage is bigger?'. Meet the comparator: the part that turns a smooth analog voltage into one clean yes-or-no bit, and learn why it deserves its own chip rather than a borrowed op-amp.

From a smooth analog world to a single yes-or-no

Every rung so far has lived in the smooth analog world, where a voltage can be any value at all. A comparator stands right on the border to the digital world and answers one brutally simple question: is the voltage on my + input bigger than the voltage on my - input? The answer is a single bit — output HIGH for yes, output LOW for no. You already glimpsed this at the very end of the op-amp introduction: take an op-amp, give it no negative feedback at all, and the two golden rules collapse — instead of patiently holding its inputs equal, the output just slams to a rail. Here we build that on purpose and make it useful.

The arithmetic shows why the slam is unavoidable. Open-loop, the output obeys Vout = A times (V+ - V-) with A — the open-loop gain — enormous, and nothing feeding back to shrink the difference. With A = 100,000 and a 5 V supply, the output reaches its limit once the input difference is a mere 5 / 100,000 = 50 microvolts. A breath of difference pins it fully high or fully low. So a comparator is really a 1-bit decision maker — and that makes it the simplest analog-to-digital converter there is, the very 1-bit quantizer sitting at the heart of the flash and sigma-delta converters you met last rung.

Why a comparator — not just an op-amp run open-loop

It is tempting to grab a spare op-amp, skip the feedback resistors, and call it a comparator. On the bench it even half-works, which is the dangerous part. But it is genuinely the wrong part, for three honest reasons worth walking through.

  1. Speed. An op-amp is deliberately slowed down inside (its frequency compensation) so it stays stable when you DO wrap feedback around it. Run open-loop, its output crawls between the rails at its slew rate — a 0.5 V/us part needs about 10 us just to cross a 5 V swing — and worse, it recovers sluggishly after being driven into saturation. A true comparator has no compensation capacitor and is built to snap across in tens of nanoseconds.
  2. The output. An op-amp's output swings toward its analog supply rails — and often does not even reach them cleanly. That is not a tidy digital level your microcontroller can read. A comparator's output stage is designed to land squarely on valid logic levels, a clean 0 or 1 that the digital world accepts without argument.
  3. Input behavior. Many op-amps have internal clamp diodes or input stages that misbehave — even latch up or draw heavy current — when the two inputs differ by more than a volt or so. But a large, lasting difference between the inputs is exactly the normal condition for a comparator. A comparator's inputs are built to sit at wildly different voltages all day without complaint.

The lesson is bigger than this one chip: reading the datasheet for what a part was actually built to do, and picking the right component for the job, beats forcing a familiar part into a role it was never compensated for. An op-amp is compensated to stay calm under feedback; a comparator is built to be decisive without it. Same triangle on the schematic, opposite design goals.

An output built to talk to logic: open-collector and the pull-up

Here is a surprise that trips up everyone the first time: a great many comparators (the classic LM393 among them) do not drive their output HIGH at all. Their output is open-collector (open-drain on a MOSFET part) — the output transistor can only pull the node down to ground, sinking current; to go up it simply lets go and floats. So you must add a pull-up resistor from the output to whatever supply you want. That resistor is what creates the HIGH, and its supply chooses the HIGH level: tie it to 3.3 V and you get 3.3 V highs, tie it to 5 V and you get 5 V highs. You have just done free level shifting between two voltage domains.

   +5V
    |
  [10k]
    |                              +3.3V   (logic supply)
    +----(-) ref = 2.5 V             |
    |        \                     [ Rp ]   pull-up: makes the HIGH, sets the domain
  [10k]       >--[ comparator ]      |
    |        /   open-collector      |
   GND      /        output          |
   Vin ---(+)            \           |
                          +----------+------> OUT to microcontroller
                          |                   (clean 0 V or 3.3 V)
                         === internal output transistor
                          |  Vin > 2.5V -> transistor OFF -> pull-up lifts OUT to 3.3V (HIGH)
                         GND Vin < 2.5V -> transistor ON  -> OUT pulled down to ~0V  (LOW)
A bare-bones comparator: a divider sets the 2.5 V threshold, the open-collector output can only sink to ground, and the pull-up to 3.3 V both manufactures the HIGH level and shifts the result into the logic domain.

Sizing the pull-up is a small tug-of-war. Too large a value, fighting the stray capacitance of the wire and the next chip's input, rounds off the rising edge into a lazy RC curve — slow, exactly when a comparator's whole job is to be fast. Too small a value wastes current and, worse, may exceed the comparator's rated sink current when the output is LOW. With Rp = 4.7 k to 3.3 V the low-state sink is 3.3 / 4700 ≈ 0.7 mA, very comfortable; drop to 330 ohm and it is 10 mA, fine for many parts but a number you must check against the datasheet first.

Open-collector hides a bonus. Tie several comparator outputs to one shared pull-up and you get wired-OR: since any single output can drag the shared line to ground but none can force it high, the line is HIGH only when all of them let go — any one alarm tripping pulls the common bell low. This is also how shared digital buses behave, and guide 3 develops it fully. One more idea to carry forward: a well-designed logic family leaves a noise margin — a deliberate gap between the highest voltage still read as a valid 0 and the lowest read as a valid 1 — so a little noise on the wire cannot accidentally flip a 0 into a 1.

The chatter problem

There is a snake in the grass, and it bites the moment you use a bare comparator on a real signal. A comparator is brutally sensitive right at its threshold — remember, only 50 microvolts of difference fully decides it. So when the input crosses slowly, or carries even a few millivolts of noise riding along on it, the output does not make one clean transition. It flickers HIGH-LOW-HIGH-LOW many times as the fuzzy signal dances back and forth across the threshold. This is comparator chatter, and to a microcontroller counting edges, one true crossing looks like a dozen phantom events.

Picture it concretely. A slow sine creeps upward through your 2.5 V threshold while carrying 5 mV of fuzz on its back. Each time that fuzz happens to wiggle the instantaneous voltage a hair back below 2.5 V, the output snaps back LOW; the next wiggle up snaps it HIGH again. For the few milliseconds the signal lingers near the threshold, the output is a burst of digital hash instead of one decisive step. The cleaner and faster your crossing, the less you notice — but real-world signals are rarely either.

Honest fine print: offset, and why mixed-signal is hard

The threshold is never perfectly where your divider says it is. Every comparator carries an input offset voltage — a few millivolts of built-in imbalance between its two inputs that quietly shifts the real trip point up or down. If you are comparing two voltages that differ by only a millivolt, a 3 mV offset can hand you the wrong answer outright. The fix is honest and simple: either pick a low-offset part, or arrange your circuit so the signals you are comparing differ by far more than the offset, drowning it out.

Now the deeper honesty, the reason this whole rung exists: mixed-signal is hard. A comparator lives exactly on the analog/digital boundary, and that boundary is a hostile place. Its own output makes fast, full-swing digital edges, and those sharp edges gulp current from the supply and couple capacitively into nearby wires — the very crosstalk that can land right back on the comparator's own millivolt-sensitive input and re-trigger it. A circuit that fights its own output is a classic beginner heartbreak. This is the central difficulty of all mixed-signal design: fast digital edges spraying noise onto delicate analog nodes.

The defenses are the craft of the next rung, but worth naming now so you know what is coming: a decoupling capacitor right at the chip's power pins (a local water tank, so the fast current gulp does not sag the long supply line), a solid ground plane with the noisy digital output kept physically away from the quiet analog input, and the hysteresis from guide 2 so small disturbances can no longer flip the decision. And once your clean 1-bit decision exists, you usually need muscle to act on it — switching a real load through a transistor driver, a relay, or an opto-isolator across an isolation barrier — which is guide 5. From this one part the rung now unfolds: hysteresis (2), logic levels and pull-ups in depth (3), analog switches and sample-hold (4), and driving loads (5).