Published on Micro Basics (http://www.ubasics.com)

Rotary Encoders

By stienman
Created Feb 23 2006 - 8:42pm
----------

Please support this site: If you find this information useful, interesting, or entertaining, please help me by voting for my entry [1] on the MyPIC32 contest, and learn more about Microchip's new line of 32 bit microcontrollers. By viewing a few of the contest entries each week you will be eligible for community prizes, such as an iPod Touch. Thanks!

----------

[Output] [2] [Timing] [3] [What's Inside] [4]

Rotary Encoders are devices with a shaft which one can rotate that has output which will indicate either: the shaft's absolute position, or its relative position. The common computer mouse has two relative position rotary encoders The following is a dialogue between myself and Tom, who had a few questions about rotary encoders.

Tom wrote:

Hi,

I want to use a rotary encoder on our next product. I have found people who "think" that they know how they work but "are not sure". Alps Corp. makes them but they are based in Japan, with no tech support in the USA or specs written in English. I imagine that they output some sort of a phased pules but again, I am not too good at reading Japanese.

Does someone have a general spec or a link to another manufacturer?

Thanks
Tom

Generally there are two kind of rotary encoders:
One that puts out absolute position, the other puts out relative position, or position change.

The absolute position output will vary depending on the resolution and manufacturer. They may use a binary code (also look up references to 'grey code').

On a 16 position absolute encoder, with four outputs, the outputs may look like this:

OUT 1    0  1  0  1  0  1  0  1  0  1  0  1  0  1  0  1
OUT 2    0  0  1  1  0  0  1  1  0  0  1  1  0  0  1  1
OUT 3    0  0  0  0  1  1  1  1  0  0  0  0  1  1  1  1
OUT 4    0  0  0  0  0  0  0  0  1  1  1  1  1  1  1  1
Position 1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16

The relative position encoder will either put out a sine wave on both wires, with the second being 90 degrees out of phase with the first, or it puts out a square wave on both wires, as follows (You'll need to set the following to fixed width font to see it):

          ___     ___     ___     ___     ___
OUT 1 ___|   |___|   |___|   |___|   |___|   |___
        ___     ___     ___     ___     ___     _
OUT 2 _|   |___|   |___|   |___|   |___|   |___|
       <--Clockwise         Counter-clockwise-->

This square wave is basically a representation of a sine wave, 90 degrees out of phase. It is easy to interface, if you take OUT 1 and define it as your direction. If OUT 1 is high when a pulse starts on OUT 2, then the rotary encoder is going clockwise. If OUT 1 is low when a pulse starts on OUT 2, then the encoder is going counter-clockwise. A sine wave output encoder is good if you want a higher resolution than you can get from a square wave encoder. It would require more hardware or software to interpret, though.

I hope this helps!

-Adam

Tom wrote:

Adam,

Thank you so much for the well written reply. If I could just impose upon you for one more piece of information I would be grateful.

What about the timing? I wish to sample the Rotary Encoder outputs with the I/O ports of a microcontroller, so I am sure that the pulse timing is important. I can't read the spec I have from Alps Corp.and they have no English web site. Do you have a reference to another manufacturer?

Thanks again.
Tom

Timing is based on the resolution of the encoder, and the speed at which it will be turned.

First, if we say the encoder is rotating at a constant speed, then generally the square waves are 50% duty cycle (depending, as always, on the manufacturer).

These encoders can come at different resolutions, meaning that every time you turn the encoder one revolution, it may put out 1 pulse on each channel, 10 pulses, 360 pulses or whatever the manufacturer designed it for. If you take a 360 pulse per revolution encoder, and turn it at 100 RPM, then you can expect 36,000 pulses per minute, or 600 Hz.

Let's say, for instance, you are creating an electronic etch-a-sketch, and the display resolution is 640x480. You decide that the user should have to turn the rotary encoder 4 turns to cross the screen horizontally, and three turns to cross the screen vertically. This means that the horizontal encoder must pulse 640 times in 4 revolutions, or 160 pulses per revolution. We will find that the vertical encoder needs to be 160 PPR too, which makes the project cheaper because we can buy in bulk. Now, through studies (i.e., we passed it around the office), we find that the fastest user can spin our control at 200 RPM (about 3 times a second). This means that we can get 32,000 pulses each minute, or we will be receiving 534 pulses per second. This gives us a minimum of 1.8 milliseconds to detect a pulse, easily detectable by a uController running at 32kHz, assuming we sample both inputs 4-10 times every 1.8mS.

I don't have any references to manufacturers. I know that many distributors sell them. For instance, you might refer to DigiKey and find out who they get their encoders from. A quick search turns up quite a bit:

Google [5]

I hope this answers your question, I wasn't exactly sure of what you were asking about the timing.

-Adam

What's Inside:

The relative rotary encoders have either square wave output or sine wave output:

Square Wave:
Many square wave encoders create the waveform either mechanically (passive), by rotating an etched PCB or other material past two sets of contacts, or optically (active), by rotating a piece of plastic between two LEDs and phototransistors.

Sine Wave:
Most sine wave encoders have one or more magnet(s) attached to the shaft, which are rotated past two hall effect sensors mounted a certain angle (such as 90°) apart.

Absolute encoders may use any of the above methods, possibly including additional electronics to encode the signal. For instance, using a regular sine wave output with one period per revolution, one can determine the absolute position of the shaft.

Higher resolution reading

You can also get more resolution out of an ordinary rotary encoder.

          ___     ___     ___     ___     ___
A     ___|   |___|   |___|   |___|   |___|   |___
        ___     ___     ___     ___     ___     _
B     _|   |___|   |___|   |___|   |___|   |___|
       <--Clockwise         Counter-clockwise-->
       ^ ^ ^ ^ 

I've added arrows below the signal to highlight that there are 4 transitions for each full pulse. A 200 pulse per revolution encoder has 800 transitions. This method requires that you keep track of the current state of the two outputs, and the previous state of the two outputs.

If the previous state was A=1, B=1, and the encoder then outputs A=0 and B=1 then the encoder has moved one transition clockwise. There are only four possible states for the encoder to be in:

A B
0 0
0 1
1 1
1 0

So by keeping track of what the encoder was outputing last time you checked, and comparing it with this time you can determine whether it has moved by 1/4 of a pulse.

Some other unique methods of determining absolute position:
These quotes were taken from three messages posted to sci.electronics.design [6] with the subject, "ENCODERS>>>>absolute or incremental.....that is the question?"

"This series of codes has the unique property of letting you determine the absolute position (within the entire code) with less than the expected minimum number of bits required for complete absolute resolution. It does this at the expensive of you needing to actually move the encoder several steps in order to "see" the pattern in the changing bits. When enough steps in the encoder position has passed by, the resultant pattern then is unique to one and only one spot within the full combination of the code. In essence, after a minimum number of increments, the absolute position can then be easily determined." -Terry Moreau <tmoreau@direct.ca [7]>

"The deal is that you need a cyclic (repeating) code sequence N bits long in which any given string of M bits is unique. You then draw this sequence around your code disk, and you need to move only M bit positions to read M bits of code at startup; you then know where you are in the whole N-bit sequence.

"Now, it's obvious that the best you can possibly do is N=2^M, because by reading M bits you only have 2^M possible values. It may be that there are some codes that have this property. On the other hand it is very easy to find codes that do nearly as well, giving N=(2^M)-1. These are the "pseudo-random binary sequences" which can be generated using linear-feedback shift registers. There's a useful section on these in Horowitz and Hill, and no doubt many other places. The good news is that the idea is basically sound. Suppose M=10, then you can do a 1023-position encoder and get absolute position after moving only 10 positions, i.e. about four degrees. A lot better than forcing your milling machine, industrial robot or whatever all the way back to its "home" position on every startup. I think that the most useful application would be for long linear tracks such as would be found on gantry robots powered by linear motors.

"The bad news is that the pattern you must print on the code disk is no longer simply periodic, and it is therefore impossible (I think - certainly very tricky) to use Moire techniques to get high resolution. So this might be something you would add to an existing high-resolution incremental encoder.

"I have a final-year student working on a linear-track version of this right now - he's reading a fairly low-resolution code using a linear CCD array, and hoping to use interpolation to improve the positioning accuracy. I will try to remember to report any interesting outcomes to this group. " -Jonathan Bromley

"I remember a Heidenhain rotary encoder with a 'normal' incremental code. As an extra feature it had multiple (32?) index holes. The distance between these index holes was not equal and therefore you could retrieve the absolute position by counting the number of pulses between two index holes. Using analog interpolation I was able to get over 100,000 steps per revolution. I hope this may help you." -Reinier Gerritsen

Other resources

Source URL:
http://www.ubasics.com/adam/electronics/doc/rotryenc.shtml