Rotary Encoder Measurement using Arduino

Rotary Position Measurement

A rotary or “shaft” encoder is an angular measuring device. It is used to precisely measure rotation of motors or to create wheel controllers (knobs) that can turn infinitely (with no end stop like a potentiometer has). Some of them are also equipped with a pushbutton when you press on the axis (like the ones used for navigation on many music controllers). They come in all kinds of resolutions, from maybe 16 to at least 1024 steps per revolution. Industrial encoders come from 1200PPR to 10000PPR (Pulse per revolution).

Measurement Principle

                Rotary encoders have two or three outputs A,B and Z. A and B are used to determine direction of rotation and counting the number of pulses will give the rotational position. Z is available on industrial grade encoders. It is zero position pulse. It gives only one pulse in one revolution.

Angle = (360 / Encoder_Resolution) * Pulse cont

Output A is connected to interrupt (RISING) and when interrupt occurs depending on State of B output Pulse count is incremented or decremented.

Rotary Encoder Waveform
Rotary Encoder Waveform
Rotory Encoder Types
Rotary Encoder Types

There are too many encoders available in market all have basic principle of A,B output as shown in figure 12.1.  Some of the encoder have GND, +V, SW, DT, CLK. Pin configuration of these encoder is like this CLK=A, DT=B,  +V=5V given for pull-ups. SW is press action switch.

Encoder Pin out
Encoder Pin out

Arduino Circuit Connections with Encoder

Encoder Arduino Circuit
Arduino Connections with Encoder

Arduino Code for Rotary Encoder


                Open serial monitor and see the result of rotary encoder. Turn knob clock wise and anti-clock wise.

Encoder Result
Encoder Result

3 thoughts on “Rotary Encoder Measurement using Arduino

  1. I wasn’t able to get this to work on the Arduino Due.
    As written, it won’t trigger interrupts.
    Line 19 should probably read:
    attachInterrupt(digitalPinToInterrupt(encoder0PinA), doEncoder, RISING);

    With that change, the value goes up and down when the knob is turned, but it is kinda random which direction it goes.

Leave a Reply