Quadrature Encoding Explained
Last revision: Figure 6 was revised on 4 July 2009
One of the most popular schemes for encoding direction and speed data is "quadrature encoding". You will find quadrature encoded signals inside your computer mouse, your desktop scanner, and motion control applications of all kinds. The types of sensors used to generate quadrature encoded signals vary greatly, but the interface itself is extremely simple and easy to understand.
We will illustrate the encoding process using a wheel encoder, shown in Figures 1 and 2, which spins about a center axis.
The encoder (which can be seen in more detail by clicking on either of the two figures above) has these basic components:
- Encoder wheel (A)
- LED (B)
- Optical transistors (C) and (D) packaged as a single unit.
| Sample # | Encoder State (D,C) | Transition | Increment | Direction | 8 X Estimated Revolution Count |
| 0 | 0,0 | x,x to 0,0 | ASSUMED ZERO | unknown | ASSUMED ZERO |
| 1 | 1,0 | 0,0 to 1,0 | +1 | clockwise | 1 |
| 2 | 1,1 | 1,0 to 1,1 | +1 | clockwise | 2 |
| 3 | 0,1 | 1,1 to 0,1 | +1 | clockwise | 3 |
| 4 | 0,0 | 0,1 to 0,0 | +1 | clockwise | 4 |
| 5 | 1,0 | 0,0 to 1,0 | +1 | clockwise | 6 |
| 6 | 1,1 | 1,0 to 1,1 | +1 | clockwise | 7 |
| 7 | 0,1 | 1,1 to 0,1 | +1 | clockwise | 8 |
| 8 | 0,0 | 0,1 to 0,0 | +1 | clockwise | 9 |
| 9 | 1,0 | 0,0 to 1,0 | +1 | clockwise | 10 |
Assuming that encoder states are sampled at regular intervals, we can use the data above to calculate rotation speed.
Now consider the case where we reverse the direction and the wheel and start spinning it counter counter-clockwise.
| Sample # | Encoder State (D,C) | Transition | Increment | Direction | 8 X Estimated Revolution Count |
| 10 | 0,0 | 1,0 to 0,0 | -1 | counter-clockwise | 9 |
| 11 | 0,1 | 0,0 to 0,1 | -1 | counter-clockwise | 8 |
| 12 | 1,1 | 0,1 to 1,1 | -1 | counter-clockwise | 7 |
| 13 | 1,0 | 1,1 to 1,0 | -1 | counter-clockwise | 6 |
| 14 | 0,0 | 1,0 to 0,0 | -1 | counter-clockwise | 5 |
| 15 | 0,1 | 0,0 to 0,1 | -1 | counter-clockwise | 4 |
| 16 | 1,1 | 0,1 to 1,1 | -1 | counter-clockwise | 3 |
| 17 | 1,0 | 1,1 to 1,0 | -1 | counter-clockwise | 2 |
| 18 | 0,0 | 1,0 to 0,0 | -1 | counter-clockwise | 1 |
| 19 | 0,1 | 0,0 to 0,1 | -1 | counter-clockwise | 0 |
Inspecting the states above, we can see that the two bit encoder field (D,C) is Gray Code Encoded. Only one of the two bits changes for any given state transition. Furthermore, we can tell whether the wheel is turning clockwise or counter-clockwise based upon the state transistions, which are mutually exclusive for the two directions, as shown in the table below.
| State | Clockwise Transition To Here | Counter-Clockwise Transition to Here |
| 0,0 | 0,1 to 0,0 | 1,0 to 0,0 |
| 1,0 | 0,0 to 1,0 | 1,1 to 1,0 |
| 1,1 | 1,0 to 1,1 | 0,1 to 1,1 |
| 0,1 | 1,1 to 0,1 | 0,0 to 0,1 |
Most engineers will be more comfortable with the table above represented as a state transition diagram, as shown in Figure 3.
The sensor state (D,C) over time is often depicted as set of waveforms 90 degrees out of phase with one another, as shown in Figure 4. The phase difference of 90 degrees is not critical (altough some LSB accuracy may be gained for cases where it is true). What is important is that state transitions obey the rules outlined in Figure 3. Quadrature encoded signals are used on many different types of sensors, and many microcontrollers have dedicated circuitry to decode these signals, and thus track position over time.
The resolution of the encoding wheel shown above is roughly 1/4 revolution of the wheel. To increase resolution, most rotary encoders will have many more divisions per rotation than is shown in the figures. Figure 5 shows a real life example of such an encoder used to track one dimension of travel by a mechanical mouse.
Quadrature encoding provides only "relative" position encoding. There is no indication of absolute position, only position relative to some starting point. To address this, some sensors will include an additional signal to flag when the sensor is in its "HOME" position. A linear encoder incorporating an additional "HOME" is shown in Figure 6. In this case, the encoder strip moves left or right under the sensor. Although the physical design is completely different, the encoder outputs for phases A and B are exactly of the form already discussed.
To Learn More
- Gray Code Encoding at Wikipedia
- Rotary Encoders at Wikipedia
- Quadrature Encoding at Circuit Cellar
- SFH 3162 Dual Phototransistor
- Panasonic Mechanical Encoders
