Sunday, 7 December 2014

How to Make an Op-Amp into a Comparator

A blog reader has asked me to write up a blog post on Analogue Comparators.  A comparator is a special application of electronics and operational amplifiers used to compare two electronic signals and provide a fixed output based on the comparison.

There are lots of online resources with how to make and use comparators, this is my version.  In writing this tutorial I read some books, trawled the internet and asked colleagues for help!  Here are the books I look at:

Higher Electronics by Mike James - Buy from Amazon

Practical Electronics for Inventors - Buy from Amazon

The Art of Electronics - Horrowitz and Hill - Buy from Amazon

These books are excellent if a little expensive.  I bought my copies a long time ago when I was studying but still use them all of the time.  I suggest people look for them in a library if they can't afford to buy them.  A new edition of the 'Art of Electronics' is due out soon so I will hold off buying that one until then.

Here are some of the websites I look at:

Wikipedia's Entry on Comparators

All about circuits entry on Comparators

Bucknell Universities Entry for Comparators

Comparators can be made from discrete bipolar transistors but now since op-amps are easy to make and manufacture complete comparator devices can be bought from electronics vendors.  I normally when designing circuits use a specific comparator integrated circuit.  Here is the schematic symbol for a comparator - It looks a bit like an Operational Amplifier:

Normally I choose to use an LM311 or an LM339.  These are almost standard goto integrated circuits for electronics design engineers these days.  Here are the datasheets for those devices if people are interested:

LM311 Datasheet

LM339 Datasheet

A comparator is a device used to compare analogue signals and provide a digital output.  Lets say we have a sine wave signal at 500mV and we want to switch an LED on when the voltage is between 300mV and 500mV.  What we need to do is compare the voltage to a reference signal at the lower threshold (300mV) and then turn an LED on.  Lets make a comparator using a 741 Op-Amp and some resistors.  We can simulate the circuit function and prove it works!  For this tutorial I am concentrating on non-inverting comparators.

Lets select the venerable LM741 Op-Amp as it's a device that is easily obtainable and most electronics engineers are familiar with it.  It isn't necessarily the best choice of op-amp but it is popular.  We will also need some resistors to set the reference voltage or a potentiometer or variable resistor.


1k Resistor
820R Resistor
100R Resistor - LED Current limit
5mm Red LED - just for indication

The 741 Op-Amp compares the two signals applied to it's inverting and non inverting inputs.  In this case I have set the reference to be on the inverting input at 2.25V with the voltage divider between R2 and R1.  The input signal comes from a signal generator set to give a 2.5V sine wave signal at 10Hz.  Because the sine wave is varying between +/- 2.5V the output comes on and off.  While the input voltage is greater than 2.25V the output goes high and the LED is on but once the voltage drops below 2.25V the LED will be off.

Here is a simulation showing the circuit in operation:

Here is a screenshot of the oscilloscope screen:

The red trace shows the sine wave from the signal generator at the non-inverting input and the blue trace shows the output of the comparator.

This circuit works well enough for a slow constantly changing signal but what about a rapidly changing and varying one.  If we had an input signal that was constantly varying between 1.75V and 2.5V the output would change state all of the time and not give a reasonable output.  At that point we need to introduce something known as Hysteresis.

Hysteresis can be a complicated concept to understand, it can be related to several things in science and engineering.  Here is a definition of it:

Wikipedia entry for Hysteresis

So what actually does Hysteresis mean - Feedback the output of a system into one of the inputs to allow the system to function between values with an upper and lower range.  So in the case of the circuit above feedback the output to the inverting input and that causes the circuit to now change state when the input voltage is between two fixed ranges that the designer can calculate.  Cool huh!

Why do we need hysteresis?  Well for a varying amplitude input signal...not a repetitive sine wave without hysteresis the output will change state continuously.  In simpler terms the output will change state erratically:

If we expand the sections where we can see that the output has changed with respect to the input we can see the following:

