What is a quadrature encoder?
A quadrature encoder is an electromechanical sensor used to measure changes in angular position, rotational velocity, and direction. It can also measure position relative to a known starting position. It features two sensors that are physically offset in relation to a rotating interrupter mechanism. The resulting assembly produces quadrature waveforms. An example is shown in Figure 1. In this example, a pair of Banner DS18VP6LP retroreflective sensors are interrupted by a mechanical shutter.
The term quadrature encoder encompasses a wide range of technologies. Figure 1 presents a classic example featuring optical sensors and an interrupter disk. The disk provides 4 windows per revolution. This appears crude compared to commercially available sensors featuring dozens or even hundreds of windows per revolution. However, even this simple mechanism divides the circle into 16 pieces (22.5-degree increments).
The quadrature encoder is defined by the output signals, not the technology used to develop those signals. Note that the quadrature encoder could also be manufactured using magnets and hall sensors, mechanical switches, and even paper with alternating white and black quadrants. In all cases, a pair of sensors are used to detect the changes.
Figure 1: Picture of a large 3D printed quadrature encoder demonstrator featuring industrial components on a Phase Dock base.
What is meant by the term quadrature?
Quadrature is a relational term describing the phase relationship between two signals. Specifically, it describes a 90-degree relationship. The prime example is the relationship between the sine and cosine waveforms as shown in Figure 2. The trigonometry definition is relevant to this conversation as sin and cosine are fundamentally concerned with rotation of an object.
It is possible to produce an encoder that is based on direct measurement of the sine and cosine signal. A rare, perhaps old-fashioned, analog example is the synchro and servo system where the quadrature or 3-phase signals are measured.
In practical terms, it is considerably easier to use digital methods representing the waveform. This is shown in the lower part of Figure 2 where sensors Q1 and Q2 respond when the corresponding waveform is positive. With this system, we divide the waveform in 4 quadrants. We see that each sensor is positive for two quadrants. There are times when both sensors are on and when both sensors are off.
The model from Figure 2 is better understood by watching Video 1. Observe the state changes follow a predictable pattern described using a 2-bit gray code with a sequence of 00, 01, 11, and 10. Recall that a gray code describes a system where a single bit changes at any given time. As you watch the video, the sensors LED indicators appear to be “walking” downward. Later as the direction changes from clockwise to counterclockwise, they appear to be walking upwards.
Figure 2: Quadrature signals in a forward (CW) direction with the blue signal leading the green. The Q1 and Q2 sensors active when corresponding signal is positive.
Tech Tip: The term quadrature is commonly encountered in electronics and communications. From a historical perspective, some of the first polyphase electric AC motors were driven by signals offset by 90 degrees. This includes the famous Tesla AC motor with strong links to the starting circuit of the modern single-phase AC motor. In another example, quadrature describes a process in radio receivers and transmitters where we use “I” and “Q” signals corresponding to the In-phase and Quadrature-phase signals.
Video 1: Video showing the quadrature encoder with 3 seconds clockwise and 3 seconds counterclockwise.
How do the quadrature signals change as the rotational direction changes?
Up until now we have assumed a constant phase relationship between the quadrature signals. Stated another way, we have assumed a system rotating in a single direction.
Figure 3 shows the change when the direction of rotation is reversed. The quadrature (90-degree) nature of the signals remains. However, the green waveform is now leading. Recall that the original gray code was 00, 01, 11, and 10. It is now transitioned into a 00, 10, 11, and 01 sequence.
Figure 3: Quadrature signals in a reverse (CCW) direction with the green signal leading the blue.
How is position maintained in a system featuring a quadrature encoder?
We are careful to state that a quadrature encoder can measure position relative to a known starting position. Stated another way, the quadrature encoder is not an absolute position sensor as it is unable to determine a shaft’s position as a direct reading. Instead, a digital system equipped with a quadrature sensor must count the number of observed transitions. Such a system must initialize the count at a known “home” or “zero” position. Consequently, all position measurements are interpreted as relative to home. A missed count or loss of memory will result in loss of position.
Once again, we recognize that the corresponding digital system must keep track of the quadrature encoder. It does so by monitoring the “direction” of the sensor’s gray code. The digital system will update the count (position variable) on each gray code transition.
What can go wrong with the quadrature encoder and associated system?
This system level integration requires careful programming. Perhaps one of the greatest challenges is associated with the speed and programming techniques used by the associated microcontroller or Programmable Logic Controller (PLC). If the device is slow or the program is slow (blocking) it will miss the sensor transition(s). Consequently, we must guard against this type of error as well as noise, sensor failure, and intermittent wire issues.
A classic mistake is to monitor the encoder sensors from within the superloop of the program such as the Arduino loop( ) function. The system will appear to function normally but may lose count as the rotational velocity is increased. This problem is compounded when the motor and sensor are incorporated into a feedback system such as the Proportional Integral Derivative Controller (PID). It’s very difficult to debug as the problem may be in the PID code or in the reckoning of position (count).
Use of interrupts will go a long way to eliminating position errors. However, this is insufficient when we consider the importance of maintaining this critical number. It’s especially true when the rotary encoder is used to position a large machine. Such systems tend to fail catastrophically when an errant position number causes the mechanism to crash into the end stops. We must include a robust method to detect errors.
Use of a state machine to monitor the rotary encoder’s gray code
Close examination of the forward and reverse gray codes shows that they are related. This is best seen using a state diagram representation shown in Figure 4. If we “walk” Figure 4 in clockwise direction, we obtain the forward (Figure 2) relationship. If we “walk” in a counterclockwise direction, we see the Figure 3 pattern.
This state diagram provides a framework for an easy yet robust method to determine direction. From within our digital system, we maintain an internal reckoning of state. We then look for appropriate transitions from the Q1 and Q2 sensors. For example, if we are in state 00 there are three valid conditions and one fault condition. This table presents the results based on the sensor inputs.
- 00: stay in current state
- 01: move to state 01 and increment the position count by 1
- 10: move to state 10 and decrement the position count by 1
- 11: fault
Similar tables are required for each state all of which may be derived from Figure 4. Note that there is no return from a fault condition. Instead, the system must be reset, and a homing operation performed as necessary.
As a test condition, suppose the mechanism is on the edge of a transition and is toggling between sensor conditions 00 and 10. This is not a problem for the system as its internal reckoning will bounce between the two states incrementing or decrementing the count on each state transition.
In another test condition, consider what happens when the sensor’s gray code is violated. Recall that a gray code allows a single bit to change. If we are in internal state 00 with a sensor transition to 11, something has gone wrong, and the count is corrupt. The proper response is to enter a fault condition and shut down the system.
Figure 4: State diagram representation of the quadrature encoder signals. The bubbles represent the states while the numbers on the lines represent the sensor values. For clarity the remain-in-state loops have been eliminated.
Homing of a quadrature encoder based system
The homing (zero position) method depends on the complexity of the system. For some rotary encoder applications, a power-on reset to zero or a reset to last known value is sufficient. For example, a rotary encoder may be used as a volume control. When the device is powered on the count may be retrieved from a non-volatile memory location. The audio system now has the same volume as when it was shut down.
A more complex example is a machine with independent X, Y, and Z motors plus encoders. A power on reset is inappropriate because we have zero confidence that the machine’s tool position wasn’t moved when the device was off. For this situation we use the classic homing routine:
- jog to the home position limit switch
- stop on detection of home
- back up slightly
- very slowly move to activate the switch
Perhaps you have seen this algorithm in action. The same technique is used for the stepper motors in many 3D printers. This is an example of another system where position is maintained as a digital count.
Quadrature encoder code implementation
Please bookmark this page.
This is a placeholder for links to future installments. Anticipate a ladder logic based implementation of the quadrature encoder state machine as well as an example written C.
Conclusion
This engineering brief provides a brief introduction to the quadrature encoder. One of the most important considerations is the inseparable nature of the quadrature encoder from the associated PLC or microcontroller. A state machine implementation provides a robust method to guard against a variety of errors including a slow hardware of software PLC or microcontroller implementation as well as sensor and intermittent wiring faults.
Stay tuned for future installments explore rotary encoder with both PLC and microcontroller implementation.
Best Wishes,
APDahlen
About the Author
Aaron Dahlen, LCDR USCG (Ret.), serves as an application engineer at DigiKey. He has a unique electronics and automation foundation built over a 27-year military career as a technician and engineer which was further enhanced by 12 years of teaching (interwoven). With an MSEE degree from Minnesota State University, Mankato, Dahlen has taught in an ABET accredited EE program, served as the program coordinator for an EET program, and taught component-level repair to military electronics technicians. Dahlen has returned to his Northern Minnesota home and thoroughly enjoys researching and writing articles such as this.
Return to the Industrial Control and Automation Index