An optical encoder uses reflected or interrupted light to detect the motion of a spinning disc that is usually attached to a motor. By using two sensors aimed at different locations on the target disc, the encoder produces a quadrature output (a pair of out-of-phase waves) that a microcontroller can use for calculating speed and direction (forward or reverse). With this information, a robot can keep track of the distance it has moved, regardless of battery strength or the load it is carrying.
Unfortunately, my homemade tachometer is susceptible to false readings because the infrared phototransistor sensor that measures the reflected light is at the end of a long cable. Electrical noise (from the motors being measured) can distort or overwhelm the unamplified signal.
To overcome that problem, the encoder described on this page places the sensor and analog-to-digital converter on the same board. By doing so, a solid digital square wave is delivered to the microcontroller or measuring instrument.
The finished encoder PCB has only four interface pins: 5V, GND, digital output A, and digital output B.
Dremel rotary tool with optical target disc and an encoder board held by a helping-hand soldering tool.
The picture above shows the encoder test rig for measuring the speed of a Dremel variable high-speed rotary tool. The Dremel is extremely difficult for my tachometer to measure, because of its very high speed and because of all the electrical noise it radiates. Therefore, the Dremel is the perfect stress test for my new digital encoder.
In practice, I held the Dremel’s target encoder disc a little bit closer, a little bit lower, and more centered left-to-right than is shown in the photograph. Adjusting the position of the encoder disc makes a tremendous difference in output reliability and the phase difference between the two sensors on a quadrature encoder.
The encoder disc is split into half black and half white, such that the photosensors will detect darkness (lower voltage) or brightness (higher voltage) as the disc rotates. Having two sensors allows detection of the direction (clockwise versus counterclockwise) of rotation as well as permits extraneous pulses of a single sensor to be ignored.
An optical encoder PCB showing the LEDs, photo transistors, and capacitors.
The front of the encoder contains four ultrabright red LEDs, whose light reflects off of the encoder disc surface and into two photosensors (phototransistors in this case) nestled between the red LEDs. Black shrink-wrap tubing slipped around each photosensor reduces the amount of light being received directly from the LEDs. The light we wish to measure is that which is reflected off of the encoder disc.
Instead of using infrared emitters, I use ultrabright red LEDs. This makes it easier to aim, I can see when the board is powered up, and I get visual feedback when changing the brightness. The disadvantage is that a lot more light is required to see a strong reflection, because the phototransistors are less sensitive to visible red light.
A sprinkling of ceramic monolithic and tantalum capacitors reduce electrical noise in the power supply lines. A capacitor located on the comparator voltage threshold steadies the potentiometer’s output and reduces the effects of stray electrical noise. This permits a higher-resistance potentiometer to be used for tuning the analog cut-off voltage between high and low, which decreases power consumption.
The back of the encoder board showing adjustment trimpots, LED indicators, and the comparator chip.
My old tachometer board contains one potentiometer for setting the dark voltage threshold (low signal) and another potentiometer for setting the bright voltage threshold (high signal). Any sensor voltage level in between is considered unchanged (either dark or light depending on the last accepted value). This hysteresis feature prevents indecisive back-and-forth pulses during the transition of seeing the dark or light portion of the encoder disk.
The problems with having two potentiometers for setting voltage thresholds are:
Therefore, unlike my old tachometer board, this new encoder board uses only a single potentiometer to set the threshold of what photosensor voltage is consider dark versus light. Hysteresis is built-in via a fixed resistor. The resistor feeds back the comparator’s digital output into the sensor’s output, which raises or lowers the sensor voltage by 10% up or down. This causes the sensor voltage to hop over the middle voltage level during transitions. There are actually two hysteresis resistors, one on each side of the comparator, so that each sensor’s output voltage is modified independently to match what they see.
The board also features a red LED to show when the first sensor reads “high” and a green LED to show when the second sensor reads “high”. I can tell at a distance that the board is operating by seeing the red and green LEDs flash on and off.
The Texas Instruments TLC3702 comparator chip was chosen for the encoder project because of its:
What does the output look like?