Introduction: Arduino Rotary Encoder Tutorial

Hi Guys in this instructables we will learn how to use Rotary Encoder with arduino.

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.

Step 1: Types of Rotary Encoder

There are mainly two types of rotary encoders

Absolute rotary encoder
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.

Step 2: 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.



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.

Step 3: Things You Need

For this instructables we will need following things :

Arduino uno

Jumper wires

Breadboard

Rotary Encoder

Step 4: Schmatics

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

Step 5: Code

Please copy the following code and upload it to the arduino Board :

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;
}

Step 6: Output

After connecting everything together and uploading the code to your arduino board , connect usb cable of arduino to the PC and open the serial monitor in arduino ide and when you will rotate the rotary Encoder you can see the values changing on the serial monitor of arduino ide.