The Classroom

The LM311 Voltage Comparator

Daniel Koch

Issue 38, September 2020

This IC is a low-component-count way to compare two signal voltages or a signal and a reference, and switch the output either on or off.

Comparators do a very specific job that presents some advantages to the maker which may not be immediately obvious. Comparing voltages or switching at a threshold can be done with the Analog to Digital Converter (ADC) built into Arduino and Raspberry Pi, but we will discuss later some very valid reasons why you may want to do this externally.

We’ve previously come across the comparator before when discussing Operational Amplifiers (Op Amps), and we have even made them out of op-amps in projects. However, the LM311 Integrated Circuit (IC) is a dedicated device which does only this job, and does it well. Of course, not every reader will be familiar with op-amps or previous articles, so here is a very brief recap.

Operational amplifiers are an IC with high-impedance inputs which means they are voltage-sensitive rather than current-sensitive. One input is inverting and the other is non-inverting, which means that a positive voltage on the inverting input will cause a negative voltage on the output, and a positive voltage on the non-inverting input causes a positive voltage on the output. The reverse is also true. Op-amps are often driven from a positive and negative split rail power supply with a zero volt ground in the middle, allowing them to operate negatively or positively. In single rail situations, a synthetic ground is arranged.

Op-amps can be arranged as a comparator, where one input is compared to the other and the output goes high or low depending on the situation in the inputs. Op-amps have many other modes of operation, but the comparator is the one relevant to this article.

Usually, this is done by using a voltage divider to produce a known voltage, and then feeding it to one of the inputs. The other is used to measure the target signal, such as the voltage on a sensor. The output will go high or low, depending on set-up, as the sensor voltage goes above or below the reference.

Voltage comparators like the LM311 do this with high accuracy, fast response, and minimum external component count. The LM311 is designed with versatility in mind as it can operate from a range of supply voltages, including +/- 15V DC, and +5V DC for logic circuits. The minimum operating voltage unfortunately rules out 3.3V operation. The outputs rated highly enough to be capable of driving some loads without additional amplification.

As always, having a copy of the manufacturer’s datasheet for the IC is useful. We always summarise the most relevant information for our maker audience, but there is always more in the datasheets. We used Texas Instruments’ LM111/LM211/LM311 document, revised March 2017. While specifications should be the same for all manufacturers the location of information, and indeed the amount of information may vary.

Note: To learn more about op-amps, you can refer to our previous Classroom articles, including ‘Operational Amplifiers - Super Electronic Building Block’ from Issue 7, January 2018 or ‘Operational Amplifiers...Revisited’ from Issue 13, July 2018.

BASIC OPERATION

While we very briefly recapped the operation of comparators, we’ll give a bit more detail here. The basic comparator has five connections: Inverting input, non-inverting input, as well as an output, positive power supply, and negative power supply. Because the LM311 can operate from single rail supplies, and we are going to use it that way, this is how we’ll move forward.

In a basic op-amp, the parameter measured is the difference in voltage between the two inputs. The difference is what is amplified and transferred to the output, not the actual voltages at the inputs. In a comparator, the difference is not measured as such, but compared, and the output is fully on or fully off depending on the difference between the inputs.

If the voltage at the non-inverting input is more positive than the voltage at the inverting input, the output is on. If the voltage at the non-inverting input is less positive than the voltage at the inverting input, the output is off. In other words, if the difference between the inputs is positive, the output is fully on, regardless of how big or small the difference is.

There is a caveat with this. For most purposes, the output is fully on or fully off. However, there is a small section of the response near the reference crossing threshold where the response is in fact not absolute.

You won’t see this in practice very often, but we did encounter it while developing a light jar project based on this month’s Classroom circuit. The graph Figure 8 in the datasheet shows that there is a short, steep response that is almost linear. The entire effect spans less than a millivolt at the input, so it won’t bother most users too often.

SOURCE: Texas Instruments

Note that we have not included power supply connections in these diagrams. This is because the information is valid for comparators in general, including those made with external components from discrete op-amps, whether operating from a bipolar (dual rail) supply or a unipolar (single rail) supply.

This also means that the voltages at both inputs could be negative, but as long as the difference is positive, the output is on.

ELECTRICAL CHARACTERISTICS

