2. Quadrature Encoder Digital Square-Wave Output

(article continued from previous page)

The comparator chip has two independent comparator circuits in it. Sensor A is connected to one comparator and sensor B is connected to the other comparator. As a sensor sees a light or dark reflection the associated comparator outputs go high and low, producing a stream pulses whose rate matches the spinning disc. Because sensor A and sensor B aren’t looking at the same spot on the target disc, the pulses occur at slightly different times.

An attached microcontroller can count the number of pulses on a particular sensor to determine the distance the robot has traveled. Or, the microcontroller can count the number of pulses in a minute and display that information to become a tachometer.

Digital square wave output from a quadrature encoder looking at an optical target disc spinning at 13714 RPM.

Digital square wave output from a quadrature encoder looking at an optical target disc spinning at 13714 RPM.

Because the comparator outputs are already clean digital signals, the receiving microcontroller does not need to spend precious processor cycle time converting analog signals to digital. In fact, if the comparator outputs are connected to interrupt pins on the microcontroller, the microcontroller can have the appropriate subroutine called automatically whenever the signal changes state.

Even though the signal is very clean and the comparator has already applied hysteresis to avoid jitter (bouncing or rapid flipping back and forth), the microcontroller can be programmed with an algorithm to check the pair of outputs for a specific ordered pattern such that a glitch in one signal won’t add errors to the count.

Avoiding Noise by Converting to Digital Immediately

As discussed earlier, by placing the comparator in close proximity to the actual photosensors, the relatively low-power (easily corrupted) analog sensor signals are converted into relatively high-power digital signals before making the long trip back to the microcontroller and display.

Close up of the digital square wave output from a quadrature encoder showing a solid, glitch-free signal.

Close up of the digital square wave output from a quadrature encoder showing a solid, glitch-free signal.

As you can see, this results in clean (noise free) pulses, even when measuring near an extremely electrically noisy source, such as a Dremel.

I didn’t measure the speed of this trace on the oscilloscope at the time. But, a close approximation can be obtained by noticing that each grid is 1 ms wide (according to the legend at top). Peak-to-peak is about 4 1/3 squares -- which is 4.33 ms.

Or, I suppose you could examine the trace in a graphics program:
140 pixels peak-to-peak wave / 32 pixels per millisecond = 4.375 milliseconds per wave
4.375 milliseconds per wave / 1000 milliseconds per second = 0.004375 seconds per wave
1 / 0.004375 seconds per wave * 60 seconds per minute / 1 revolution per wave = 13714 rpm

Oscilloscope trace of square-wave outputs from a quadrature encoder shows the target is spinning at 22 thousand RPM.

Oscilloscope trace of square-wave outputs from a quadrature encoder shows the target is spinning at 22 thousand RPM.

At full speed, my old Dremel manages about 22069 RPM (367.82 Hz * 60 seconds per minute = 22069.2 RPM). This is a little slower than the 30000 RPM maximum listed on the tool. Old age and air resistance from the large-diameter thick-paper target disc probably contributed to a reduction in maximum speed.

Nevertheless, this new optical encoder produced results that the analog sensors on my old tachometer could not. This can cleanly measure a Dremel at full speed without electrically noisy results.

Noise From Fluorescent Lighting

At some point I was testing the hysteresis. To do so, the target disc was slowed down to about 120 RPM to observe the effect of the slow transition from black-to-white and white-to-black. Yet, even in these ideal conditions, I noticed a steady sinusoidal wave at the bottom of the phototransistor voltage.

The noise at the bottom of the oscilloscope trace is from AC fluorescent overhead lighting.

The noise at the bottom of the oscilloscope trace is from AC fluorescent overhead lighting.

It appears to be about 40 Hz. That’s not exactly a frequency I expect from AC or fluorescent lighting ballast. However, it would not be surprising that a waveform present in the room lighting would be picked up by light sensors. So, I decided to try turning off the room lights.

Oscilloscope trace showing a much steadier base voltage when the room is lit by a flashlight.

Oscilloscope trace showing a much steadier base voltage when the room is lit by a flashlight.

When the workspace is lit by a flashlight, the 40 Hz background noise disappears. (When I turned on the room lighting again, the 40 Hz noise reappeared!)

Interestingly, the lowest voltage point is slightly lower than before, since the flashlight is unable to produce as much direct or reflected ambient light into the phototransistors.

The light from the flashlight isn’t necessary for the circuit to work. The circuit works in complete darkness because the red LEDs illuminate the optical disc. However, I wanted to see if light from a battery-operated DC source would introduce any noise into the encoder circuit. In particular, I wanted to see if there was a weakness in the phototransistor amplification or in the comparator chip.

Although noise is not desired in the encoder, perhaps there is some way that a robot could recognize rooms by their unique ambient frequency signatures?

Finally, how is the encoder circuit implemented and how is it used in a robot?