Comparators and Schmitt Triggers
A thermostat must switch a heater off when a temperature sensor's voltage rises past . Connect the sensor to one input of a comparator and to the other, and the heater relay clicks dozens of times as the temperature creeps through the threshold: every millivolt of noise on the slow signal flips the output back and forth. A battery monitor, a zero-crossing detector and a sensor alarm all have the same problem. The cure is hysteresis: two thresholds instead of one, set by a little positive feedback. This lesson covers the comparator as a device, why an op-amp is a poor one, the Schmitt trigger in its two forms, its design equations, and the window comparator.
Comparator or op-amp?
Any op-amp run open-loop is a comparator: with saturates for any difference above a fraction of a millivolt. But an op-amp is designed for the opposite job, linear operation with negative feedback, and it shows:
| Op-amp used open-loop | Comparator | |
|---|---|---|
| Speed | slew-limited ( is per edge) and slow to recover from saturation | no compensation capacitor; propagation delay of nanoseconds to a few microseconds |
| Output | swings between its own rails | often open-drain / open-collector: a pull-up resistor to any logic supply sets the high level, and outputs can be wired together; push-pull versions too |
| Inputs | some parts misbehave with large differential voltages | built for large differential inputs |
| With negative feedback | stable | unstable: never use a comparator as an amplifier |
For slow, non-critical jobs (a battery-low LED) an op-amp works. For anything fast, or driving logic, use a comparator.
Positive feedback and hysteresis
Feed a fraction of the output back to the non-inverting input. Now the threshold depends on the output state: while the output is high, the threshold is pushed up; once the output switches low, the threshold drops. A noisy input that has just crossed the upper threshold must fall all the way back below the lower one before the output can switch again.
Inverting Schmitt trigger
The input drives the inverting input; the non-inverting input sits on a divider between a reference (through ) and the output (through ). With output levels and :
The output is high while ; it falls when the input rises past and rises again only when the input falls below . The centre of the window is shifted from unless is midway between and .
Non-inverting Schmitt trigger
The input drives the non-inverting input through , the output feeds back through , and the inverting input sits at . Switching happens when :
Its input current flows through into the output, so it loads the source; the inverting form does not.
How much hysteresis?
Make larger than the peak-to-peak noise on the input, with margin, but small enough that the thresholds are still accurate for the application. Two more tricks help: a small capacitor across speeds up the transition (it couples the edge back instantly), and an RC low-pass before the comparator reduces the noise that the hysteresis must cover. With an open-drain output, include the pull-up resistor in the feedback calculation: the high level comes through it.
Window comparator
Two comparators with thresholds and , one watching and one , and open-drain outputs tied together with one pull-up: the output is high only when the input is inside the window (a wired-AND). Battery and supply supervisors work this way.
Worked example: a threshold with of hysteresis
Given: a push-pull comparator on a single supply (, ), an inverting Schmitt trigger referred to a reference, ; noise on the input up to .
Find: for , and both thresholds.
Solve:
Check: the window is centred on because the reference is midway between the output levels, and is twice the noise. is an E24 value.
In Python: counting false transitions
import numpy as np
rng = np.random.default_rng(3)
t = np.linspace(0, 8e-3, 80001)
ramp = np.interp(t, [0, 4e-3, 8e-3], [1.5, 3.5, 1.5])
vin = ramp + rng.normal(0, 0.02, t.size) # 20 mV rms noise, about 120 mV pp
def schmitt(v, vref=2.5, width=0.0):
out, high = np.empty(v.size), True
for k, x in enumerate(v):
thr = vref + width / 2 if high else vref - width / 2
if high and x > thr:
high = False
elif not high and x < thr:
high = True
out[k] = 5.0 if high else 0.0
return out
for w in [0.0, 0.05, 0.1, 0.2]:
out = schmitt(vin, width=w)
n = int(np.count_nonzero(np.diff(out)))
print(f"hysteresis {w*1e3:5.0f} mV: {n:4d} output transitions")hysteresis 0 mV: 872 output transitions
hysteresis 50 mV: 120 output transitions
hysteresis 100 mV: 4 output transitions
hysteresis 200 mV: 2 output transitions
The ideal answer is two transitions (one on the way up, one on the way down). Without hysteresis the output chatters; once the window exceeds the noise's peak-to-peak value, the chatter stops.
Cross-check with SPICE: a behavioural comparator
ngspice's B source computes any expression; v(in) < v(p) ? 5 : 0 is an ideal comparator, and an RC of , gives it a output time constant so the solver can follow the edge. The input is a slow to triangle plus two sines of standing in for noise. wrdata writes both outputs to a file and awk counts their transitions.
cat > cmp.cir <<'EOF'
* plain comparator vs Schmitt trigger on a slow ramp with 'noise'
Vramp r 0 PULSE(1.5 3.5 0 4m 4m 1n 8m)
Vn1 r a SIN(0 20m 23k)
Vn2 a in SIN(0 20m 37k)
Vref ref 0 2.5
* plain: output high while vin < 2.5 V (inverting)
B1 o1x 0 V = v(in) < v(ref) ? 5 : 0
Ro1 o1x o1 100
Co1 o1 0 10p
* Schmitt: same comparator, + input from ref through 10k and from output through 240k
B2 o2x 0 V = v(in) < v(p) ? 5 : 0
Ro2 o2x o2 100
Co2 o2 0 10p
R1 ref p 10k
R2 o2 p 240k
.control
tran 0.2u 8m
wrdata cmp.txt v(o1) v(o2)
meas tran tsw_up when v(o2)=2.5 fall=1
meas tran tsw_dn when v(o2)=2.5 rise=1
.endc
.end
EOF
ngspice -b cmp.cir 2>&1 | grep -E '^tsw'
awk 'NR>1{a=($2>2.5);b=($4>2.5); if(NR>2){if(a!=pa)na++; if(b!=pb)nb++} pa=a;pb=b} END{print "plain comparator transitions:", na; print "Schmitt trigger transitions: ", nb}' cmp.txttsw_up = 2.153730e-03
tsw_dn = 6.138207e-03
plain comparator transitions: 16
Schmitt trigger transitions: 2
The Schmitt trigger switches once on the way up, at (where ramp plus noise first reach ; the ramp alone would get there at ), and once on the way down at (near , for the ramp alone). The plain comparator flips 16 times.
Interactive: where the threshold sits
The threshold divider is linear, so ee-netlist can show it: the comparator's output is represented by the source Vo, which you switch between its two states, and (the lab has no comparator element; it solves the divider for each state).
With the output high the lab shows ; switch it to and drops to . Lower to for a window.
Guided lab: chatter and its cure
Hand calculation. As in the worked example: , .
Simulation. The Python count, the SPICE deck and the lab.
What you would measure. Use a comparator with a push-pull output (or an open-drain one with a pull-up to , and include it in the calculation) on a single current-limited supply ( limit), decoupling. Make the reference from two resistors with a capacitor (its source resistance of adds to : the window becomes , or use a buffered reference). Drive the input with a triangle from to from the function generator, and add noise by summing in a small sine through a resistor. Without the scope (single-shot trigger on the output edge) shows bursts of edges at each crossing; with a single clean edge. Measure the thresholds by reading the triangle's value at each edge with the cursors: expect and within about (resistor tolerance, the comparator's offset and its output levels).
Hazards and safe setup. current-limited; keep the generator's output between and (use its offset control), because driving a comparator input outside its supply can latch it up or damage it.
Pitfalls
- Forgetting that the output levels set the thresholds. A push-pull output that only reaches under load, or a pull-up resistor in series, shifts .
- Negative feedback around a comparator. It oscillates.
- Hysteresis applied to the wrong input. Feedback to the inverting input is negative feedback.
- Ignoring the reference's source resistance. It adds to .
- Slow input edges at the comparator input without hysteresis, even without visible noise: the comparator can oscillate during the transition itself.
Practice
- An inverting Schmitt trigger has , , , , . Find the thresholds.
- A non-inverting Schmitt trigger on outputs, , needs thresholds at . Find .
- Why is a comparator with an open-drain output convenient for a window comparator?
Answer
- ; , .
- With : (and ), so (e.g. and ).
- Two open-drain outputs can be tied to one pull-up: either comparator can pull the line low, so the line is high only when both agree the input is inside the window.
Test yourself
Hysteresis in a comparator circuit is created by…
- negative feedback to the inverting input
- positive feedback to the non-inverting input
- a capacitor at the input
- a higher supply voltage
An inverting Schmitt trigger has , , and . The hysteresis width is…
Why should a comparator not be used as a linear amplifier with negative feedback?
- Its gain is too low
- It has no internal compensation, so the feedback loop is unstable and oscillates
- Its inputs draw too much current
- It cannot drive resistive loads
The noise on a slow input is peak-to-peak. A sensible hysteresis width is…
- about