Menu Close

Arduino- 360 Degree Encoder

Robo India presents tutorial on how to use Rotary Encoder with Arduino.

1. Introduction:

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. The shaft has unlimited 360 degree rotation.

2. Hardware Interfacing: 

The interface of 360 Degree Encoder is very basic consisting of five pins.
CLK and DT Pins: You can connect these pins to any digital pins of Arduino. SW Pin is the button Pin.Other two pins are VCC and GND.

3. Connecting with Arduino UNO

Connect 360 Degree Encoder to Arduino Uno as following :

4. Programming

You may download this Arduino Sketch from here.

 #define outputA 7     //DT pin 
 #define outputB 6     //CLK pin

 int counter = 0; 
 int aState;
 int aLastState; 
 boolean bcw; 

 void setup() 
 { 
   pinMode (outputA,INPUT);
   pinMode (outputB,INPUT);
   
   Serial.begin (9600);
   aLastState = digitalRead(outputA);    // Reads the initial state of the 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, means knob is rotating
   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 ++;
             bcw = true;
          } 
        else 
          {
            bcw = false;
            counter --;
          }
      Serial.print("Position: ");
      Serial.println(counter);
      if (bcw)
        {
          Serial.println ("clockwise");
        }
      else 
        {
          Serial.println("counterclockwise");
         }
    } 
aLastState = aState;              // Updates the previous state of the outputA with the current state
 }

5. Output

After uploading the code, open the serial monitor. Now, you can start to rotate your encoder’s shaft and see the output printed on the serial monitor.

If you have any query please write us at support@roboindia.com

Thanks and Regards
Content Development Team 
Robo India
https://roboindia.com


Leave a Reply