Voltage comparator is a bistable circuit (flip-flop) which compares two analog signals at the input and depending on the outcome of the comparison it gives HIGH or LOW output. We can imagine it as a small "multimeter" with a switch. When the measured voltage is higher on the first input pin, the switch is turned on. However, when the voltage is higher on the second input pin, the switch is turned off. Even though there are different models of comparators, we will study it in the example of LM393, which is probably the most used one.
LM393 - Basic characteristics
LM393 voltage comparator on the MQ breakout board
This comparator is on the multitude of breakout modules we use with Arduino, therefore it is a great example to show its purpose. LM393 is a "Low Power, Low offset Voltage, Single supply, Dual, Differencial comparator".
- Low power - comparator uses low power consumption to operate
- Low offset voltage - can compare very similar voltages
- Single supply - uses the same power source as the points we compare
- Dual - this model has two comparators on the chip
- Differential - means that it compares two input voltages depending on each other, not some constant voltage
You should bear in mind that there is no such thing as an ideal comparator, respectively the one we described in the text above. In reality, there is noise, delayed response time, voltage offset, etc. These and all the other characteristics can always be found in datasheet (LM393 datasheet).
LM393 - Pinout
LM393 is an 8-pin integrated circuit, as seen in the picture. Two pins are inputs for voltage Vcc and GND. Vcc is a positive terminal of the power supply, according to the datasheet up to +36V, while GND is a negative terminal. These two terminals allow the comparator to work. Once we connect the power source to the chip, it needs two voltage comparisons. As it is already mentioned, LM393 has two mutually independent operation amplifiers (op-amp).
ln1(-) and ln1(+) are inputs for the first operating amplifier (Output1), while ln2(-) and ln2(+) are inputs for the second one (Output2). ln1(+) and ln2(+) are non-inverted, while ln1(-) and ln2(-) are inverted inputs.
When the voltage in the non-inverted input is higher than the one in the inverted, output pin goes to the maximum positive value - HIGH (Vcc). In case the non-inverted voltage falls below the invert input voltage, the output goes to the maximum negative value - LOW (GND). The output is, of course, limited to the power values we have brought to Vcc/GND pins.
Now that we know how the voltage comparator works, we can make our own electrical circuit. We will do it on an example of night light. Using the photoresistor we will read off the amount of light, and LM393 will be the switch turning the LED diode on or off. This is actually an optical light sensor.
Photoresistor: Before we proceed, we will briefly explain how the photoresistor works. The amount of its resistance depends on the amount of light falling on it. When it is in the dark, the amount of resistance is high. On the other hand, when it is exposed to light, there is little resistance. In some kind of ideal conditions, resistance in the dark would be app. 200kΩ, and when there is bright light 1-2kΩ. For more, check the datasheet.
The following step is to make voltage divider with a fixed resistor and a photoresistor. Fixed resistor should be 10kΩ, which will provide an output of 0.429V when the photoresistor is in the dark (it provides 200kΩ resistance) and 7.5V when the resistor is exposed to light (it provides 2kΩ resistance). Whether we will connect this output voltage to ln(-) or ln(+) does not matter in this case, but we will tell you a bit more about it later on. So, let's connect it to the inverted input, In1(-), yellow wire.
After that we will connect the input voltage for comparison and over 10kΩ potentiometer so that we can adjust the circuit sensitivity. We connect the potentiometer by connecting the reference voltages to the edge pins. On the left pin, we connect the GND, and on the right +9V. The central pin is our output voltage and we connect it to a non-inverted input of the comparator In1(+). The voltage that the input provides depends on the position of the potentiometer. If we turn it completely left, i.e. towards the GND, the output voltage tends to 0V. As we turn it clockwise, the output voltage is proportional to + 9V. For starters, we can put it roughly in the central position.
What is going to happen next is:
- in the dark: the voltage on the inverted pin In1(-) is lower than that of the non-inverted In1(+), therefore we get GND on the output
- in the light: the voltage on the inverted pin In1(-) is higher than that of the non-inverted pin In1(+), therefore we get Vcc (+9V) on the output
Since we want the LED diode to light in the dark, the Output pin will be connected to the cathode(-) of the LED diode, while we will connect the anode (+) over 330Ω to + 9V.
In case we replaced the inverted and non-inverted input on the voltage comparator, they would get +9V on the output, in the dark. Then we would only connect that output to the LED module anode while connecting the cathode to GND. Of course, through some resistor.