SURYATEJA
Published © Apache-2.0

Rotary Encoder or Arduino 360 Degree Encoder

Working principles of rotary encoder are demonstrated here.

BeginnerProtip1 hour12,841
Rotary Encoder or Arduino 360 Degree Encoder

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Rotary Encoder with Push-Button
Rotary Encoder with Push-Button
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

CONNECTIONS

Schematics

CONNECTIONS

Code

CODE

Arduino
/**************
 * VCC to 5V  *
 * GND to GND *
 * CLK to D3  *
 * CLK to D4  *
 **************/
int pinA = 3;
int pinB = 4; 
int encoderPosCount = 0; 
int pinALast;  
int aVal;
boolean bCW;

 void setup() 
 { 
  //SET pinA and pinB and input
   pinMode (pinA,INPUT);
   pinMode (pinB,INPUT);
   pinALast = digitalRead(pinA);//Read Pin A 
   Serial.begin (9600);
   Serial.println("BEGIN");
   Serial.println();
 } 

 void loop() 
 { 
   aVal = digitalRead(pinA);
   if (aVal != pinALast)
   { 
     if (digitalRead(pinB) != aVal) //We're Rotating Clockwise
     { 
       encoderPosCount ++;
       bCW = true;
     } 
     
     else 
     
     {
       bCW = false;
       encoderPosCount--;
     }
     
     if (bCW)
     {
       Serial.println ("Rotate Clockwise");
     }
     
     else
     
     {
       Serial.println("Rotate Counterclockwise");
     }
     
     Serial.print("Encoder Count: ");
     Serial.println(encoderPosCount);
     Serial.println();
   } 
   
   pinALast = aVal;
 } 

Credits

SURYATEJA

SURYATEJA

18 projects • 59 followers

Comments