VOLTAGE COMPARATORS: The Most Misunderstood and Underutilized Peripheral in Microcontrollers

May 3, 2007
By Keith Curtis,Microchip Technology Inc. Voltage comparators began appearing in single-chip microcontrollers (MCUs) in the late 1990s. At the time, the change was viewed as a simple cost reduction. Comparators required less silicon,

By Keith Curtis,Microchip Technology Inc.

Voltage comparators began appearing in single-chip microcontrollers (MCUs) in the late 1990s. At the time, the change was viewed as a simple cost reduction. Comparators required less silicon, while still allowing the microcontroller to compare two analog voltages. This "single-bit ADC" view of the voltage comparator persisted for much of the latter half of the 1990s, and well into the early years of the 21st century.

Fortunately, as 8-bit MCUs began moving into a number of mixed-signal applications, more designers with analog backgrounds began working with MCUs. The resulting new breed of mixed-signal MCU designer was familiar with the flexibility and power of voltage comparators, and began to exploit its capabilities. Applications including sensor-to-digital conversion, logic gates, amplifiers, and power conversion began to appear, using the on-chip voltage comparators.

Unfortunately, the number designers has yet to achieve the critical mass required to effectively "spread the word" about the voltage comparator, so this article intends to open the eyes of designers to the wealth of mixed-signal applications possible with the humble voltage comparator.

Let’s start with sensor-to-digital conversion. Most analog sensors produce a change in resistance, inductance, or capacitance that is proportional to the environmental factor they are measuring. A thermistor varies its resistance in proportion to the temperature, a humidity sensor varies its capacitance, and some proximity sensors even vary their inductance. The traditional method of conversion is to convert the resistance, capacitance or inductance into a voltage and then convert the voltage to a digital value with an ADC. However, what if you could convert the output of the sensor directly into a digital value?

Using a simple relaxation oscillator, you can convert resistance, capacitance or inductance into a variable frequency and then measure that frequency using a timer peripheral. Figure 1 shows two simple oscillator circuits. Besides the obvious advantages of simplicity, both circuits are also relatively immune to noise, due to their inherent averaging of the incoming signal. Their resolution is also determined by the length of time over which the sample is counted.

In both circuits, resistors R1, R2 and R3 provide hysteresis, shifting the trip level of the comparator up and down with the state of the comparator output. R4 and L1, in the left circuit set the frequency of operation, as do R4 and C1 in the right circuit. Replacing R4, C1 or L1 with a resistive, capacitive, or inductive sensor, we create a variable-frequency oscillator in which the frequency is determined by the value of the sensor. The frequency is then converted to a digital value using Timer0 and Timer1. Timer1 counts at the frequency of the oscillation, and Timer0 sets the sample period. When Timer0 rolls over, Timer1 is stopped and the result of the conversion is retrieved from Timer1.

The combination of a few external components, some software, and a couple of internal timers provides a method for measuring resistance, inductance or capacitance using a comparator.

In addition, most new MCUs with on-chip comparators also include a 2:1 or 4:1 analog multiplexer on the inverting input. This lets you multiplex the converter between as many as four sensors by simply adding a resistor (R4) for each of the sensors and routing the sensor/resistor junction to the various multiplexer inputs.

Producing logic gates is simply a matter of combining diode logic with a few resistors to create the necessary logic functions. Figure 2 shows aple that implements both the AND and OR functions, along with the slightly more complex XOR function.

The circuit on the left in Figure 2 is used for both AND and OR functions. To make an AND gate, choose values for R3 and R4 that set the inverting input above VDD/2. To make an OR gate, choose values that set the inverting input slightly below VDD/2. (R1 and R2 should be equal in value.) In the AND configuration, both A and B must be high to pull the non-inverting input above the VDD/2 point, which causes the output to go high. In the OR configuration, either A or B must be high to pull the non-inverting input to VDD/2, which will pull the output high. To make either the NAND or NOR, simply swap the inverting and non-inverting inputs.

