Rotary encoder

A rotary encoder is a type of position sensor which is used for determining the angular position of a rotating shaft. It generates an electrical signal, either analog or digital, according to the rotational movement

There are many different types of rotary encoders which are classified by either Output Signal or Sensing Technology. The particular rotary encoder that we will use in this tutorial is an incremental rotary encoder and it’s the simplest position sensor to measure rotation.

This rotary encoder is also known as quadrature encoder or relative rotary encoder and its output is a series of square wave pulses.


How Rotary Encoder Works:


Let’s take a closer look at the encoder and see its working principle. Here’s how the square wave pulses are generated: The encoder has a disk with evenly spaced contact zones that are connected to the common pin C and two other separate contact pins A and B, as illustrated below.

When the disk will start rotating step by step, the pins A and B will start making contact with the common pin and the two square wave output signals will be generated accordingly.

Any of the two outputs can be used for determining the rotated position if we just count the pulses of the signal. However, if we want to determine the rotation direction as well, we need to consider both signals at the same time.

We can notice that the two output signals are displaced at 90 degrees out of phase from each other. If the encoder is rotating clockwise the output A will be ahead of output B

So if we count the steps each time the signal changes, from High to Low or from Low to High, we can notice at that time the two output signals have opposite values. Vice versa, if the encoder is rotating counter clockwise, the output signals have equal values. So considering this, we can easily program our controller to read the encoder position and the rotation direction.

Let’s make a practical example of it using the Arduino. The particular module that I will use for this example comes on a breakout board and it has five pins. The first pin is the output A, the second pin is the output B, the third pin is the Button pin and of course the other two pins are the VCC and the GND pin.

greater detail of the code behind the rotary encoder + arduino experiment, as featured articles and great tutorials are available all over the web. If you would like to learn more regarding RTs and their usage you might want to read these great works:



Interfacing with Arduino:




/* Arduino Rotary Encoder Tutorial


* by Dejan Nedelkovski,




#define outputA 6

#define outputB 7


int counter = 0;

int aState;

int aLastState;


void setup() {

pinMode (outputA,INPUT);

pinMode (outputB,INPUT);


Serial.begin (9600);

// Reads the initial state of the outputA

aLastState = digitalRead(outputA);



void loop() {

aState = digitalRead(outputA); // Reads the “current” state of the outputA

// If the previous and the current state of the outputA are different, that means a Pulse has occured

if (aState != aLastState){

// If the outputB state is different to the outputA state, that means the encoder is rotating clockwise

if (digitalRead(outputB) != aState) {

counter ++;

} else {

counter –;


Serial.print(“Position: “);





  • Automotive optical sensors
  • Accurate position sensor for encoder
  • Sensor for motion, speed, and direction
  • Sensor for “turn and push” encoding