
Fig. 1 LM741 based comparator
uses bipolar power supply.
Comparator Circuits Examples Tutorial
Comparators are every for allowing digital circuits and micro-controllers to interface with analog voltages in the real world. Often having two inputs they output a HIGH or LOW based on the relationship of those inputs.
They are used internally within Arduino making analog-to-digital conversion (ADC) possible when used with a digital-to-analog voltage converter (DAC).
In my Arduino Analog to Digital Conversion Voltmeter I generated a variable voltage with the Arduino pulse-width-modulation with the use of a comparator to measure an input voltage up to 20-volts - far above the 5-volt digital logic.
In another case I used a LM339 quad comparator to build a non-microcontroller based battery charger based on the circuit in Fig. 1.
Above we are using all four comparators in the LM339 as a 4-bit voltmeter. The voltage divider resistors and the 100K pot set the trip on voltages.
Comparator Basics
Also see Voltage Comparator Circuits
A comparator is often an operational amplifier without feedback between the inputs and output. It's either all the way ON (near Vcc) or OFF (near 0 volts). Illustrated in Fig. 2 is a comparator built from a common LM741 op amp.
In this test circuit we use a 12-volt bipolar power supply. In the NEG input on pin 2 is fixed at 6-volts with a Zener diode connected to the + side of the power supply. We will call this Vref.
On the POS input on pin 3 we connect a potentiometer back to + and GND. This is Vin. If Vin is less than 6-volts the output on pin 7 of the LM741 will be about minus 10-volts. Here we use a 1N4001 diode to protect the LED from excessive reverse voltage.
If we adjust the 10K pot to where Vin is greater than Vref the output on pin 7 goes to about plus 10-volts forward biasing the 1N4001 and turning on the LED.
The disadvantage of using the LM741 is the use of a bipolar power supply.
The comparator circuit in Fig. 2 uses a LM358 op-amp instead of the LM741. The LM358 is designed for single supply operation. It does the same thing as Fig. 2 with the addition of Q1 to act as an open collector output driver.
When Vin is less than Vref the output on pin goes to about 10-volts turning on transistor Q1 which turns on the LED. By setting a trip point with the 10K pot one can make an under-voltage indicator.
As a rule for comparator inputs with open collector outputs such as the LM339 or LM311 (not the LM358 circuit):
Current WILL flow through the open collector when the voltage at the MINUS input is higher than the voltage at the PLUS input.
Current WILL NOT flow through the open collector when the voltage at the MINUS input is lower than the voltage at the PLUS input.
Fig. 4 uses a LM311 comparator specifically designed to be ONLY a comparator - all the external parts in Fig. 3 minus the LED circuit, pot, Zener. Its feedback and input resistors are internal. It operates identical to the LM358 circuit.
The external 4.7K resistor is used for interfacing to digital logic by producing a HIGH or 12-volts out when the LM311 is turned off. This is how digital circuits "communicate" with resistive sensors. Setting Vref to a known point and checking the output tell us when Vin is greater than Vref.

Fig. 4 LM339 comparator over voltage alarm
using opto-coupler to sound horn - click picture for full size.
In Fig. 5 we are using a one of four LM339 quad comparators to sound a horn if Vin goes too high due to Vcc going over 12-volts. The LM339 is equal to 4 LM311s in a single package with common power and GRD connections for all 4 comparators. See Fig. 1 above.
This circuit is useful in say an automobile charging system regulator that fails dumping excessive voltage into system ruining the battery and electronics. Here I used a 4N25 opto-coupler to drive a low power buzzer or horn - the current capacity of the comparator output is low while the opto-coupler is much higher.
See LM339 spec sheet.
In Fig. 6 we use an external PNP bipolar transistor to drive a relay. When the open collector output of the comparator is turned on current flows through the 1K resistor and the base-emitter junction turning on the transistor completing the circuit for relay K1 and the LED on indicator.
Now we use a LM339 to construct a "night light". The Zener diode has been replaced by a CdS photocell that decreases in resistance in proportion to increasing light levels. As the resistance of R2 decreases in daylight Vref is higher than Vin thus the comparator id off.
As light levels drop R2 increases resistance with Vref falling below Vin turning on a relay though an opto-coupler to turn on lights or whatever. The very same PNP circuit in Fig. 6 would also work. Use the 10K pot to set the trip-on point.
As a final note R1 and R2 can be reversed for the opposite effect. Say turn off a blower motor in a green house as the sun goes down. The photocell could be replaced by a thermistor for temperature.
Hobby Electronics Homepage and Webmaster Homepage (Off site.)
- Web Master
- Gen. Electronics
- YouTube Channel
- Arduino Projects
- Raspberry Pi & Linux
- PIC18F2550 in C
- PIC16F628A Assembly
- PICAXE Projects
- Bristol VA/TN
- Environmentalism
- US Constitution
- Religious Themes
- Religion Archive 1
- Comparator Theory Circuits Tutorial
- Analog Solar Panel Battery Charge Controller
- Better Arduino Rotary Encoder Sensor
- Simple 3-Wire MAX6675 Thermocouple ADC Arduino Interface
- TA8050P H-Bridge Motor Control
- All NPN Transistor H-Bridge Motor Control
- Basic Triacs and SCRs
- Comparator Hysteresis and Schmitt Triggers
- Comparator Theory Circuits Tutorial
- Photodiode Circuits Operation and Uses
- Optocoupler MOSFET DC Relays Using Photovoltaic drivers
- Connecting Crydom MOSFET Solid State Relays
- Photodiode Op-Amp Circuits Tutorial
- Optocoupler Input Circuits for PLC
- H11L1, 6N137A, FED8183, TLP2662 Digital Output Optocouplers
- Optical Isolation of H-Bridge Motor Controls
- All NPN Transistor H-Bridge Motor Control
Optical Isolation of H-Bridge Motor Controls YouTube
Optical Isolation of H-Bridge Motor Controls
Opto-Couplers Theory and Circuits YouTube
Opto-Isolated Transistor Drivers for Micro-Controllers
All NPN Transistor H-Bridge Motor Control YouTube
All NPN Transistor H-Bridge Motor Control
- Experiments with TL431 Shunt Regulator
- TL431 Precision Current Regulator Circuits
- LM317 Adjustable Voltage current Boost Power Supply
- LM317 High Power Constant Current Source Circuit
- Constant Current Circuits with the LM334
- LM317 Constant Current Source Circuits
- Introduction Hall Effect Switches, Sensors, and Circuits
- Basic Transistor Driver Circuits for Micro-Controllers
- Opto-Isolated Transistor Drivers for Micro-Controllers
- Geiger Counter Basics Radioactivity
- ULN2003A Darlington Transistor Array with Circuit Examples
- Tutorial Using TIP120 and TIP125 Power Darlington Transistors
- Driving 2N3055-MJ2955 Power Transistors with Darlington Transistors
- H-Bridge Motor Control with Power MOSFETS
- Build a High Power Transistor H-Bridge Motor Control