The circuit on the right in Figure 2 is used for the XOR circuit. If either A or B is low, then the inverting input is clamped to 0.7V, and the other high input generates a high output. If both A and B are high, then the input at the non-inverting input is held slightly below VDD, while the inverting input is pulled to VDD, resulting in a low output. Note that for any of the logic circuits, the resistor values chosen should be large enough to keep all currents in the 1 to 10 mA range, so the output drive in the comparator can drive the logic easily.

Next, let’s examine how a comparator can be used as a low-frequency op amp. Any variable-duty-cycle digital signal can be converted into a DC voltage by simply filtering the pulse chain through a sufficiently low frequency low-pass filter. To make an op amp out of a comparator, we will use this same averaging capability of the filter for generating both the feedback and output voltages. Figure 3 shows the schematic of the resulting op amp.

In the non-inverting circuit, R1 and R2 fix the gain, just as in a normal op amp circuit. C1 and R3/C2 act as filtering components to average the PWM digital signal at the output of the comparator, and use it as a DC level for feedback and the linear output of the circuit. In the inverting circuit, R4 and R5 fix the gain and C3 and R6/C4 act as the averaging filters to translate the digital PWM to linear voltages. Note that in the inverting topology, R7 and R8 are needed to generate the virtual ground for the circuit.

Finally, how about a switching power-supply circuit? One method for generating an alternate supply voltage is to generate a PWM switching signal that is gated by voltage feedback from the output. In this circuit, one comparator generates a ramp waveform, and a second provides the feedback from the output voltage. The schematic in Figure 4 shows how this is implemented with two comparators.

In this circuit, comparator U1A is a pulse generator, similar to the sensor-to-digital oscillators shown above, operating at a frequency determined by R4, R5, and C1. Resistor R5 is included in the circuit so that the charge voltage on C1 never goes lower than about 1.5V. This is important because U1B controls the operation of the oscillator by pulling the non-inverting input of U1A to approximately 0.7V, which shuts off the oscillations. Note that the oscillator is designed to pull its output low when turned to off, so Q1 will also be shut off.

When the oscillator is running, Q1 is regularly turned on, building up a current flow in L1. When Q1 is turned off, the current flow in L1 forward biases D3 and charges C2--charging up the output voltage. A sample of the output voltage on C2 is then scaled and compared to the forward voltage of D2. If the output voltage is too high, U1B shuts off the oscillator and C2 discharges into the load, dropping the output voltage. When the output voltage falls below the desired voltage, the output of U1B goes high and the oscillator restarts, pumping current back into C2.

There you have it. Circuits for converting R/C/L sensor outputs into digital values, logic gates, amplifiers and even switching power supplies, all built out of discrete components and comparators.

Next time you search for a MCU and find one with comparators, take a minute and think about the complex functions you could build. Comparators can save material costs and you can impress your peers when you show them just how powerful a simple voltage comparator can be.

Keith Curtis is a Principal Applications Engineer, Security at the Microcontroller and Technology Development Division of Microchip Technology. He can be reached at [email protected]

Company: MICROCHIP TECHNOLOGY INC.

Product URL: Click here for more information

Sponsored Recommendations

Near- and Far-Field Measurements

April 16, 2024
In this comprehensive application note, we delve into the methods of measuring the transmission (or reception) pattern, a key determinant of antenna gain, using a vector network...

DigiKey Factory Tomorrow Season 3: Sustainable Manufacturing

April 16, 2024
Industry 4.0 is helping manufacturers develop and integrate technologies such as AI, edge computing and connectivity for the factories of tomorrow. Learn more at DigiKey today...

Connectivity – The Backbone of Sustainable Automation

April 16, 2024
Advanced interfaces for signals, data, and electrical power are essential. They help save resources and costs when networking production equipment.

Empowered by Cutting-Edge Automation Technology: The Sustainable Journey

April 16, 2024
Advanced automation is key to efficient production and is a powerful tool for optimizing infrastructure and processes in terms of sustainability.

Comments

To join the conversation, and become an exclusive member of Electronic Design, create an account today!