Friday, October 15, 2010

Comparator With Automatic Voltage Reference

I'm working on building a circuit to adapt the output of wheel encoder sensors to a microcontroller. The output of these IR reflectance sensors is a sine wave, with some issues. Here's the general problem:

Let's say you have a sine wave, and the amplitude varies over time, and the signal is modulated by a lower frequency sine wave.  (In my head I was thinking it was kind of like if the DC offset were varying over time but of course DC doesn't vary...)  The sine wave has to be converted to a pulse train with a voltage comparator. Just pick a reference voltage and anytime the signal is higher than the reference, the comparator output is +5V (ish) and if it's below then it's 0V (ish). You can use a simple voltage divider or a potentiometer to set the reference voltage like so.

Comparator with potentiometer voltage divider for Vref

Simply using a voltage divider as a voltage reference to trigger the comparator isn't good enough. At best, the pulse width will vary as the sine wave varies. That can cause some inaccuracy in measuring speed and distance.

At best, pulse width varies and so does accuracy
At worst, you might miss pulses entirely if you can't get the voltage reference set just right or if the DC offset varies too widely.

It's possible to miss pulses with a fixed reference voltage
Wouldn't it be nice if we could automatically remove the signal component and leave behind the modulation and use that for the comparator reference voltage? Well, we can, and it's not that hard.

All we have to do is use a low pass filter. We filter out the higher frequency output of the wheel encoder sensors, and retain the modulation signal.  Basically what we're trying to do is adjust Vref to be a value between the upper and lower peak of our signal.  We take care of amplitude modulation at the same time.  This same approach can be used to automatically detect the DC offset of a signal and use that as Vref.

A simple low pass filter is more than adequate and keeps parts count low on the 1"x1" printed circuit board I'm designing. Pick a resistor in series with the signal input, and place a capacitor in parallel, and feed the output to the reference pin of your comparator. Like this:

Auto Vref using low pass filter to detect DC offset/amplitude fluctuation
You can experiment in LTSpice or in real life with different values for R and C but the formula to find the cutoff frequency is given by:

A higher value of R and C means the circuit cutoff frequency decreases, approaching but never reaching 0 (DC). You can imagine that a giant capacitor and resistor would filter out most ripple and a tiny resistor and capacitor wouldn't.

I measured the encoder frequency at approximately 100-200Hz. The slope of the frequency response curve for this type of filter is somewhat shallow so there is a tradeoff between the amount of higher frequency attenuation and how quickly the circuit reacts to changing DC offsets.

Frequency response of filter with higher cutoff frequency; -30dB at 100Hz

This first picture (above) shows a higher cutoff frequency and so it is more sensitive to changing DC offsets, but doesn't attenuate the encoder signal as much (-30dB at 100Hz). Which really isn't that big of a deal, as long as there is some attenuation.

Lower cutoff frequency attenuates the signal at 100Hz by -50dB
The picture above shows a lower cutoff frequency and so there's greater attenuation of the higher frequency encoder signal (-50dB at 100Hz). But if the DC offset fluctuates too rapidly, this circuit won't keep up. Because it's attenuating lower frequencies more, too.

In my case, a value of R=10k and C=1uF is a nice happy medium for my particular situation. More compact, low value capacitors didn't attenuate enough and higher capacitance prevented the circuit from tracking DC offset properly.

In the first video below you can make out the larger encoder waveform and the attenuated waveform that makes Vref. Notice how it tracks nicely between the peaks of the signal.

The second video shows the comparator output versus the encoder signal. Notice that the pulse width of the output remains fairly stable despite the DC offset and amplitude fluctuations in the signal.

Some of you may have noticed the phase shift introduced by the filter. This has the added bonus of adding hysteresis to the circuit making it more immune to noise.  [EDITED 11/07/2010]


  1. Hi,

    another way would be to get rid op the DC in the signal by means of a series capacitor. that is how audio amps do it. would that have been valid in your case too ?

  2. Thanks for the comment/question. You're right, capacitors in series cancel DC. But I wanted the opposite: to cancel AC and keep the DC.

    Well, technically, what I really needed to do was to filter out the AC encoder signal and keep the lower frequency AC modulation that was caused by a wobbly wheel.

    The phrase I originally used was "fluctating dc offset" and it is kind of dumb on my part. :) DC doesn't fluctuate of course. I meant to better illustrate the concept: that the higher frequency encoder signal was modulated by another signal.

    Since the two frequencies were pretty close and my filter had a pretty shallow cutoff, I wasn't able to completely filter the encoder signal.

  3. Thanks, I read the post wrong, and thought you wanted to eliminate the wobble, instead of the higher freq. signal.
    thanks for your explanation !

  4. Hi and thanks for the nice post.
    I'm working on a project that requires DC feedback ultimately to remove DC drift from the input or (original signal). Might you help me understand how I could get the similar effect.
    I'm only interested in the DC component, semantically the a very long duty-cycle AC signal that my original signal rides on....
    I guess, for starters, How can a generate a DC voltage output generated by PWM drive signal?

    Thank you to all for your kind support,I kinda new at all this stuff...thanks


Note: Only a member of this blog may post a comment.