TExcelTAD
Home › Analog Electronics: Diodes, Transistors & Op-Amps › Comparators and Schmitt Triggers

Comparators and Schmitt Triggers

A thermostat must switch a heater off when a temperature sensor's voltage rises past 2.5 V2.5\ \text{V}. Connect the sensor to one input of a comparator and 2.5 V2.5\ \text{V} 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: vout=A0(v+−v−)v_\text{out} = A_0 (v_+ - v_-) with A0=105A_0 = 10^5 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 (0.5 V/μs0.5\ \text{V}/\mu\text{s} is 10 μs10\ \mu\text{s} per 5 V5\ \text{V} 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.

- + +5 V 0 V cmp vin 2.5 V ref R1 10 kOhm R2 240 kOhm out p 2.2 2.4 2.6 2.8 vin (V) 5 0 vout (V) rising: trips at 2.6 V falling: trips at 2.4 V hysteresis 0.2 V

Inverting Schmitt trigger

The input drives the inverting input; the non-inverting input sits on a divider between a reference VrefV_\text{ref} (through R1R_1) and the output (through R2R_2). With output levels VOHV_{OH} and VOLV_{OL}:

VT±=VrefR2+VOH,OLR1R1+R2,ΔVH=VT+−VT−=(VOH−VOL)R1R1+R2V_{T\pm} = \frac{V_\text{ref} R_2 + V_{OH,OL} R_1}{R_1 + R_2}, \qquad \Delta V_H = V_{T+} - V_{T-} = (V_{OH} - V_{OL})\frac{R_1}{R_1 + R_2}

The output is high while vin<VT+v_\text{in} < V_{T+}; it falls when the input rises past VT+V_{T+} and rises again only when the input falls below VT−V_{T-}. The centre of the window is shifted from VrefV_\text{ref} unless VrefV_\text{ref} is midway between VOLV_{OL} and VOHV_{OH}.

Non-inverting Schmitt trigger

The input drives the non-inverting input through R1R_1, the output feeds back through R2R_2, and the inverting input sits at VrefV_\text{ref}. Switching happens when v+=Vrefv_+ = V_\text{ref}:

VT±=Vref(1+R1R2)−VOL,OHR1R2,ΔVH=(VOH−VOL)R1R2V_{T\pm} = V_\text{ref}\left(1 + \frac{R_1}{R_2}\right) - V_{OL,OH}\frac{R_1}{R_2}, \qquad \Delta V_H = (V_{OH} - V_{OL})\frac{R_1}{R_2}

Its input current flows through R1R_1 into the output, so it loads the source; the inverting form does not.

How much hysteresis?

Make ΔVH\Delta V_H 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 R2R_2 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 VLV_L and VHV_H, one watching vin>VLv_\text{in} > V_L and one vin<VHv_\text{in} < V_H, 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 2.5 V2.5\ \text{V} threshold with 0.2 V0.2\ \text{V} of hysteresis

Given: a push-pull comparator on a single 5 V5\ \text{V} supply (VOL=0V_{OL} = 0, VOH=5 VV_{OH} = 5\ \text{V}), an inverting Schmitt trigger referred to a 2.5 V2.5\ \text{V} reference, R1=10 kΩR_1 = 10\ \text{k}\Omega; noise on the input up to 100 mVpp100\ \text{mV}_\text{pp}.

Find: R2R_2 for ΔVH=0.2 V\Delta V_H = 0.2\ \text{V}, and both thresholds.

Solve:

R1R1+R2=0.2 V5 V=0.04⇒R2=24R1=240 kΩVT+=2.5 V⋅240+5 V⋅10250=2.60 V,VT−=2.5 V⋅240250=2.40 V\begin{aligned} \frac{R_1}{R_1 + R_2} &= \frac{0.2\ \text{V}}{5\ \text{V}} = 0.04 \quad\Rightarrow\quad R_2 = 24 R_1 = 240\ \text{k}\Omega \\ V_{T+} &= \frac{2.5\ \text{V} \cdot 240 + 5\ \text{V} \cdot 10}{250} = 2.60\ \text{V}, \qquad V_{T-} = \frac{2.5\ \text{V} \cdot 240}{250} = 2.40\ \text{V} \end{aligned}

Check: the window is centred on 2.5 V2.5\ \text{V} because the reference is midway between the output levels, and 0.2 V0.2\ \text{V} is twice the noise. 240 kΩ240\ \text{k}\Omega is an E24 value.

In Python: counting false transitions

python · try it yourself
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 100 Ω100\ \Omega, 10 pF10\ \text{pF} gives it a 1 ns1\ \text{ns} output time constant so the solver can follow the edge. The input is a slow 1.51.5 to 3.5 V3.5\ \text{V} triangle plus two sines of 20 mV20\ \text{mV} standing in for noise. wrdata writes both outputs to a file and awk counts their transitions.

