Motor driver/Controller

1.0 Introduction

A typical rotary incremental encoder consists of a light-emitting diode (LED), a disk, and a light detector on the opposite side of the disk (see next figure). The disk, which is mounted on the rotating shaft, has patterns of opaque and transparent sectors coded into the disk. As the disk rotates, the opaque segments block the light and, where the glass is clear, light is allowed to pass. This generates square-wave pulses, which can then be interpreted into position or motion. These pulses can be read by microcontroller as part of a PID feedback control system to determine translation to distance, rotational velocity, and/or angle of a moving robot or robot part. For instance, if you have a wheel rotating, and you want to measure the time it takes to rotate exactly 40 degrees, or if you want to know when you have traveled X distance, you can use an rotary encoder. The encoder will be fixed on your robot, and the mechanical part (the encoder wheel) will rotate with the wheel. Since the output of an encoder is a square wave, you can then count the pulses if you hook up this signal to a digital counter or microcontroller. Knowing the distance/angle between each pulse, and the time from start to finish, you can easily determine position or angle or velocity or whatever. Encoders are necessary for making robot arms, and very useful for acceleration control of heavier robots. They are also commonly used in robot for maze navigation.


Rotary Incremental Encoder Basic working model

Rotary Encoders usually offer 100 to 6,000 segments per revolution. This means the encoder can provide 3.6 deg of resolution for 100 segments and 0.06 deg of resolution for 6,000 segments. Linear encoders work under the same principle as rotary encoders except that instead of a rotating disk, there is a stationary opaque strip with transparent slits along its surface, and the LED-detector assembly is attached to the moving body.

2.0 Quadrature Encoder

An encoder with one set of pulses is sometime not sufficient because it cannot indicate the direction of rotation. Using two code tracks with sectors positioned 90 degree out of phase (see next figure); the two output channels of the quadrature encoder indicate both position and direction of rotation. For example, if A leads B, the disk is rotating in a clockwise direction. If B leads A, the disk is rotating in a counter-clockwise direction. Therefore, by monitoring both the number of pulses and the relative phase of signals A and B, the microcontroller can track both the position and direction of rotation. In addition, some quadrature encoders include a third output channel – called a zero or reference signal – which supplies a single pulse per revolution. This single pulse can be used for precise determination of a reference position. This signal is called the Z-Terminal or the index in most of encoder. A typical, ideal quadrature signal looks like this:


Quadrature Encoder Output

With incremental encoders, you can measure only changes in position (from which you can determine velocity and acceleration), but it is not possible to determine the absolute position of an object. Another type of encoder, called an absolute encoder, is capable of determining the absolute position of an object. Its function is similar to position feedback using variable resistor (analog output), the only differences are that it can be rotated in 360 degree and digital output. This type of encoder has alternating opaque and transparent segments like the incremental encoder, but the absolute encoder uses multiple groups of segments that form concentric circles on the encoder wheel like a bull’s-eye on a target or dartboard. The concentric circles start in the middle of the encoder wheel and, as the rings go out toward the outside of the ring, each of them has doubled the number of segments than the previous inner ring.

To make encoder measurements, you need a basic electronic component called a counter. Based on its several inputs, a basic counter emits a value that represents the number of edges (low to high or high to low transitions in the waveform) counted. Most of the Microchip PICs have this peripheral; normally Timer 0 or Timer 1 is used as external input counter. External interrupt pins (INT) can also be used for counting the pulse; the rising edge (low to high) or falling edge (high to low) is configurable. Once the edges are counted, the next thing you need to take care is how those values are converted to position, further to speed and etc. The process by which edge counts are converted to position depends on the type of encoding used. There are three basic types of encoding, X1, X2, and X4.

2.1 1X Encoding

You will be able to see the signals shown in the next figure if we are scanning from left to right ; and reverse the direction or scan from right to left on previous figure. This is a quadrature cycle and the resulting increments and decrements for X1 encoding. When channel A leads channel B, the increment occurs on the rising edge of channel A. When channel B leads channel A, the decrement occurs on the falling edge of channel A.


1X Encoding

2.2 2X Encoding

A shortcoming of the previous method is that the count frequency is the same as the frequency of channel A. Thus, an encoder is said to have a resolution of 500 pulses per revolution (ppr) does exactly that. We can do better by using both edges of Channel A. This is not too hard to arrange in hardware but this uses up valuable board space. The equality test described just now works just as well if we are detecting falling edges. Thus we can use the same routine for both rising and falling edges and detect twice as many transitions. With 2X decoding our 500 ppr encoder can generate 1000 pulses per revolution.


2X Encoding

2.3 4X Encoding

It is possible to do even better if we examine the edges of both channel A and channel B. There are four edges for each phase of channel A and it is possible to get 2000 pulses per revolution from our 500 ppr encoder.


4X Encoding

When I cut out the middle part of this signals which shown in the next figure, we can see clearly that the two bit encoder field (A, B) is Gray Code Encoded. Only one of the two bits changes for any given state transition.


4X Encoding State Transition

Furthermore, we can tell whether the wheel is turning clockwise or counter-clockwise based on the state transitions, which are mutually exclusive for the two directions, as shown in the table below.


4X Encoding State Transition Table

Most engineers will be more comfortable with the table above representing a state transition diagram, as shown in figure below.


4X Encoding State Transition Diagram

If you have a microcontroller with the ability to generate interrupt form external source, it is pretty simple to get the count we want. But at least it must has two external interrupt pins for 4X encoding, let’s say we use PIC18F4520 (3 external interrupt pins). Channel A is connected to the INT1/RB1 pin and channel B is connected to the INT0/RB0 pin. The sense of the interrupt is changed after each interrupt so that the routine responds alternately to rising and falling edges. On each interrupt, after determining the current state, we can get the direction by checking back the previous state and Count Value will be increased or decreased. Listing below is the example interrupt routine for PIC18F4520 and the sequence is based on the previous state transition diagram.


4X Encoding Interrupt Routine Listing

Angle of rotation (degree) = (CountValue/XN) x 360 where N = number of pulses generated by the encoder per shaft revolution, X is the encoding type. Let’s say Rotary Encoder B-106-23983 (available from is used, N=500ppr and we use 4X encoding, so our angle of rotation (degree) = CountValue x 0.18. This encoder outputs capable to produce up to 100 kHz pulse, it is good enough for a normal mobile robot. Your microcontroller speed might limit the maximum angular speed of your encoder due to the time for serving interrupt routine. Some of the 8-bit Microchip PIC has Quadrature Encoder Interface (QEI) feature with noise filters like PIC18F2331, PIC18F2431, PIC18F4331 and PIC18F4431. With this model of microcontroller, configure some of the related registers is sufficient; position and velocity of your encoder can be obtained directly without serving the interrupt routine. Hope this article helps in using rotary encoder. If you have any feedback, please discuss in our technical forum:

* If you like to have faster response from author regarding your inquiry/comments/feedback, please do post in our technical forum as we seldom check the comment section in tutorial site.