3. Converting an Analog Sensor Voltage to a Digital Output

(article continued from previous page)

An LED put into a circuit backwards from the usual polarization can sense light like a photodiode. The circuit on the prior page show how this signal can be amplified by an op amp and can then be compared to an arbitrary voltage level to act as a color light detector.

The following pages look more closely at the signal coming from a reversed-oriented LED. However, since it is acting as a photodiode, I'll refer to the reversed LED simply as a “photodiode” since it is a more concise term.

By the way, the information presented here is equally applicable to other types of photosensors and other weak signals sources.

Recall that the sensor signal is converted into a digital (on/off) value through the components connected to the top half of the op amp, as shown on the schematic presented earlier. The oscilloscope traces on this page show the improvement of adding hysteresis (R6 and R7) to the digital comparator.

Perhaps your robot or device will use software instead of hardware to interpret the sensor’s meaning. Even so, your software algorithm can benefit from the lessons learned about converted a noisy, slow-moving analog signal into a distinct digital value.

Unamplified Photodiode

Oscilloscope trace of the voltage of an unamplified photodiode receiving increasing amounts of light.

Oscilloscope trace of the voltage of an unamplified photodiode receiving increasing amounts of light.

The above picture is the unamplified voltage level (signal) coming from the photodiode (LED1). From left to right, the voltage level gradually meanders upwards.

On the leftmost side of the picture, the voltage level is generated without much reflected light; primarily ambient room lighting and sideways leakage from the nearby LED (LED5). On the rightmost side of the figure, the voltage level is generated from a significant amount of reflected light; out of LED5, reflected off an object, and into the photodiode (LED1).

The total difference from the low point to the high point is about 1 volt. Although not unusable, that’s not much of a signal. Even worse, the voltage level increases gradually, sometimes drooping a bit here and there, sometimes it spikes briefly.

Looking at the picture, can you tell at which point the color (or reflective object) can be considered detected? It’s completely arbitrary. There isn’t a right or wrong answer.

Trip Level - Converting To Digital

Recall that the potentiometer (R8) sets the voltage level that is considered “detected”. The second half of the op amp circuit outputs either high or low, depending on whether or not the photodiode signal voltage level is above or below the potentiometer voltage level.

But, here’s the problem. Since the photodiode signal naturally has spikes and dips, it is not uncommon for the photodiode signal to briefly appear above and then below the potentiometer voltage level as the photodiode voltage level gradually rises or falls. Without the benefit of being able to look ahead at the trend, the op amp chip rigorously compares the photodiode to the potentiometer and often changes output states a few times before the photodiode’s voltage level is either a high enough or low enough that the spikes and dips don’t matter.

Oscilloscope trace of a bouncing digital signal due to the conversion of a borderline analog voltage.

Oscilloscope trace of a bouncing digital signal due to the conversion of a borderline analog voltage.

Bummer. I wanted a nice sharp edge where the color sensor reaches the desired detection level. But the digital value jumps up and down.

This isn’t the op amp’s fault. There’s nothing wrong with the signal or the circuit. Reality is messy and doesn’t always conform to a clean digital “detect” or “no detect”.

Depending on the robot, the output shown above may be acceptable. The digital signal is fine if the robot can handle “An object is detected. Oh, no, it isn’t. Wait! Yes it is. Umm. Okay. Yes.” For example, a robot that roams hallways full of people and rooms full of junk will be programmed with an algorithm that can deal with on-again-off-again encounters.

Digital Comparator with Hysteresis

The circuit can be improved very simply by feeding back some of the op amp’s output into the potentiometer to dynamically change the trip point. When the op amp’s output is low, the potentiometer’s voltage level will be lowered a bit. When the op amp’s output is high, the potentiometer’s voltage level will be raised a bit. By feeding the current state into the next comparison, the op amp is able to consider the trend.

Cool, huh?

This technique is called positive feedback, as a high output supports a continued high output, and a low output supports a continued low output. If the amount of positive feedback is larger than the dips and spikes, then the output won’t change back and forth during transitions. This results in the solid, decisive output shown in the next oscilloscope image.

Oscilloscope trace of a nice clean digital conversion of a gradually rising analog voltage.

Oscilloscope trace of a nice clean digital conversion of a gradually rising analog voltage.

As you can see, for the small cost of two measly resistors (R6 and R7), the short-term output is cleaned up a lot. This frees the robot’s brains from dealing with lots of false changes in detection states.

That’s fine for a slowly rising signal. But, does the hysteresis trick work for a faster signal that is going up and down?

Oscilloscope trace of a nice square pulse wave converted from a rapidly changing analog voltage.

Oscilloscope trace of a nice square pulse wave converted from a rapidly changing analog voltage.

Yes, hysteresis still permits a fast pulse train!

Extra Pulses and Missing Pulses

Oscilloscope trace shows bouncing in what should have been an evenly-spaced square wave.

Oscilloscope trace shows bouncing in what should have been an evenly-spaced square wave.

As you can see above, hysteresis through positive feedback isn’t a complete miracle cure. If the signal is bad enough, bouncing can still occur.

Oscilloscope trace shows a missed pulse in what should have been an evenly-spaced square wave.

Oscilloscope trace shows a missed pulse in what should have been an evenly-spaced square wave.

Rapid oscillations are one extreme. Missed pulses are the other extreme.

If the signal is weak enough, positive feedback can result in missed pulses, since positive feedback tends to keep the output in the previous state unless a significant voltage level change is encountered. Note the extra wide pulse in the above image when a low level should have occurred in the middle of it.

Let’s face it: a bad signal is a bad signal. All in all, positive feedback results in a more logic-friendly output for most cases.

Is there a way to improve the quality of the signal in the first place, rather than just improving the interpretation of a low-quality signal? Click the Next link to find out.