How an Operational Amplifier (Op-Amp) Comparator Works.

Before we start dealing with the comparator, let's recall what an operational amplifier (op-amp) is. An operational amplifier has five pins and is shown on schematics as a triangle, as in the figure below.
How an Operational Amplifier (Op-Amp) Comparator Works.

Let's take a closer look at the pins:
  • two pins for connecting power, the positive and negative supply voltage;
  • two inputs, one non-inverting, labeled V+, and one inverting, labeled V-;
  • one output, labeled Vout;

Most likely, someone who hasn't been acquainted with an operational amplifier before will wonder what inverting and non-inverting inputs are; let's look at this with an example.
How an Operational Amplifier (Op-Amp) Comparator Works.

The figure above shows that if the voltage at the non-inverting input is higher than at the inverting input, the output will be at the positive supply voltage.
How an Operational Amplifier (Op-Amp) Comparator Works.

If, on the contrary, the voltage at the inverting input is higher than at the non-inverting input, the output will be at the negative supply voltage.
Essentially, we've looked at how a comparator works. Comparator comes from the English word compare: it compares two voltages and, depending on which input is higher, sets the output to the positive or negative supply voltage. We can also say that a comparator is an op-amp circuit without negative feedback, with a high gain. Negative feedback means connecting the inverting input to the output, directly or through an electronic component, for example a resistor, capacitor or diode.
How an Operational Amplifier (Op-Amp) Comparator Works.

To demonstrate how a comparator works, let's look at the circuit shown below.
How an Operational Amplifier (Op-Amp) Comparator Works.

In this circuit, a divider made of 10K and 100K resistors sets a voltage of 0.45V at the inverting input; it is also called the reference voltage. While the voltage at the non-inverting input is below 0.45V, the output will be 0V and the LED won't light; as soon as the voltage at the non-inverting input exceeds this value, the output will become 5V and the LED will light. So by turning the potentiometer, we can turn the LED on and off. The circuit isn't practical, but it's illustrative.
One of the articles describes how a peak detector works, and there you can see an op-amp connected as a comparator. You can click on the photo to enlarge it.
How an Operational Amplifier (Op-Amp) Comparator Works.

Let's simplify the circuit a little.
How an Operational Amplifier (Op-Amp) Comparator Works.

And connect an oscilloscope to the comparator inputs. Channel one is the non-inverting input, channel two the inverting input.
How an Operational Amplifier (Op-Amp) Comparator Works.

Clapping produces spikes; if the amplitude of the spikes (yellow) exceeds the reference voltage (turquoise), the output goes to the positive supply voltage, otherwise to the negative.
In this case the sensor is a microphone; a photodiode can also serve as the sensor, for example to switch on a light at low illumination, which we set with the reference voltage.
Earlier, we agreed that a comparator is an op-amp circuit without negative feedback. But besides negative feedback, there is also positive feedback.
How an Operational Amplifier (Op-Amp) Comparator Works.

The circuit shown above is called an inverting Schmitt trigger; essentially it's the same comparator, only with positive feedback. It works like this: remember how on the oscillogram the output voltage changed when the yellow lines crossed the turquoise one? Here there are two lines that can be crossed: when the upper line is exceeded, the output goes to the negative supply voltage; if the value drops below the lower line, to the positive; and between the lines the system keeps its state.
How an Operational Amplifier (Op-Amp) Comparator Works.

There is also a non-inverting Schmitt trigger, shown in the circuit below.
How an Operational Amplifier (Op-Amp) Comparator Works.

A logical question would be why Otto Herbert Schmitt wasn't satisfied with an ordinary comparator and invented his own. The answer is simple: if you feed a noisy signal to the input of a comparator without positive feedback, it will cause many false triggers; to avoid this, the Schmitt trigger was invented, which has two switching thresholds.
True, it too has room for improvement. I'd like to get rid of the dual supply, and since the switching thresholds are set with a divider, they are symmetric about zero, whereas I'd like to choose them arbitrarily.
That's probably all I wanted to say about op-amp comparators; if you want to understand them in more detail, you're welcome here.