Table of contents
  1. What the LM358 is
  2. How a comparator works
  3. Example: a 12V battery low-voltage alarm
  4. Why the output chatters, and hysteresis
  5. Limits of using an LM358 as a comparator
  6. Checking in Patterndo
  7. Summary

A comparator is a circuit that compares two voltages and tells you which is larger as High or Low. It is used where a "threshold decision" is needed, such as an alarm that warns when the battery voltage drops, or a light that turns on depending on the brightness of light. There are dedicated comparator ICs, but at the beginner stage the common LM358 op-amp is often used as a comparator. This article covers the features of the LM358, building a comparator circuit, why the output chatters and how hysteresis fixes it, and what to watch out for.

What the LM358 is

The LM358 is an IC with two operational amplifiers (op-amps) in an 8-pin DIP. The LM324 is a 14-pin product with four of the same circuit.

PinNameRole
1OUT AOutput of the first amplifier
2IN− AInverting input of the first
3IN+ ANon-inverting input of the first
4V− (GND)Ground (with a single supply)
5IN+ BNon-inverting input of the second
6IN− BInverting input of the second
7OUT BOutput of the second
8V+Supply (about 3 to 32 V)

The strengths of the LM358 are that it works from a single supply and that its inputs are allowed down to ground (0 V). The output goes down close to 0 V, but at the high end it only rises to about 1.5 V below the supply voltage. With a 5 V supply, the output High is about 3.5 V.

How a comparator works

An op-amp used without feedback amplifies the difference between its two inputs enormously, so the output almost always sticks to one extreme.

  • IN+ voltage > IN− voltage: output High
  • IN+ voltage < IN− voltage: output Low

So by putting a reference voltage on one input and the voltage to watch on the other, you can tell whether the threshold has been crossed.

Example: a 12V battery low-voltage alarm

Let us build a circuit that lights an LED when a 12 V battery falls below 10.5 V.

  1. Reference voltage: make a stable 5.1 V with a 5.1 V Zener diode and a resistor (How to Use Diodes and Zener Diodes). With 5 mA of Zener current from the 12 V supply, the resistor is (12 − 5.1) ÷ 0.005 ≈ 1.4kΩ, so use 1.5 kΩ.
  2. Voltage to watch: divide the battery so that 10.5 V becomes 5.1 V. With R1 = 10 kΩ (top) and R2 = 9.1 kΩ (bottom), 10.5 × 9.1 / 19.1 ≈ 5.0V. For the divider design, see Designing Voltage Dividers and the Loading Effect.
  3. Comparator connection: put the divider output (the voltage to watch) on IN− and the reference voltage on IN+. When the battery is healthy, the watched voltage is above the reference, so the output is Low; when it falls below 10.5 V, the watched voltage is below the reference and the output goes High.
  4. LED: connect the output through 1 kΩ and the LED to ground and it lights on low voltage (the current is about (12 − 1.5 − 2) ÷ 1k ≈ 8mA).

Why the output chatters, and hysteresis

If the watched voltage changes slowly near the reference voltage, noise makes the output flip quickly between High and Low (chattering). The LED flickers or the relay rattles. The remedy is hysteresis. Feed the output back to IN+ through a resistor (positive feedback) to deliberately make the turn-on and turn-off thresholds different.

The arrangement is as follows. Make the reference voltage with a divider of Ra (toward +V) and Rb (toward ground), feed it to IN+, and connect a feedback resistor Rf from the output to IN+.

Hysteresis width ΔV ≈ (output High − output Low) × (Ra ∥ Rb) / (Rf + Ra ∥ Rb)

With Ra = Rb = 10 kΩ (5 kΩ in parallel), Rf = 470 kΩ, and the output swinging between about 0 V and 3.5 V, ΔV ≈ 3.5 × 5k / 475k ≈ 37mV. Reducing Rf widens the band. Make the width larger than the noise on the signal you are watching.

Limits of using an LM358 as a comparator

  • It is slow. An op-amp output does not switch as fast as a comparator, so it is not suited to fast signals. Use it to watch slowly changing voltages.
  • The output High does not reach the supply. When connecting directly to 5 V logic, check that the High level exceeds the TTL threshold.
  • A dedicated comparator (LM393 and so on) has an open-collector output so it needs a pull-up resistor, but it is fast and can be matched directly to the supply level. If you need a true comparator, use one of these.
  • For an unused amplifier, connect the inverting input to the output (a buffer configuration) and tie the non-inverting input to mid-supply or ground. Leaving inputs floating can make it oscillate and raise the total current.
  • Put a 100 nF next to the supply pin (Power Rails, Grounding, and Decoupling Capacitor Placement).

Checking in Patterndo

Patterndo has LM358 and LM324 models, simulated as amplifiers that reflect open-loop gain and output voltage limits (High and Low headroom). Make the reference voltage and the watched voltage with dividers, and change the input voltage to see the point where the output flips. The model is close to an ideal device, so the speed and noise characteristics of real parts are not reflected.

Summary

An op-amp used without feedback becomes a comparator, with the reference voltage on one input and the watched voltage on the other. Prevent chatter near the threshold by giving hysteresis with positive feedback, and if speed matters, choose a dedicated comparator such as the LM393. For the order of checks when something goes wrong, see It Is Finished but It Does Not Work: A Step-by-Step Checklist.

This article is general guidance for learning purposes. Specifications and pinouts differ between parts, so check the datasheet of the part you use before real work. For safety-related work, read the cautions for each item first.