bash · try it yourself
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.txt
tsw_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 2.15 ms2.15\ \text{ms} (where ramp plus noise first reach 2.6 V2.6\ \text{V}; the ramp alone would get there at 2.2 ms2.2\ \text{ms}), and once on the way down at 6.14 ms6.14\ \text{ms} (near 2.4 V2.4\ \text{V}, 6.2 ms6.2\ \text{ms} 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, 00 and 5 V5\ \text{V} (the lab has no comparator element; it solves the divider for each state).

node voltages (to ground 0)ref2.500 Vp2.600 Vo5.000 VnetlistVref ref 0 2.5R1 ref p 10kR2 p o 240kVo o 0 5element currents (node 1 to node 2 through it)Vrefref>010.0 uAR1ref>p-10.0 uAR2p>o-10.0 uAVoo>0-10.0 uA

With the output high the lab shows V(p)=2.600 VV(p) = 2.600\ \text{V}; switch it to 00 and V(p)V(p) drops to 2.400 V2.400\ \text{V}. Lower R2R_2 to 90 kΩ90\ \text{k}\Omega for a 0.5 V0.5\ \text{V} window.

Guided lab: chatter and its cure

Hand calculation. As in the worked example: VT+=2.60 VV_{T+} = 2.60\ \text{V}, VT−=2.40 VV_{T-} = 2.40\ \text{V}.

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 10 kΩ10\ \text{k}\Omega pull-up to 5 V5\ \text{V}, and include it in the calculation) on a single 5 V5\ \text{V} current-limited supply (30 mA30\ \text{mA} limit), 100 nF100\ \text{nF} decoupling. Make the 2.5 V2.5\ \text{V} reference from two 10 kΩ10\ \text{k}\Omega resistors with a 1 μF1\ \mu\text{F} capacitor (its source resistance of 5 kΩ5\ \text{k}\Omega adds to R1R_1: the window becomes 5⋅15/255=0.29 V5 \cdot 15/255 = 0.29\ \text{V}, or use a buffered reference). Drive the input with a 1 Hz1\ \text{Hz} triangle from 1.51.5 to 3.5 V3.5\ \text{V} from the function generator, and add noise by summing in a small 20 kHz20\ \text{kHz} sine through a 100 kΩ100\ \text{k}\Omega resistor. Without R2R_2 the scope (single-shot trigger on the output edge) shows bursts of edges at each crossing; with R2R_2 a single clean edge. Measure the thresholds by reading the triangle's value at each edge with the cursors: expect 2.602.60 and 2.40 V2.40\ \text{V} within about ±30 mV\pm 30\ \text{mV} (resistor tolerance, the comparator's offset and its output levels).

Hazards and safe setup. 5 V5\ \text{V} current-limited; keep the generator's output between 00 and 5 V5\ \text{V} (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 4.8 V4.8\ \text{V} under load, or a pull-up resistor in series, shifts VT+V_{T+}.
  • 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 R1R_1.
  • Slow input edges at the comparator input without hysteresis, even without visible noise: the comparator can oscillate during the transition itself.

Practice

  1. An inverting Schmitt trigger has VOH=12 VV_{OH} = 12\ \text{V}, VOL=0V_{OL} = 0, Vref=6 VV_\text{ref} = 6\ \text{V}, R1=1 kΩR_1 = 1\ \text{k}\Omega, R2=100 kΩR_2 = 100\ \text{k}\Omega. Find the thresholds.
  2. A non-inverting Schmitt trigger on ±5 V\pm 5\ \text{V} outputs, Vref=0V_\text{ref} = 0, needs thresholds at ±0.5 V\pm 0.5\ \text{V}. Find R1/R2R_1/R_2.
  3. Why is a comparator with an open-drain output convenient for a window comparator?
Answer
  1. ΔVH=12⋅1/101=0.119 V\Delta V_H = 12 \cdot 1/101 = 0.119\ \text{V}; VT+=(6⋅100+12⋅1)/101=6.059 VV_{T+} = (6 \cdot 100 + 12 \cdot 1)/101 = 6.059\ \text{V}, VT−=600/101=5.941 VV_{T-} = 600/101 = 5.941\ \text{V}.
  2. With Vref=0V_\text{ref} = 0: VT+=−VOLR1/R2=5R1/R2=0.5 VV_{T+} = -V_{OL} R_1/R_2 = 5 R_1/R_2 = 0.5\ \text{V} (and VT−=−0.5 VV_{T-} = -0.5\ \text{V}), so R1/R2=0.1R_1/R_2 = 0.1 (e.g. 10 kΩ10\ \text{k}\Omega and 100 kΩ100\ \text{k}\Omega).
  3. 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…

  1. negative feedback to the inverting input
  2. positive feedback to the non-inverting input
  3. a capacitor at the input
  4. a higher supply voltage

An inverting Schmitt trigger has VOH=5 VV_{OH} = 5\ \text{V}, VOL=0V_{OL} = 0, R1=10 kΩR_1 = 10\ \text{k}\Omega and R2=240 kΩR_2 = 240\ \text{k}\Omega. The hysteresis width is…

  1. 0.1 V0.1\ \text{V}
  2. 0.2 V0.2\ \text{V}
  3. 0.21 V0.21\ \text{V}
  4. 2.5 V2.5\ \text{V}

Why should a comparator not be used as a linear amplifier with negative feedback?

  1. Its gain is too low
  2. It has no internal compensation, so the feedback loop is unstable and oscillates
  3. Its inputs draw too much current
  4. It cannot drive resistive loads

The noise on a slow input is 50 mV50\ \text{mV} peak-to-peak. A sensible hysteresis width is…

  1. 5 mV5\ \text{mV}
  2. 25 mV25\ \text{mV}
  3. about 100 mV100\ \text{mV}
  4. 5 V5\ \text{V}