Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...

Rotary encoder is a electro-mechanical device which is used to measure the angular position of a shaft. It converts the angular position of the shaft into analog or digital code. It has infinite movement and it has no starting or end point  but by keeping track of its movement in the code, we can know its end and starting point. It looks like a potentiometer but it is different in operation. Rotary encoders are used in the automation field for detecting speed, angle, position, acceleration and position.

Types of Rotary Encoders

There are mainly two types of rotary encoders

  1. Absolute rotary encoder
  2. Incremental rotary encoder

The difference between the two is that the output of the absolute rotary encoder will tell you about the current position of the shaft, making them angle transducers while the output of the incremental encoder will give information about the motion of the shaft which can further be used to measure the speed, distance and the position.

The encoder we have used is the incremental encoder. This rotary encoder is also known as relative or quadrature rotary encoder and the output of this encoder is series of square wave pulses.

Working of Rotary Encoder

The rotary encoder generates two square waves which have a phase difference of 90 degrees. The two waves are known as the channel A and channel B.
We can distinguish the direction of the movement by looking into the output square waves. You can see in the below figure that the two waves are 90 degrees out of phase with each other. If the rotary encoder has moved clockwise, then the output wave A will be ahead of output wave B and if the rotary encoder has moved counter clockwise, then the output wave B will be ahead of output wave A.

Timing Diagram

Timing Diagram

Also you can see from the figure that while rotating clockwise, the channel A will high and the channel B will be low and while rotating anti-clockwise, both channels A and B will be high.

If both channels A and B are high, then the switch will rotate counter clockwise. If channel A is high while the channel B is low, then the switch rotates clockwise.

Circuit Diagram and Explanation

Connect the pins of the rotary encoder to the Arduino as follows

  • CLK pin to the pin 2 of Arduino
  • DT pin to the pin 3 of Arduino
  • SW pin to the pin 4 of Arduino
  • VCC and the GND to the 5V and GND of Arduino
rotary encoder arduino interfacing

Rotary Encoder Arduino Interfacing

Code

const int clkPin= 2; //the clk attach to pin 2
const int dtPin= 3; //the dt pin attach to pin 3
const int swPin= 4 ;//the sw pin attach to pin 4

int encoderVal = 0;

void setup()
{
//set clkPin,dePin,swPin as INPUT
pinMode(clkPin, INPUT);
pinMode(dtPin, INPUT);
pinMode(swPin, INPUT);
digitalWrite(swPin, HIGH);
Serial.begin(9600); // initialize serial communications at 9600 bps

}

void loop()
{
int change = getEncoderTurn();//
encoderVal = encoderVal + change;
if(digitalRead(swPin) == LOW)//if button pull down
{
encoderVal = 0;
}
Serial.println(encoderVal);
}

int getEncoderTurn(void)
{
static int oldA = HIGH; //set the oldA as HIGH
static int oldB = HIGH; //set the oldB as HIGH
int result = 0;
int newA = digitalRead(clkPin);//read the value of clkPin to newA
int newB = digitalRead(dtPin);//read the value of dtPin to newB
if (newA != oldA || newB != oldB) //if the value of clkPin or the dtPin has changed
{
// something has changed
if (oldA == HIGH && newA == LOW)
{
result = (oldB * 2 - 1);
}
}
oldA = newA;
oldB = newB;
return result;
}

Video


If you have any questions, feel free to ask us in the comment section.

JOIN OUR NEWSLETTER
We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
We hate spam. Your email address will not be sold or shared with anyone else.

Leave a Reply

Your email address will not be published.

Subscribe To Our List

We help electronics hobbyists to create projects related to Micro-controllers. Join our newsletter to get access to new projects first.
Email address
Secure and Spam free...
Skip to toolbar