February 5, 2014

# Encoders

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 Encoder

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.

### Incremental Encoder

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

### 1x 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.

### 2x encoding

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

### 4x encoding

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.

## 44 thoughts on “Encoders”

• My spouse and I stumbled over here different website and
thought I should check things out. I like what I see so i am just following you.

Look forward to going over your web page for a second time.

• It’s an remarkable paragraph for all the internet viewers; they will
get advantage from it I am sure.

• Hey there! This post could not be written any better! Reading through this

I will forward this write-up to him. Pretty
sure he will have a good read. Thank you for sharing!

• Hi mates, how is the whole thing, and what you wish for to say on the topic of this
post, in my view its in fact amazing in favor of me.

• I’m extremely inspired together with your writing skills as smartly as
with the structure on your blog. Is this a paid subject or did
you modify it your self? Either way keep up the excellent high quality writing, it’s
rare to look a nice blog like this one today..

• Howdy! This post couldn’t be written any better!
Reading this post reminds me of my previous room mate!

Thank you for sharing!

• Your style is so unique in comparison to other people I’ve read stuff from.

I appreciate you for posting when you have the opportunity, Guess
I will just bookmark this web site.

• You actually make it seem so easy with your presentation but I find this matter to be really something which
I think I would never understand. It seems too complicated and
extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!

• Howdy! Someone in my Myspace group shared this website with us
so I came to look it over. I’m definitely enjoying
the information. I’m bookmarking and will be tweeting this to my followers!

Terrific blog and brilliant design.

• What i do not understood is actually how you’re not really a lot more well-preferred than you may be right now.

You are so intelligent. You realize therefore significantly in terms
of this matter, made me in my opinion consider it from numerous
varied angles. Its like women and men aren’t interested except it’s one thing to
do with Lady gaga! Your own stuffs nice. At all times take care of
it up!

• You’ve made some decent points there. I looked on the web to learn more about the issue and found most people will go along with your views on this site.

• I think the admin of this web site is in fact working hard in support of his
web page, as here every stuff is quality based data.

is amazing. It sort of feels that you are doing any distinctive trick.

Also, The contents are masterwork. you’ve done a excellent process
in this matter!

• Way cool! Some very valid points! I appreciate you writing this
article and also the rest of the site is also very good.

• I do not even know how I finished up right here,
however I believed this submit was great. I don’t recognise who you are however definitely you are going to
a well-known blogger in the event you are not already. Cheers!

awesome, nice written and include approximately all important infos.

• I am sure this piece of writing has touched all the internet users, its really really pleasant article on building up new weblog.

• Hi to all, how is the whole thing, I think every one
is getting more from this web page, and your views are fastidious in favor of new visitors.

• It’s a pity you don’t have a donate button! I’d definitely donate to
this superb blog! I suppose for now i’ll settle for book-marking and adding
with my Facebook group. Talk soon!

• Spot on with this write-up, I truly believe that this website needs a great deal
more attention. I’ll probably be returning to read more, thanks for the
info!

• I’ve been surfing on-line greater than three
hours today, but I never discovered any interesting article like yours.
It is pretty price enough for me. In my opinion, if all web owners and bloggers made good content as
you probably did, the internet will probably be much more helpful than ever before.

• If some one desires expert view about blogging and site-building afterward i recommend him/her to visit
this web site, Keep up the pleasant work.

It is the little changes that will make the most important changes.

Thanks a lot for sharing!

• It’s in reality a great and useful piece of information. I am happy that
you shared this helpful information with us. Please stay us up to date like this.

Thank you for sharing.

• Hi to all, the contents present at this website are truly awesome for people experience, well, keep up the nice work fellows.

in it or something. I think that you can do with a few pics
to drive the message home a little bit, but instead of that,
this is excellent blog. An excellent read. I’ll
certainly be back.

• I simply want to tell you that I am very new to blogging and honestly liked you’re web site. Probably I’m going to bookmark your website . You definitely come with remarkable article content. With thanks for sharing your blog site.

• Oh my goodness! Impressive article dude! Thanks, However I am experiencing problems
with your RSS. I don’t understand the reason why I cannot join it.
Is there anyone else having similar RSS problems? Anyone that knows the answer will you kindly respond?
Thanks!!

• Hey, I think your website might be having browser compatibility issues.
When I look at your blog site in Firefox, it looks fine but when opening in Internet Explorer, it
has some overlapping. I just wanted to give you a
quick heads up! Other then that, amazing blog!

• I do accept as true with all the ideas you’ve introduced
for your post. They are really convincing and will
definitely work. Nonetheless, the posts are too quick for starters.
May you please lengthen them a little from subsequent time?
Thanks for the post.

• Sourabh Moitra says:

Thank you for your suggestion. We will definitely take it into account.

• This is a good tip especially to those fresh to the blogosphere.
Short but very accurate info… Many thanks for sharing this one.

• I have been exploring for a little bit for any high quality articles or weblog posts on this kind of area .
Exploring in Yahoo I at last stumbled upon this site. Studying this information So i’m satisfied to show that
I’ve a very good uncanny feeling I found out exactly what I needed.
I most surely will make sure to do not disregard this site and
give it a look on a continuing basis.

• If some one wants expert view concerning running a blog afterward i propose him/her
to go to see this webpage, Keep up the fastidious job.

• Hey, thanks for the article.Really looking forward to read more.

• Thanks for another informative website. Where else may I get that kind of information written in such a perfect manner?
I’ve a undertaking that I am just now working on, and I’ve been on the look out for such information.

• Excellent post. I was checking continuously this blog and I’m impressed!
Extremely useful information specially the final part 🙂 I maintain such information a lot.
I was seeking this particular info for a very long time.
Thank you and best of luck.

• Pretty nice post. I just stumbled upon your weblog and wished to say that I
have really enjoyed browsing your blog posts. After all I will be subscribing to your rss feed and I hope you write again very soon!

• whoah this weblog is magnificent i like studying your articles.
Stay up the good work! You understand, a lot of people are searching round for
this info, you could help them greatly.

• Hi, constantly i used to check website posts here in the early
hours in the morning, for the reason that i enjoy to find out more and more.

• An outstanding share! I have just forwarded this onto a colleague who has been conducting a little homework on this.
And he actually ordered me lunch because I discovered it for
him… lol. So allow me to reword this…. Thank YOU for the meal!!

But yeah, thanks for spending the time to discuss this topic here on your web site.

• I just want to say I am just new to blogs and actually liked your website. Very likely I’m going to bookmark your website . You definitely have wonderful writings. Regards for revealing your blog site.

• Say, you got a nice article post.Really thank you!