What this shows is that the output is struggling to keep up with the input.  Even though the voltage at the input is above the threshold voltage of 2.25V the output doesn't always change state and when it does the time that it's on for varies considerably.  We can prevent this from happening by added some positive feedback and introducing hysteresis.

We add positive feedback by adding some resistors from the output back to the inverting input.  Here is the updated circuit diagram:

Lets choose some parameters.  Lets say we want out output to be high (LED on) when the input signal is between 2V and 2.25V.  Those are known as the Upper threshold voltage Vin1 and the Lower threshold voltage Vin2.

We can then apply some Ohm's law and calculate some resistor values needed to introduce these threshold voltages.

It's at this point we might need a diagram to explain things - here is the classic Hysteresis diagram which is supposed to help explain what we need to achieve.

It never really helped me though I must admit!  What I believe its meant to show is a graph of the voltages expected.  Vout is going to be 5V as that is the maximum possible voltage we can get from the op-amp supply.  Vin varies but is between Vin1 and Vin2

We have already said that Vin1 and Vin2 are 2.5V and 2.0V

Lets label the diagram just to make it easier to understand:
Now lets use Ohms law in various forms to generate those voltages and introduce Hysteresis.  I must admit this bit is hard...I will try and keep things simple...

We want to cause the output VT (5V) to change when the input is between 2V and 2.5V.
Now we need to calculate the value of the R3 to get the voltages we need.  If we apply ohms law to every part of the circuit with the information we know we can re-arrange things to solve for the resistor values.

There are several ways we can solve this.  Lets choose a value for R3 and then insert the numbers and solve for R5. Lets set R3 = 10k There are lots of formulas to work with (I hate mathematics)

Vin1 = VREF * (R5 + R3) / R3

Vin1 = 2.5 V

VREF = 2.25 V

R5 = ?

R3 = 10k or 10000 Ohms

We now need to rearrange the formula to solve for R5

Vin1 * R3 = VREF * (R5 + R3)

(Vin1 * R3) / VREF =  R5 + R3

((Vin1 * R3) / VREF ) - R3  =  R5

Lets know re-write the formula with R5 on the left just for completeness:

 R5 = ((Vin1 * R3) / VREF ) - R3

Lets now insert the values and calculate R5:

R5 = ((2.5 * 10000) / 2.25 ) - 10000

R5 = (25000 / 2.25) - 10000

R5 = 1111. 11111111 Ohms.  Lets use a 1k resistor as it is the closest E24 series resistor value.

This will change the Vin2 value so for completeness lets calculate what that value will be:

Vin2 = (VREF * (R5 + R3) - VCC * R5) / R3

Lets insert the values and perform the calculation:

Vin2 = (2.25 * (1000 + 10000) - 5 * 1000) / 10000

Vin2 = (24750 - 5000) / 10000

Vin2 = 1.975 V close enough for our purposes.  If you need a value closer to 2 Volts for the Vin2 then change R5 to a slightly larger value.

We can now update the Hysteresis diagram just for completeness:
The circuit diagram can all be updated with the resistor values for R3 and R5:
If we re-run the simulation we can see the results from the oscilloscope screen:

When displaying the results of hysteresis most textbooks favour a triangular wave input so just for fun here is what the output looks like with a triangular wave input:

What this shows clearly are the values of Vin1 and Vin2, Which according to the simulation are 2.294V and 1.991mV.  This is close enough to the calculated values to be acceptable.

I wouldn't normally use a 741 Op-Amp to make a comparator.  I would use a specific comparator like the LM311.  Its specifically designed for the purpose and is easy to use.  Here is the circuit diagram for achieving the same results:

It performs exactly the same as the 741 circuit with less components and already has hysteresis - Win!  There are also lots of online calculators to make it easy for the aspiring designer.

Hysteresis Calculator

Well...that's about all for now.  I might update the pages with actual circuits and a video.

Take care - Langster