In this tutorial, we will try to learn what an encoder is and the various encoding techniques that are commonly used. We will be dealing with digital rotary encoders in particular. So what is an encoder?
An encoder in layman-ish terms is a device used to convert motion parameters like distance, velocity, acceleration, etc., of a moving object of our interest into electrical signals of some kind.
The encoders can be, in general, classified into two :
a) Absolute Encoders b) Incremental encoders
Before going into the details, let us have a bird’s eye view of what an encoder is. A rotary encoder is a device which is used to “encode” the rotation of a body, let’s say a motor which drives a robot in a pre-designed room/arena. It means that the locations where the robot is supposed to go are determinate. Therefore, it is convenient if we have a mechanism to find how much our robot have moved and in which direction. There are analog and digital rotary encoders available in the market. The simplest encoder that you can think of is the potentiometer. Consider that you couple a potentiometer onto the shaft of wheel. Even though the maximum limit is 360 degrees, we can find the amount by which our wheel have moved by measuring the change in resistance of the potentiometer. There are encoders which can be used to encode any number of rotations too. The kind of encoders used for that purpose are generally the digital rotary encoders. A digital rotary encoder is a device which converts the motion of a body into digital pulses or counts. Usually the digital encoders have two outputs also called as two channels namely channel A and channel B. I believe that this would be more than enough for a general introduction to encoders since we have talked about all the major variants of encoders. Now lets look at each kind in particular.
Absolute encoders are those encoders which have the capability to store the current position information even if the power is turned off. It means that the output of these encoders are based on the physical position of the subject being encoded. A potentiometer can be thought of as an absolute encoder since it is having a “memory like” feature. The resistance of the potentiometer remains unchanged whether the encoder is ON/OFF. This allows us to find the absolute position of our wheel. Another example of absolute encoders are the parallel absolute encoders. May be you can read more about it if interested.
An incremental encoder can be thought of as an encoder which is not having any “memory”. It means that an incremental encoder can keep track of the motion of, let’s say a wheel only when the encoder is powered. Therefore there is no relation between the encoder state and the physical position of the “wheel”. The word incremental signifies that the encoder increments its count by one each time it moves a bit. Usually, there are two channels for the output. Channel A and Channel B. In this tutorial we will be giving more importance to the incremental encoders.
How does it work?
It is interesting to know how an encoder is made and the principles underlying. I will talk about the optical encoders in general. An optical encoder consists of a light emitting device, a detector, and a circular disk which has alternate opaque and transparent regions. The circular disk is kept in between the emitter and the detector in such a way that the detector alternatively turns ON/OFF depending on whether the light falls on the detector through the slits on the encoder or not.
Before continuing further it is good to have a look at a typical encoder mounting arrangement.
Now let us scrutinize each component in an encoder. Lets start with the led. Since the encoders are prone to ambient light interferences, designers always try to use leds which emit light out of the visible range, generally in the Infra-Red range. This becomes important if the robot is supposed to work in outside environments where sunlight affects the normal operation of the encoder very badly. The detector therefore cannot simply be an LDR even though many designers use it to make the design simpler. It is always advised to use light detectors which are wavelength specific. Such detectors promises better performance in ambient “noisy” environments. For example a 680 nm led coupled with a similar photo diode. Such photodiodes detects light with wavelength lying near around 680 nm. All other wavelengths are considered to be “dark”. Normally all the encoders have a circuit inside that converts the output of the photodiode into digital pulses of HIGH/LOW.
Usually encoders may either have a single channel output or a dual channel output. Single channel output encoders have only one LED-photo diode pair. Therefore when it moves over the encoder disk, it generates square wave which is used for encoding. The number of HIGH to LOW and LOW to HIGH transitions can be used to encode the distance whereas the rate of transitions can be used to calculate the instantaneous velocity of the system. The drawback of this kind of an encoder is that the system lacks sense of direction. This have serious after effects as such a system becomes highly unreliable in environments with vibrations. The error may add up due to mis detections triggered by the high frequency vibrations of the system due to various actuations. Therefore it is important to have a sense of direction too. This is where the dual channel output comes into picture. A dual channel optical rotary encoder has two detectors which are kept at a finite pre-determined distance from each other. This kind of encoder therefore generates two square waves when the system is in motion. The two square waves will be having a constant phase shift between the output because of the finite distance between the two detectors. The characteristics of the two outputs are used to decide upon the direction of motion of the encoder.
Therefore, lets say, if channel A leads channel B and the motor is moving clockwise, then if the channel B is leading the channel A, it means that the motor is rotating in counter clockwise direction. Besides giving us the sense of direction, a dual channel output can give us better precision. A dual channel encoder can provide four times more precise encoding than a single channel encoder using the same encoder disk. Such encoders are called quadrature encoders.
A quadrature encoder is simply a dual channel encoder, may be an optical or magnetic encoder. It typically have two 90 degree phase shifted square wave output channels. As the sequence of output states of such an encoder is similar to the states in the quadrature encoding scheme popular in the communication engineering, it is called a quadrature encoder. As previously mentioned, these encoders have two detectors. Now consider rotating such an encoder. Initially as the encoder rotates, the first detector comes above the one of the slits on the encoder disk. Then the corresponding detector registers a LOW to HIGH transition. It remains HIGH as long as the slit is right above the detector. As the encoder keeps on moving the second detector registers a LOW to HIGH transition. As it keeps on moving, first the detector 1 and then the detector 2 registers a HIGH to LOW transition as the slit is over. This process continues generating two phase shifted channel outputs called the quadrature channel outputs. The figure given below will give a more clear picture of how a quadrature encoder works.
How to decode ?
There are three ways in which you can decode the distance and direction informations from a quadrature encoder. Depending on the kind of decoding algorithm you opt, the precision and complexity of the encoder will increase. The various kind of encodings are
a) 1x encoding
b) 2x encoding
c) 4x encoding
This is the simplest of the encoding techniques used. In this we increment or decrement our count based on the either one of the state transitions of one of the channels. This is usually done by feeding one of the channel outputs to an interrupt pin of a microcontroller and triggering counting when a transition of one kind occurs. “Transition of one kind” means that we count either only HIGH to LOW or LOW to HIGH transitions. This is decided by the programmer. This is how we count.
Consider there occurs a HIGH to LOW transition on the channel A which is fed to the interrupt pin of the microcontroller. We will write a code in the microcontroller to check whether the channel B is HIGH or LOW and decide whether to increment or decrement the count based on the state of channel B. for example if we find that the channel B is LOW, we might increment the count, whereas a we may decrement the count if the channel B is LOW during the HIGH to LOW transition of the channel A. We do not count the LOW to HIGH transitions in 1x encoding and therefore, it is the least precise yet simple encoding technique.
This as the name signifies is a better encoding technique which can provide double the precision in contrast to 1x encoding. It means that if there are 500 slits on our encoder disk and then if we are doing 2x encoding, it is in effect equivalent to doing a 1x encoding using a 1000 slit encoder disk. Interesting Isn’t it ?? So how is it done ?
Consider that we set up the interrupt routine to be triggered on both the rising and falling edge of the channel A. This will allow us to count twice as much time as we are doing with 1x encoding. If we count the event of a LOW on channel B during the rising edge of A and a HIGH on channel B during the falling edge of A as clockwise direction, then we will assume the event of a HIGH on channel B during the rising edge of A and a LOW on channel B during the falling edge of A as counter clockwise direction
This is the best way of encoding in which we feed both the channel A and the channel B to the interrupt pins of a microcontroller and check whether there is any kind of state change and use that information to trigger the interrupt service routine to increment or decrement the timer. This gives you the maximum precision possible with a given quadrature encoder. The programmer should always try to keep his code as efficient as possible so that the system will work reliably when the encoder rotates at high speed. Otherwise, usually people experience problems since some values may get skipped becuase the microcontroller fails to catch up with the speed of the counting. The following code depicts how to implement 4x encoding in a microcontroller.