An op-amp comparator: which input is higher?
- 192parts on the bench
- 25boards running now
- 1.00xreal time, on every board
An op-amp amplifies the difference between its two inputs, by an enormous amount. With no feedback connected, that amount is the op-amp’s whole open-loop gain, 100 000 in Mokxi’s model. A single millivolt of difference asks for 100 V at the output, which no 5 V circuit can give, so the output goes as far as it can, to one rail or the other. The op-amp stops being an amplifier and becomes a comparator: its output says which input is higher.
The circuit above holds the inverting input, IN-, at 2.50 V with two 10 k resistors, and puts a potentiometer on the non-inverting input, IN+. The output drives a green LED through 220 ohms. When the knob sets IN+ above 2.50 V the LED is on; below, it is off.
The knob starts just past the middle, so the LED is lit. Open the circuit in the editor, press Run and turn the knob slowly back through the middle. The LED does not fade. It snaps.
How sharp the edge is
The knob’s position is a number from 0 to 1023. Just below the middle it puts 2.4976 V on IN+, and the output sits at 0 V. One step further it puts 2.5024 V there, and the output is high. A few millivolts are the whole transition, because the gain turns a few millivolts into more than the supply.
The output high reads about 4.3 V rather than 5 V while the LED is lit. That is not the comparator being unsure: the op-amp has 100 ohms of output resistance in the model, and the LED and its resistor draw about 7 mA through it. With no load the output goes all the way to the rail. Many real op-amps cannot get that close to either rail at all: an LM358 on 5 V tops out around 3.5 V.
Why a plain comparator chatters
Real signals are noisy. A light level from an LDR or a temperature from a thermistor drifting slowly past the threshold carries a few millivolts of noise on top, so while it is close to the threshold it crosses back and forth many times, and the output crosses with it: a burst of on-off-on-off instead of one clean change. A relay driven like that buzzes, and a microcontroller counting the edges counts dozens.
The cure is hysteresis: two thresholds instead of one, a higher one for switching on and a lower one for switching off, so that once the output has changed, the input has to come a good way back before it changes again.
Adding hysteresis: a Schmitt trigger
Feed a little of the output back to the non-inverting input and you get it. The second circuit, opened with the button below, drives IN+ from a slow triangle wave through 10 k and adds a 100 k resistor from the output back to IN+. IN- is still 2.50 V.
Work out where it switches. With the output low, at 0 V, IN+ is the input divided down by 100 k over 110 k, so IN+ reaches 2.50 V when the input reaches 2.75 V. With the output high, at 5 V, the feedback lifts IN+ to (10 x Vin + 5) / 11, which falls to 2.50 V only when the input falls to 2.25 V. On the scope the output switches up at 2.75 V and back down at 2.25 V: half a volt of hysteresis. Any noise smaller than that cannot make it chatter.
Op-amp or comparator chip?
An op-amp will work as a comparator, and for a slow signal like a light level it is fine. Dedicated comparators such as the LM393 are built for the job: they switch in about a microsecond rather than tens of microseconds, and they are designed to live with their inputs far apart, which some op-amps are not. Most have an open-collector output, which pulls low but cannot pull high, so they need a pull-up resistor, and that lets you choose the output voltage to suit a 3.3 V board.
Mokxi’s op-amp has a gain-bandwidth product, a slew rate and headroom short of its rails, but its input offset is zero unless you set it and it has no common-mode limits, so treat its threshold as exact in a way a real part’s is not: a real op-amp’s few millivolts of offset move the threshold by that much. Set its offset property to 3 mV and watch the threshold move.
Common mistakes
Feedback to the wrong input. Feedback to IN- makes an amplifier; feedback to IN+ makes hysteresis. Swap them by accident and a comparator becomes an amplifier or an amplifier latches to a rail. Forgetting the pull-up on an open-collector comparator, which then only ever reads low. And driving a relay or a motor straight from an op-amp output, which can source a few tens of milliamps at most: put a transistor after it.
Questions
Can I use an op-amp as a comparator?
Yes, for slow signals: with no feedback its gain drives the output to one rail or the other depending on which input is higher. For fast edges or open-collector outputs, a dedicated comparator such as the LM393 is better.
What is hysteresis in a comparator?
Two switching thresholds instead of one, made by feeding a little of the output back to the non-inverting input. The input has to rise past the upper one to switch on and fall past the lower one to switch off, so noise near the threshold cannot make the output chatter.
How do I calculate Schmitt trigger thresholds?
Write the voltage at IN+ as a divider between the input and the output, once with the output low and once with it high, and solve each for the input voltage that makes IN+ equal the reference. With 10 k in, 100 k feedback, 2.5 V reference and a 0 to 5 V output, that gives 2.75 V and 2.25 V.
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.