# interfaceing... Rotary Encoders

Dan Gendreau gendreau at rochester.rr.com
Thu Nov 4 01:06:57 CET 1999

```> From: SYSph at ntOPus
...
> Is there a
> knob-type product that I could use to replace this? I am thinking of my
> friend's car stereo, you turn it to the left and it scans in descending
> order, to the right and it goes in an ascending order. What's the name for
> this kind of interface device, and where are they readily available?

I believe the device you refer to is called a Rotary Encoder. You can buy
them in most parts catalogs like Digikey in the Switch section. They usually
look and mount like potentiometers. A Rotary Encoder behaves like two
switches. it is fairly easy to read using a microcontroller chip, end even
with pure hardware in some cases.

When you rotate an encoder, the two outputs (I'll call them P1 and P2) each
produce a square wave signal. P1 and P2 are always 90 degrees out of phase.

For example, if you rotate the encoder clockwise, you should get a binary
signal something like this:
P1 0011001100110011...
P2 0110011001100110...

And counterclockwise rotation yeilds this signal:
P1 1100110011001100...
P2 0110011001100110...

There are a couple of algorithms read this signal. The more resolution you
want, the more complex the algorithm....

Method 1:
The simplest way is to watch for +P1 transitions. P1 acts like a clock. At
the moment of the +P1 transition, The value of P2 will tell you whether it
is rotating clockwise or counterclockwise. This lets you know to increment
or decrement your counter. This method is so simple it can also be easily
implimented in hardware with an Up/Down counter and some glue logic.

Sample Code:
OnP1PlusTransition(BOOL p2)
{
if(p2)
counter=counter+1;
else
counter=counter-1;
}

Method 2:
Building on Method 1, you can double the resolution of the encoder by also
watching for -P1 transitions. When you get a -P1 transition, The value of P2
tells you whether to decrement or increment your counter. NOTE: For -P1
transitions, The value of P2 has the OPPOSITE meaning of Method 1. I hope

Sample Code:
OnP1AnyTransition(BOOL p1,BOOL p2)
{
if(p1==0)
p2=!p2;
if(p2)
counter=counter+1;
else
counter=counter-1;
}

Method 3:
Continuing from the rules of Method 2, you can further double the
resolution of the encoder by watching for +/-P2 transitions. In this case
The value of P1 tells you whether to increment/decrement your counters and
again, it has the opposite meaning of P2 in Method 2.

Sample Code:
OnP1AnyTransition(BOOL p1,BOOL p2)
{
if(p1==0)
p2=!p2;
if(p2)
counter=counter+1;
else
counter=counter-1;
}

OnP2AnyTransition(BOOL p1,BOOL p2)
{
if(p2==1)
p1=!p1;
if(p1)
counter=counter+1;
else
counter=counter-1;
}

Other fancy tricks: If you can also keep track of the time between
transitions, you can add accelleration. The shorter the average time between
transitions, the more you increment/decrement by. And vice versa. That way
if you turn the encoder quickly, you will get a large change and if you turn
it slowly, you get high precision. Its a bit like audio compression.

Sample Code:
yeah right... like this isnt already too long. heh :)

I hope that helped, Cheers!
-Dan Gendreau

```