Rotary quadrature encoders often are used to command digital potentiometers or digital controllers, and quadrature decoding is typically performed in a programmable device (like an FPGA or microcontroller). This design performs quadrature decoding with commonly available components, and it comes in handy when you need to use a quadrature encoder in the lab or in a simple product.
This design uses a manually turned contact encoder. Contact encoders are relatively inexpensive, and their internal switches produce switch closures that can be debounced and converted into two voltage pulse trains (often labeled A and B). The pulse trains are 90° out of phase with each other in time due to the encoder’s construction. The design works equally well with an optical encoder, which doesn’t require switch debouncing.
The presence of pulses shows that the encoder is turning (indicating a command), and the order of arrival of the channel A and B pulses shows the direction the encoder is turning (indicating the commanded direction). Figures 1a and 1b show pulse trains A and B (plotted in time) resulting from opposite encoder rotation directions. These pulse trains are called quadrature outputs, and their conversion to a single pulse train and a separate digital signal indicating direction is called quadrature decoding.
The circuit shown in Figure 2 initially debounces the switch signals. When a mechanical switch closes, its contacts can bounce open briefly (and sometimes repeatedly) before settling in the closed state. The debounce circuit, of course, mitigates this characteristic . The circuit functions conceptually like the simplified schematic shown in Figure 3.
After the switch closes, the switch voltage rises exponentially with a time constant of R5 × C6 (1 ms in this case). Any switch bounce (contact closures) during this time simply pulls this voltage back to ground and doesn’t trigger false outputs. In the actual design, a FET is used to short the capacitor to avoid capacitor discharge currents, degrading the encoder switch contacts and limiting the encoder life (Fig. 2, again).
After debouncing, one of the two channels is converted into pulses that are shorter than the minimum duration of a switch signal. These pulses are sent with the second channel to a classic “set/reset” circuit that produces an output indicating the direction of encoder motion. The pulses are then delayed relative to the direction signal. This delay is needed because when the user turns the encoder one “click,” the circuit must indicate the new direction before sending out the pulse.
Components R4 and C4 allow U1b to create a pulse on the falling edge of the debounced signal. The pulse will be approximately 1 ms wide (about the time it takes the voltage at U1, pins 4 and 5, to fall exponentially to U1b’s transition voltage). This pulse is sized to be shorter than the briefest expected contact closure.
In this case, the manually operated encoder generates 24 contact closures per revolution. The fastest expected rate that a user could reasonably be expected to turn the encoder knob is two revolutions per second. This would result in a 50-Hz signal, which is equivalent to a 10-ms switch closure signalcomfortably longer than a 1-ms pulse. Diode D1 prevents the signal at U1, pins 4 and 5, from rising significantly above VCC when the debounced switch signal (U1, pin 3) transitions high.
U2 forms a classic digital “set/reset” logic circuit. Its output (U2, pin 6) will be high or low based upon the phase of the quadrature switch signal (U1, pins 9 and 10) relative to the 1-ms pulse (U1, pin 6). Thus, the output of the “set/reset” circuit (DIR, U2, pin 6) indicates the direction that the rotary encoder is turned. As mentioned previously, each pulse is delayed by the low-pass filter R1 and C1 relative to this direction signal. That’s because the direction indicated by a new pulse needs to be provided before the pulse itself.
Figure 4 illustrates the various circuit outputs. This circuit was used to command digital potentiometers that used negative pulse edges to increment their resistance, so the INC signal (U1, pin 11) is normally high.
I concur with Robert and add that I have seen and worked on several high-end products from lab instruments to defibrillators that suffered from poorly designed mechanical encoder interfaces. A simple test is to put a mark on the control and the panel, then rotate the control rapidly right and left a number of times to see that you always get back to the original setting. Good luck.
The challenge is that the high frequency mechanical switch bounce is hard to distinguish from an encoder pulse. Careful design is needed to properly debounce these devices, particularly as the switch contacts degrade over their lifetime, and still detect motion correctly. It is very easy to get it wrong and you won't know for sure until the product has been in the field for several years. It is also dependent on the switch supplier's quality and long term reliability. Meanwhile, many consumer products have implemented this function to work correctly, over the long term, at low cost.
I have seen it designed wrong but have not done it right myself. There is probably a simple but well designed combination of RC circuitry and debounce timing and filtering firmware that will work for any switch. My guess? I would test a handful of new switches to measure their debounce times on a scope. Maybe even build a fixture to rotate a few continuously for a week, then retest to see how bad the debounce has gotten. Add some margin to your design. Maybe run a few heat and humidity cycles. Maybe you can get (and trust?) the manufacturers life test data.
To debounce, start with an R-C filter with a 1mS time constant and schmidt trigger inputs, then poll the switch at a speedy (about 1mS) rate. Filter it in firmware to detect about 10mS of steady output before calling it a transition.
My guess is this is typically one of those seemingly simple problems where a hardware engineer does some of the job of debouncing the switch in RC hardware, then a software engineer is never told that it is a challenging problem requiring time and care, or what the actual switch debounce time or algorithm is. He sort of gets it working...
Dave Erickson InfraRedX Burlington, MA
Dave Erickson -August 04, 2009
The circuit is indeed simple, however it is important to note its shortcomings. One is that it has a resolution that is 4 times worse that an optimal decoder that counts all four edges of a complete quadrature cycle. But the biggest shortcoming is that this circuit can accumulate error indefinitely. That may not matter for applications where human feedback is compensating for the error in a closed-loop manner. But there are plenty of applications where accumulated error can be a killer. The opportunity for error occurs every time the control direction is changed. Take the simple case where the control is moved back and forth just far enough to open and close the 2-3 switch, leaving the 2-1 switch open. In that case, every time the 2-3 switch opens, a pulse will be generated at the INC output - but the DIR output will not change. So this will be interpreted as continuing rotation in the same direction, and thus we have indefinite accumulated error.
The subject of quadrature decoders has been well-developed, so much so that most designers who use them have come to expect a certain level of performance. When a proposal is presented that fails to meet such a basic requirement as no accumulated error, then that fact should certainly be mentioned.
Robert Scott
Real-Time Specialties
Ypsilanti, Michigan
Robert Scott -July 24, 2009
Your Comments:
Enter the text from the image below
Please refresh the page if you have trouble reading this text.
Search Electronic Design
Email Newsletter
Sponsored By:
Electronic Design UPDATE provides readers with late-breaking news, opinions from industry experts, and timely technology stories. It's a unique opportunity to get your product message in front of engineers, engineering managers, and corporate managers while they're reading about critical information online.