For most maker purposes, the LM311’s power supply options are adequate. It will work easily with minimal fuss from a +5V single rail power supply, something that cannot be said of all op-amps and comparators. Although most op-amps and their derivatives can be made to work from a single rail supply, those devices designed to do so make circuit design easier. Current draw is different for different supply situations, but was measured on the workbench as 1.19mA from a +5V supply with no load.

The LM311 is quite an old device, and the maximum input differential (the difference between the voltages at the inputs) is 30V (+/- 15V). This is appreciable less than the supply voltage of 36V (+/- 18V), and is sometimes stated as a detracting factor from the device. This may be true for engineers, but for most makers, it is irrelevant.

Most of our projects are powered by 5V or 12V, with a smattering of 24V projects doing the rounds. As these are usually single rail supplies, there will not be a situation in these cases where the gap between the maximum input differential and the maximum supply voltage becomes a problem. It is still a good choice because it is rugged, simple, and very easily available at the retail level. Few other comparators can be bought over the counter at your local electronics store.

As will all op-amps and their derivative devices, the LM311 has very high impedance inputs, with a maximum required input current of 300nA, with the typical figure given as 100nA. Switching time also varies depending on the input and output conditions but all figures in the datasheet tables and graphs are below 200ns, and even with large voltage ranges in supply and signal, the largest figure we found in any literature was under 1ms for a full swing.

The simplified diagram shows the output of the LM311 as an N-channel transistor with open collector and emitter terminals. It can handle a maximum of 40V at 50mA, which means it can switch many relays on its own, or be used with an external transistor for switching larger loads.

Looking at the functional block diagram on page 10 of the datasheet, more transistors and some resistors can be seen involved in the output. Because the output is not a truly floating transistor, the load needs to be referenced to Vcc+, GND, or Vcc-, as relevant to the power supply and project requirements. For our purposes, the emitter output connects straight to ground, and the load connects to the collector. While there are other ways to use these pins, and reasons to match, they are beyond this article.

THE PHYSICAL PACKAGE

In the retail market, the LM311 comes in an 8-pin dual in-line plastic (DIP) package. It may be possible to find surface-mount versions too, but we were unable to find an Australian retailer for these. Other packages exist, shown in the datasheets, which you may encounter while searching if you’re not buying from our regular suppliers. They are generally available on a commercial basis only.

For single-rail supply, the Vcc- pin becomes the ground pin, while the Vcc+ pin retains its role. Other pins that may need further explanation are the BALANCE and BAL/STRB pins. The balance pin is used for externally offsetting the non-inverting input. All manufactured devices have a degree of tolerance, as we are familiar with from resistors and transistors. The LM311 is no different, and the balance pin is used to manually adjust when the zero-crossing occurs. While it is good to know what this does, it is not used often in maker circuits.

The Balance/strobe pin has an additional feature - grounding it will turn the output off, regardless of input states. This can be used as a manual override to disable the output. While we will be using a power switch to deactivate our build later on in the circuit, the strobe input can be useful for microcontroller-based circuits where the LM311 may be used as a digital input.

Generally, the two pins are left unconnected or shorted together to avoid spurious activity.

USING THE LM311

In practice, using a comparator is fairly simple. A reference voltage needs to be fed to one of the inputs, and this is usually done using a voltage divider. The voltage to be monitored is fed to the other input. In most applications, the reference voltage is connected to the non-inverting input, and the signal to be monitored is connected to the inverting input.

Because we’re focusing on single rail power supply use, the Vcc+ pin connects to the positive supply rail, while Vcc- connects to the ground or 0V rail. The emitter of the output should connect to ground, while the collector connects to the negative side of the load. Remember that the output can handle 50mA, so if your load is not current-limited to this or lower, you will need to either use a transistor, or current limit your load with a resistor if suitable.

Note that, as the output of the LM311 is an N-channel transistor, there is more than one way to increase the current at the output. In diagram 4A, we have used a PNP transistor. Current flow in the HIGH (output on) state is shown.

In diagram 4B, we have used an NPN transistor. Generally, NPN transistors are not used as high-side switches because the base current must flow through the emitter to ground, and doing so through the load can be problematic.

Using them as a low-side switch can also result in the fairly small resistance across the transistor in its ‘on’ state giving an imperfect ground if you are switching a whole circuit on or off with the output transistor. An LED won’t care, but a microcontroller or other sensitive circuit probably will. So, a high-side switch suits certain applications, and a PNP transistor should be used for a high-side switch.

