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:

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:

(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:

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:

Most comparators have some small hysteresis built in, but more pronounced effects can be accomplished 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:

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

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

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?

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

Why is the input waveform not distorted when you put in hysteresis?

Cheers for the nice write-ups on here.

Wow, I’m really late with this reply, sorry!

To answer your question, the comparator’s output *does* affect the input signal. However, assuming the signal source has a relatively low output impedance, this effect is negligible with the large feedback resistors used here.

Let’s say that our signal source has a larger source impedance of 10K ohms, and we selected very small feedback resistor values of 2 ohms and 10 ohms. The comparator’s output would then have a significant effect on the source signal and it would in fact be severely distorted!

Another thing to consider is the high output state of the comparator comes from the 3.9K ohm pull-up resistor in this case. This is effectively in series with the R2 feedback resistor. Since 3.9K ohms is very very small compared to the selected 10M ohm R2 resistor in the schematic, it can be ignored. However, if we selected R2 to be, say, 10K ohms, the effective R2 resistance would be 13.9K ohms, and would result in a noticeable error in our calculated threshold voltages.