Between Analog and Digital: Comparators

All information about this project is available in a special website section.
We thank you for your attention!
No items found.
Click image to zoom

Sometimes, there's a need to compare two voltages. A special circuitry stage exists to serve just this purpose—a comparator. Today, I will tell you all about comparators and show a few examples of their use.

What is the field of application for comparators? — It is literally any device; many microcontrollers have built-in comparators of their own!

Remember our post on PWM (https://teardownit.com/posts/pwm-led-dimmer)? That circuit used as many as two comparators—U1A and U2B.

What could be simpler than a comparator? It compares two voltages at its two inputs, and based on the result of this comparison, it connects its output to the positive or negative power line.

When the voltage at the non-inverting input is higher than at the inverting input, it will be a positive supply voltage line and, accordingly, a high voltage level, a logical one. Otherwise, it will be a negative power line, a low level, or a logical zero.

Why does the comparator look like an op-amp in the diagram? Most often, this is the operational amplifier.

U1A switches the timing capacitor C1 to charge or discharge, depending on the voltage at point C, which comes to the non-inverting input of the comparator through resistor R10.

Let me draw your attention to resistor R9, as it is of great importance in this scheme.

We are accustomed to connecting a resistor between an op-amp's output and the inverting input to set the gain. This is negative feedback.

The output of U2A is connected to its inverting input. We should recall two fundamental principles of the operation of an operational amplifier: virtually no current flows through its inputs, and the output voltage, with negative feedback present, will be such that the voltages at the inverting and non-inverting inputs are equal.

Thus, the voltage at the output will equal the voltage at the non-inverting input. We've got a buffer—a voltage follower.

Why do we need such a buffer? Because the input impedance of the operational amplifier is very high, and the output impedance is low.

That means we can transmit input voltage to the load that requires a significant current and simultaneously not overload the circuit generating this voltage and not distort its operation.

And here is a small part of the Fulltone OCD guitar pedal circuit from our post about DIY Dumble-like sounding MOSFET Overdrive. The operational amplifier no longer repeats but amplifies the input voltage.

For simplicity's sake, let's ignore capacitors, although they contribute to impedance. We have to understand the core principle.

The amplifier input is the left terminal of resistor R10. To pull up the non-inverting input of the op-amp to virtual ground, which is half the supply voltage, resistor R12 is used.

R10 and R12 form a voltage divider. The voltage at the non-inverting input will equal the input multiplied by R12 / (R10 + R12) = 220 / 230 = 22/23.

22/23 is almost 1, so we'll assume that the voltage at the non-inverting input, and therefore at the inverting input, is equal to the input.

R13 and R11 are also voltage dividers in the feedback circuit of the operational amplifier. The output voltage will equal the input voltage multiplied by (R13 + R11) / R11 = 189 / 39 = 4.85. Or 4.64, if you insist on multiplying by 22/23.

Let's get back to the PWM regulator circuit. Here, we have the same voltage divider but in a stage of positive rather than negative feedback! And it is assembled with resistors R9 and R10.

Almost no current flows through the inputs of an op-amp; it only goes through R9 and R10. And since this is the same current (let’s denote it as 'I'), we can write the equations of Ohm’s law for resistors R9 and R10.

Let's say the input voltage at the correct pin of R10 is Uin, and the output voltage at the output of U1A is Uout. Call the potential of the non-inverting input U1A 'U', and we obtain the following equations:.

I = (Uout - U) / R9
I = (U - Uin) / R8

Next, we transform our equations to calculate the dependence of U on Uin.

(Uout - U) / 5.6 = (U - Uin) / 2

For simplicity, let's round 5.6 up to 6.

(Uout - U) / 3 = U - Uin
Uout - U = 3U - 3Uin
3Uin = 4U - Uout
U = 3/4Uin + Uout / 4

To understand what Vout might be equal to, we need to refer to the datasheet for the LM358 chip from Texas Instruments.

Our PWM controller has a bipolar power supply of +12 V and -12 V, for a total of 24 volts. These are our power bus voltages. However, due to its design characteristics, the deviation of the operational amplifier's output voltage also depends on the output current.

The load U1A is a parametric voltage stabilizer based on a two-anode Zener diode assembled from two conventional Zener diodes connected in a back-to-back series.

The forward voltage across the Zener diode at the standard shunt regulator current is 0.7 volts. Then, the correct pin of R8 will have a voltage of 5.8 volts with a plus or minus sign, depending on the polarity of the U1A output.

That means the current through R8 will be about 5 milliamps. Then Vout can be somewhere between + (12 - 1.5) = +10.5 V and - (12 - 0.75) = - 11.25 V.

So, when the logic level at the output of the comparator is high, then U = 3/4Uin + 2.63V.
When the logic level is low, U = 3/4Uin - 2.8 V.

The potential of the comparator's inverting input is zero since it is connected to the ground through resistor R11.

We need the voltage at the non-inverting input below zero volts to switch the comparator from high to low. And from low to high—above zero volts. Then we get the following equations:

3/4Uin + 2.63V = 0
Uin = -3.5 V

3/4Uin - 2.8 V = 0
Uin = 3.75 V

What will then happen to the comparator in the input voltage range of -3.5 to +3.75 volts? Nothing; the comparator will not change its state.

If its output is high, it will remain high until the input voltage drops below -3.5 volts. And if it was low, then it would wait for the voltage to rise above +3.75 V to switch to high. This means the comparator has so-called hysteresis.

Comparator hysteresis is excellent for protecting against false alarms and interference. And thanks to positive feedback, we got a comparator with memory!

It saves its state and waits for the signal to be set to high or reset to low. It sounds like an RS flip-flop, right?

Sometimes, lathes are actually assembled this way. Suppose an extra comparator or an operational amplifier is left unused in some chip, and we need a flip-flop. Why add an extra element when we already have almost everything we need? Just bring in a couple of resistors.

We have an extra operational amplifier in our PWM regulator. It is U2A, which we've used as a buffer, even though the input impedance of U2B is precisely the same without a repeater. If we needed an RS flip-flop in this circuit, we could easily build it on U2A.

Now, let's build an electronic thermometer with an LED scale.

The temperature sensor is a thermistor TH1 with a negative temperature coefficient. Together with R10, they form a voltage divider.

With TH1's nominal resistance of 10 kOhm, the voltage at the resistor will be 1/11 of the supply voltage, approximately 0.5 volts.

With a total resistance of tuning resistor R9 of 100 kOhm, there will also be approximately 0.5 volts at the left pin of R1. As the resistance R9 decreases, this voltage will increase to 5 volts.

The resistive ladder R1-R8 generates a series of voltages in increments of 1/8 of the voltage at the left pin of R1.

As the temperature of the thermistor increases, its resistance will decrease. Accordingly, the voltage at the non-inverting inputs of the comparators will increase. The higher this voltage is, the more comparators will switch to a high output state, and accordingly, more LEDs will light up.

Resistance R9 can be adjusted so that D4 and D5 correspond to the desired temperature range. If D6 is not lit, the temperature is too low, and if D3 is lit, the temperature is too high.

This thermometer can serve not only as a temperature indicator but also as a temperature regulator. For example, you can do this:

When D6 goes out, the heating turns on, and when D5 lights up, it turns off.

When D3 lights up, ventilation is turned on; when D4 goes out, it turns off. This will provide hysteresis so the heater and fan do not turn on and off too often.

This is another application of hysteresis for which RS flip-flops come in handy. We already know they can be built on identical operational amplifiers in comparator mode.

The terminals for the outer LEDs (D1 and D8) can be connected to an alarm signal. Suppose the temperature in the terrarium or other place where we've installed the thermostat significantly exceeds the permissible limits. In that case, an alarm siren goes off, an SMS is sent to the phone, and so on.

So, with the help of comparators, one can turn analog values into logical events and organize automated control with a convenient visual indication.

At the same time, we did not need any microcontroller; two LM324 chips were just enough.

The operation and assembly process of the electronic thermometer are shown in the video.

Products in this post

No items found.

Sign up for my newsletter

Kevin Gibbs

Hi! I'm Kevin! I am a very curious engineer :))
I'm the website founder and author of many posts.

I invite you to follow exciting experiments, research, and challenges.
Let's go on to new knowledge and adventures!

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.