Using an NPN transistor as a high-side switch is possible. However, if there is anything about the circuit being driven which means that the base current cannot flow easily to ground, such as in a high-resistance circuit or with back-EMF from an inductive load, the transistor will not function correctly.

We have used an NPN transistor in the past as a high-side switch, with very simple low-resistance loads such as LEDs. Sometimes this is for editorial reasons rather than technical reasons.

In simple circuits, it can be hard to come up with something that does not already exist elsewhere, and doing things like making a high-side switch from an NPN rather than PNP transistor can help avoid too much similarity to already-published material. So, while you might see it done, it shouldn’t be the first response.

Hands On:

Parts Required:JaycarAltronicsCore Electronics
1 x LM311ZL3311Z2516COM-13950
1 x LED *ZD0152Z0860COM-09856
1 x LDRRD3485Z1621ASEN-09088
1 x 150Ω Resistor*RR0552R7538COM-05092
1 x 2.4kΩ Resistor *RR0581R7567COM-05092
2 X 4.7KΩ Resistors *RR0588R7574COM-05092
1 x 10kΩ PotentiometerRP7510R2227ADA562

Parts Required:

* Quantity shown, may be sold in packs. You’ll also need a breadboard and prototyping hardware, and a power supply of choice within the voltage limits. A regular benchtop power supply or 12V plugpack would be fine.

We’re going to present a very simple build this month, but we’re going to modify it as we go along. We’ll use it to explore the behaviour of the LM311 with different input configurations. The schematic will be our starting point, and the output will remain the same. It’s just an LED to tell us what’s going on. We’ll use an LDR as our input sensor, first with a fixed-resistor voltage divider, then with a potentiometer giving a variable reference voltage.

The build is particularly simple because we are going to use what we have learned in a separate project. Coming up in issue #039, our Firefly Light Jar project uses a LDR with an LM311 comparator to turn on an LED string when the ambient light level has fallen to a chosen level. Be sure to check it out.

We found that LDRs vary, so you may need to change the resistor value of R1 to suit. Our LDR measured a resistance of 141Ω in full winter sun, and over 40MΩ in complete darkness with only the multimeter backlight in the room. In the shade inside the windows on a sunny day, we measured 850Ω, and with a hand covering the surface of the LDR in the same conditions, we got 4348Ω. For this reason, we’ll make our voltage divider with the LDR and a 2.4kΩ resistor.

First, connect the breadboard and components as shown in the schematic and fritzing.

Note what happens when you cover the LDR. How much do you need to cover it for the circuit to activate? Do you need to cast a shadow or wrap it almost entirely in a black tape? R2 and R3 should be the same value as each other, so the voltage at their junction with the non-inverting input is half the supply voltage.

Now it is time to start making changes. The first change will be to swap the fixed-resistor voltage divider for a potentiometer. Remove R2 and R3, and connect a potentiometer as VR1 with one end connected to the supply rail, the other end connected to the ground rail, and the wiper connected to the non-inverting input of the LM311, pin 2.

Now you should be able to adjust the reference voltage so that the output LED turns on when you want it to. Try setting VR1 so that the LED turns on with just a shadow cast over the LED, then set it so the whole circuit has to be in a completely dark room for the LED to turn on.

With the potentiometer now giving control over the reference voltage, we can explore what the position of the components does to the circuit. Swap the position of LDR1 and R1, and see how it affects the circuit. How far and what way do you need to need to adjust the potentiometer to get the circuit to trigger?

Swap back to the original configuration of LDR1 and R1, but connect the junction of LDR1 and R1 to the non-inverting input, and the wiper of the potentiometer to the inverting input. How does the circuit behave now?

Now that you’ve had a chance to experiment with and explore the behaviour of the LM311, you’ll be in a better position to decide how to use it in projects. Of course, we’ve only explored the LDR as a sensor, but anything that works within the input voltage limits will work. A piezo force gauge, hall effect sensor, IR LED proximity sensor, they all have possible uses with the LM311. The age of the device does present some limitations but for most makers, these are of little, if any, relevance. Combined with its ready over-the-counter availability, the LM311 is still a versatile and useful device for the maker.

Make sure you keep an eye open in the near future for our Firefly Light Jar project, based on the circuit we’ve been exploring in this issue of Classroom.