Comparator Basics

Comparators are quite handy devices that are often used to detect when a certain voltage threshold has been crossed. They are basically like an open-loop operational amplifier, but unlike op amps, are specifically designed for driving their output to “rail to rail”.

The simplest comparator just compares an input voltage to a reference voltage, setting its output high when the input voltage exceeds the reference voltage, and setting its output low when the input voltage falls below the reference voltage:

A basic comparator circuit

A basic comparator circuit

Note that the only additional components are the pull up resistor on the comparator’s output (the LM393 uses open collector outputs), and the resistor divider network to set the reference voltage. This works as expected, with the comparator’s output switching between its high and low states whenever the input signal crosses the 2.5V mark:

Comparator output (blue) vs input (yellow)

Comparator output (blue) vs input (yellow)

(Note that the input signal here is actually exceeding the maximum negative input voltage for the LM393 when run from a single supply, which likely accounts for the distorted waveform on the input pin; so yeah, don’t do that!)

However, noisy or slow-moving input signals can easily cause false triggering, resulting in many rapid pulses at the comparator’s output:

Noisy inputs cause false triggering

Noisy inputs cause false triggering

This is where hysteresis comes into play:

Hysteresis – the phenomenon in which the value of a physical property lags behind the changes causing it.

The idea is to set different high (Vth) and low (Vtl) thresholds so that small voltage changes on the input don’t cause the comparator’s output to flip back and forth. Such a scheme is commonly known as a Schmitt trigger:

Different high and low trigger voltages prevent false rising/falling edges

Different high and low trigger voltages prevent false rising/falling edges

Most comparators have some small hysteresis built in, but more pronounced effects can be accomplished with positive feedback:

Comparator with positive feedback

Comparator with positive feedback

The resistor R2 feeds some of the output signal back to the input pin, hence the voltage seen at the comparator’s input is actually a combination of the original input signal and the comparator’s output.

When the comparator output is low R2 acts as a pull-down resistor, and when the comparator’s output is high R2 acts as a pull-up resistor; the stronger R2 is, the harder the input signal needs to push in order to drive the comparator’s input voltage above or below the threshold voltage.

By changing the ratio of the resistor divider formed by R1 and R2, we can control how much effect the comparator’s output has on the voltage seen at the input. Larger values of R2 feed less of the output back to the input and vice versa, so for back-of-the-napkin calculations, larger R1/R2 ratios will provide a larger difference between the high (Vth) and low (Vtl) voltage thresholds.

The following formulas can be used to calculate the appropriate R1/R2 ratio and Vref voltage for the desired Vth and Vtl values (equations derived here):

\(
\begin{aligned}
\frac{R1}{R2} = \frac{\mathbf{V}_{TH} – \mathbf{V}_{TL}}{\mathbf{V}_{CC} – \mathbf{V}_{SS}}\\
\end{aligned}
\)

\(
\begin{aligned}
\mathbf{V}_{ref} = \frac{\mathbf{V}_{SS} + (\mathbf{V}_{TH} \times \frac{\mathbf{V}_{CC} – \mathbf{V}_{SS}}{\mathbf{V}_{TH} – \mathbf{V}_{TL}})}{\frac{\mathbf{V}_{CC} – \mathbf{V}_{SS}}{\mathbf{V}_{TH} – \mathbf{V}_{TL}} + 1} \\
\end{aligned}
\)

Let’s give a good wide margin of error and set Vtl = 2.5V and Vth = 3.5V:

\(
\begin{aligned}
\frac{R1}{R2} = \frac{3.5 – 2.5}{5 – 0} = \frac{1}{5} = \frac{2M}{10M} \\
\end{aligned}
\)

\(
\begin{aligned}
\mathbf{V}_{ref} = \frac{0 + (3.5 \times \frac{5 – 0}{3.5 – 2.5})}{\frac{5 – 0}{3.5 – 2.5} + 1} = 2.92V \\
\end{aligned}
\)

Selecting voltage divider resistors of 5.6K and 3.9K provides a Vref very close to our desired 2.92V, producing our final schematic:

Comparator with calculated feedback / Vref resistor values

Comparator with calculated feedback / Vref resistor values

The difference between the comparator’s high and low output states with respect to the input signal is quite clear:

Comparator output with hysteresis applied

Comparator output with hysteresis applied

But there’s a problem. A closer look reveals severe oscillation during the falling edge of the comparator’s output:

Comparator output oscillation

Comparator output oscillation

Analog Device’s “Ask the Applications Engineer #5” mentions several potential causes of comparator oscillations, including:

Another cause of the oscillation may be a high source impedance and stray capacitance to the input. Even a few thousand ohms of source impedance and picofarads of stray capacitance can cause unruly oscillations.

Hmmm, a high input impedance (e.g., our 2M ohm R1 resistor) coupled with the stray capacitance from a solderless breadboard?

DUT in a solderless breadboard

DUT in a solderless breadboard

Check and mate. Deadbugging the circuit on a copper PCB completely eliminates the falling edge oscillations, providing a nice clean output from the comparator:

Deadbugged DUT

Deadbugged DUT

Comparator output with clean falling edge

Comparator output with clean falling edge

One thought on “Comparator Basics

Leave a Reply

Your email address will not be published. Required fields are marked *