Comparator Circuits: Threshold Detection and Schmitt Hysteresis
A comparator answers a single question: is voltage A higher than voltage B? Its output is a digital-style high or low. Comparators are used in battery-low alarms, light- and temperature-activated switches, zero-crossing detectors and inside ADCs. They look like op-amps, but they are built for a different job. This tutorial explains how to use a dedicated comparator, why noisy inputs cause output chatter, and how to calculate hysteresis to cure it.
How a Comparator Works
If V+ > V- : output HIGH If V+ < V- : output LOW (no feedback network: the device runs open-loop)
One input carries the signal and the other a reference voltage, often from a resistor divider. Connecting the signal to the + input makes the output go high when the signal rises above the reference; connecting it to the - input inverts that behaviour.
Comparator or op-amp?
| Feature | Op-amp (e.g. LM358) | Comparator (e.g. LM393) |
|---|---|---|
| Designed for | Linear operation with negative feedback | Open-loop switching |
| Output stage | Push-pull | Often open-collector, needs pull-up |
| Recovery from saturation | Slow | Fast |
| Speed | Limited by slew rate | Response time in the order of a microsecond for the LM393 |
| Output logic level | Set by its own supply | Set by the pull-up voltage, can differ from supply |
An op-amp can be used as a slow comparator in simple projects, but a real comparator switches faster and cleaner. The LM393 is a dual comparator that runs from a single supply between about 2 V and 36 V, and its input range includes ground.
The Open-Collector Output
The LM393 output is a transistor that can only pull the output low. When the output should be high, the transistor turns off and the output floats, so you must add a pull-up resistor to the voltage you want as the logic high. This is useful: a comparator powered from 12 V can produce a clean 3.3 V or 5 V logic signal simply by connecting its pull-up to that rail. Several open-collector outputs can also share one pull-up to form a wired-AND.
A pull-up of 1 kΩ to 10 kΩ is typical. Lower values give faster rising edges but more current when the output is low: with 1 kΩ to 5 V, the output transistor sinks 5 mA.
Chatter and the Schmitt Trigger
Real signals contain noise. If a slowly rising signal sits near the threshold, noise makes it cross back and forth many times, and the output switches rapidly. A relay connected to such a comparator buzzes. The fix is hysteresis: two thresholds instead of one. The output switches high-to-low at an upper threshold and low-to-high at a lower one, so small noise cannot flip it back. A comparator with hysteresis is called a Schmitt trigger.
Hysteresis is created with positive feedback: a resistor from the output to the + input, so that the output state shifts the reference slightly.
Vcc Vcc
| |
[R1] [Rpu] pull-up
| |
+------+--------(+) |
| | LM393 -+---- Vout
[R3] [R2] Vin---(-) |
| | |
| GND |
+-------------------------+
Output HIGH (R3 effectively in parallel with R1):
VTH = Vcc x R2 / (R2 + R1||R3)
Output LOW (R3 effectively in parallel with R2):
VTL = Vcc x (R2||R3) / (R1 + R2||R3)
Hysteresis = VTH - VTLThese formulas assume the pull-up resistor is much smaller than R3, so the output high level is close to Vcc.
Worked example
Vcc = 5 V, R1 = R2 = 10 kΩ, R3 = 100 kΩ, Rpu = 1 kΩ. Without R3 the threshold would be 2.5 V.
R1 || R3 = (10 x 100) / 110 = 9.09 kΩ VTH = 5 x 10 / (10 + 9.09) = 2.62 V R2 || R3 = 9.09 kΩ VTL = 5 x 9.09 / (10 + 9.09) = 2.38 V Hysteresis = 2.62 - 2.38 = 0.24 V
Because the signal goes to the - input, the output goes low when Vin rises above 2.62 V and returns high only when Vin falls below 2.38 V. Any noise smaller than 0.24 V peak-to-peak can no longer cause chatter. For more hysteresis, reduce R3; with R3 = 47 kΩ the thresholds become about 2.74 V and 2.26 V.
Practical Applications
- Battery-low indicator: compare a divided battery voltage with a Zener or TL431 reference; add hysteresis so the alarm does not flicker as the battery recovers under changing load.
- Dark-activated light: an LDR divider on one input and an adjustable potentiometer reference on the other.
- Thermostat: an NTC thermistor divider against a set-point; hysteresis stops the heater relay cycling rapidly.
- Square-wave shaping: clean up a slow or noisy sine wave into sharp edges for counting.
Troubleshooting
- Output never goes high, or floats at a random level: the pull-up resistor is missing.
- Output oscillates near the threshold: add hysteresis and a 100 nF decoupling capacitor near the IC.
- Thresholds differ from calculation: the pull-up is not small compared with R3, or the output high level is below Vcc.
- Comparator ignores inputs near Vcc: the LM393 input range only extends to about 1.5 V below Vcc; keep signals within it.
Summary
A comparator turns an analogue comparison into a clean logic level. Dedicated parts like the LM393 switch quickly but need a pull-up resistor on their open-collector output. Add positive feedback to create two thresholds, calculate them with the parallel-resistor formulas, and choose the hysteresis to exceed the noise on your signal. This single technique fixes most 'flickering relay' problems in sensor projects.