How to use Rotary Encoder with Arduino

Before we proceed you have to have a clear concept about a Rotary Encoder and the working principle of this machine. Its a type of position sensor which is used for determining the angular position of a rotating shaft; Rotary Encoder generates an electrical signal to the rational movement and that could be either analog or digital.

In the market, there are different types of rotary encoders based on their usage. They could be classified by their output signal or sensing technology. But we are talking about the regular Rotary Encoder, which actually used in simplest position sensor to measure rotation. If you find this a bit tough you can see articles over Rotary Encoders on versatile websites, we hope they can help you out to understand the procedure. Now we hope you have a basic idea about Rotary Encoder and we will jump to the working principle of Rotary Encoder and here we are,

The encoder has a disc with a spaced contacted zone connected with common pin C and there are also two separate pins A and pin B. When the disc will rotate, then pin A and pin B will contact with the common pin C and there will generate two square wave output signals. You can use any of the two outputs to determine the rotated position if you just count the pulse of the signal. On the other hand, considering both signals at a time would help to have a good grip on the determination. Two output signals displaced at 90 degrees out of phase from each other. Output A will be ahead from the output B if the encode is rotating clockwise.

If you count the steps each time, the signal changes from low to high and high to low, you can notice that two output signal have opposite values. If the encoder is rotating counter clock-wise, the output signal will have the same values. So, you can easily program your controller to detect the encoder position and the direction of rotation.

Now, let’s have a look at a small experiment where we will show you how to use a rotary encoder with Arduino. Let’s pick a breakout board that has five pins where the first pin is output A and the second pin is output B. The third pin is button pin here and the remaining pins are consequently VCC and GND. Connect the output pins to any digital pins on the Arduino. 

Here is the Arduino code to run the program:

/*     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: “);



   aLastState = aState; // Updates the previous state of the outputA with the current state