Pages

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:

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.

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.

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.

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