2 (3)
2 (3)
Lab Experiment 2
Measuring Angular Displacement using an Encoder
What is an Encoder?
An incremental optical encoder, shown in Figure 2-1, is a relative angular displacement sensor which
measures angular displacement relative to a previously known position. Unlike an absolute encoder, an
incremental encoder does not retain its position information upon power loss. An incremental encoder
outputs a series of pulses which correlate to the relative change in angular position. Encoders are
commonly used to measure angular displacement of rotating load shafts. Information extracted from an
incremental encoder can also be used to derive instantaneous rotational velocities.
An incremental optical encoder typically consists of a coded disk, an Infrared (IR) LED, and two
photosensors. The disk is coded with an alternating light and dark radial pattern causing it to act as a
shutter. As shown schematically in Figure 2-2, the light emitted by the IR LED is interrupted by the
coding as the disk rotates around its axis.
Figure 2-2: Output of an incremental encoder showing signals A and B when rotating in a clockwise manner
The two photosensors (A and B) positioned behind the coded disk sense the infrared light emitted by the
IR LED, which results in A and B signals/pulses, in four distinct states as outlined in Table 2-1:
1 OFF ON
2 OFF OFF
3 ON OFF
4 ON ON
Encoders which output A and B signals are often referred to as quadrature encoders since the signals are
separated in phase by 90° and result in four distinct states. Non-quadrature encoders have only one
output signal and thus are unable to detect direction. The resolution of an encoder is determined using
the number of light or dark patterns on the disk, a measure that is given in terms of pulses per
revolution, or PPR.
Some encoders utilize an index pulse (Z channel), which is triggered once for every full rotation of the
disk (see Figure 2-3). The index pulse can be used for calibration or so-called homing of a system, as well
as a revolution counter. Depending on the encoder, the width of the index plus may be aligned with any
of the four quadrature states. For example, the index pulse may have a width that spans a full cycle (4
states), a half cycle (2 states), or a quarter cycle (1 state). In the example shown in Figure 2-3, the width
of the index pulse is aligned with a full cycle of the B signal.
There are two methods that an encoder registers the index pulse: (a) using pre-defined states of signals
A and B, or (b) using user-defined states of signals A and B, in which case the user must select a
combination of A and B states that occurs only once during the width of the index pulse.
Encoder Decoding
In order to make encoder measurements, you need to connect the encoder outputs to a counter. A
decoding algorithm is then used to determine the number of counts and possibly the direction of
rotation.
Four common decoding algorithms are used: non-quadrature, X1, X2, and X4.
Non-quadrature
When a non-quadrature decoder is used, only the rising edge of signal A is counted as the shaft rotates.
The counter is incremented on the rising edge of signal A. Because signal B is not used, the encoder
cannot detect the direction of rotation. For example, using a non-quadrature decoder, a 9 PPR encoder
will result in a total of 9 counts for every rotation of the encoder shaft. The count will increase
regardless of which direction the shaft is rotated.
X1 Decoder
When an X1 decoder is used, only the rising edge of signal A is counted as the shaft rotates. When a
rising edge of signal A occurs, the algorithm looks at the current state of signal B. If signal B is low, the
counter is incremented. Otherwise, when signal B is high, the counter is decremented. Using an X1
decoder, a 9 PPR encoder will result in a total of 9 counts for every rotation of the encoder shaft.
X2 Decoder
When an X2 decoder is used, both the rising and falling edges of signal A are counted as the shaft
rotates. When a rising edge of signal A occurs, the algorithm looks at the current state of signal B. If
signal B is low, the counter is incremented. Otherwise, when signal B is high, the counter is
decremented. When a falling edge of signal A occurs, if signal B is high the counter is incremented,
otherwise when signal B is low the counter is decremented. Using an X2 decoder, a 9 PPR encoder will
generate a total of 18 counts for every rotation of the encoder shaft.
X4 Decoder
When an X4 algorithm is used, both the rising and falling edges of both signals A and B are counted.
Using a state machine diagram, Figure 2-4 illustrates how the counter is incremented or decremented
depending on the state of signals A and B. An X4 decoder generates four times the number of counts
generated by an X1 decoder resulting in the highest resolution among the three types of decoders.
Using an X4 decoder, a 9 PPR encoder will generate a total of 36 counts for every rotation of the
encoder shaft.
Counts
θ= ∙ 360 °
N ∙ PPR
The angular resolution of an encoder (not to be confused with encoder resolution, or PPR) depends on
the encoder’s PPR and the decoding algorithm used, and can be calculated using Equation 2-2:
Equation 2-2
360 °
∆ θ=
N ∙ PPR
Figure 2-5 compares the number of counts generated by each of the non-quadrature, X1, X2, and X4
decoders.
Figure 2-5: Comparison of the number of counts generated by different decoding algorithms
2.2 Implement
The Virtual Instrument (VI) used to collect data from and calibrate the encoder is shown in Figure 2-6.
Non-quadrature Decoding
1. Open Mechatronic Sensors Board.lvproj
2. From the Project Explorer window, open Mechatronic Sensors - Incremental Encoder.vi
3. From the Decoder drop-down menu, select non-quad.
4. Run the VI.
5. Wait for the Initialized? LED indicator to turn on.
6. In non-quadrature decoding only signal A is used. Rotate the encoder knob in the clockwise
direction. How does the Edge (count) numeric display change?
7. Rotate the knob in the counterclockwise directions. How does the Edge (count) numeric display
change?
Note: At any time, you can press the Reset button to reset the counter. This will rest the Edge
(count) and Angle (deg) numeric displays to zero.
8. Using the Edge (count) numeric display, determine the number of pulses the encoder generates
per each full revolution (PPR).
Note: PPR is determined in non-quadrature mode. It refers to the total number of pulses
generated by Signal A when the encoder makes one full revolution. The value of PPR will be
used to calibrate the encoder pulses in terms of angular displacement in degrees.
10. Calibrate the pulses of the encoder in terms of angular displacement. To do this, enter the PPR
value which was calculated in the previous section in the PPR numeric control and press the
Enter key.
11. Verify the accuracy of your calibration. To do this, first press the Reset button then rotate the
encoder knob and verify that the correct angular position is displayed in the Angle (deg)
numeric indicator.
12. Continue to the next section.
X2 Decoding
Note: An encoder will have a fixed PPR value regardless of the decoding algorithm that is used.
16. Rotate the knob in the counter-clockwise direction. How do the Edge (count) and Angle (deg)
numeric displays change?
17. Examine the behavior of signal A and signal B.
18. What is the resolution of the measured angular displacement?
19. Continue to the next section.
X4 Decoding
2.3 Analyze
2-1 How did the Edge (count) numeric display change when the knob was rotated in the clockwise
direction in step 6?
2-2 How did the Edge (count) numeric display change when the knob was rotated in the counter-
clockwise direction in step 7? Explain the observed behavior.
2-3 What is the PPR of the sensor that you calculated in step 8?
2-4 When using non-quadrature decoding, as the encoder knob was rotated in step11, did the Angle
(deg) numeric indicator display the correct angular position?
2-5 When using X2 decoding, as you rotated the encoder knob in step 15, how did the Edge (count) and
Angle (deg) numeric displays change?
2-6 As you rotated the encoder knob in the counter-clockwise directions in step 16, how did the Edge
(count) and Angle (deg) numeric displays change? Explain the observed behavior.
2-7 What is the resolution of the measured angular displacement that you calculated in step 18? Does it
match the expected angular resolution using Equation 2-2?
2-8 When using X4 decoding, as you rotated the encoder knob in the clockwise and counter-clockwise
directions in step 22, how did the Edge (counts) and Angle (deg) numeric displays change?
2-9 What is the resolution of the measured angular displacement that you calculated in step 23? Does it
match the expected angular resolution using Equation 2-2?
2-11 Using the screenshot you captured in step 24, compare the behavior of signals A and B and you
rotate the encoder in the clockwise direction with the state machine diagram shown in Figure 2-4. Do
your results match the state machine sequence?