Reading Rotary Encoder :


Component list:

1.  Rotary Encoder

2.  Arduino Uno




This timing diagram shows the relationship between Clock and Data Signals.

When the knob turns Clockwise, CLK signal Leads the Data signal and when the knob turns Counter Clockwise, 

Data leads Clock signal.  This relationship can be used to determine to increment or decrement a counter.

Also can calculate the direction of rotation. This is the basis for the following Arduino code.

The Built in Switch is used to clear the Counter.

Arduino Code:

//Reading Rotary Encoder
//Ref: https://lastminuteengineers.com/rotary-encoder-arduino-tutorial/

// Rotary Encoder Inputs
#define CLK 6
#define DT 5
#define SW 7

int counter = 0;
int currentStateCLK;
int lastStateCLK;
String currentDir ="";
unsigned long lastButtonPress = 0;

void setup() {
  
  // Set encoder pins as inputs
  pinMode(CLK,INPUT);
  pinMode(DT,INPUT);
  pinMode(SW, INPUT_PULLUP);   //Enable Pullup internally for the SW

  // Setup Serial Monitor
  Serial.begin(9600);

  // Read the initial state of CLK
  lastStateCLK = digitalRead(CLK);
}

void loop() {
  
  // Read the current state of CLK
  currentStateCLK = digitalRead(CLK);

  // If last and current state of CLK are different, then pulse occurred
  // React to only 1 state change to avoid double count
  if (currentStateCLK != lastStateCLK  && currentStateCLK == 1){

    // If the DT state is different than the CLK state then
    // the encoder is rotating CCW so decrement
    if (digitalRead(DT) != currentStateCLK) {
      counter --;
      currentDir ="CCW";
    } else {
      // Encoder is rotating CW so increment
      counter ++;
      currentDir ="CW";
    }

    Serial.print("Direction: ");
    Serial.print(currentDir);
    Serial.print(" | Counter: ");
    Serial.println(counter);
  }

  // Remember last CLK state
  lastStateCLK = currentStateCLK;

  // Read the button state
  int btnState = digitalRead(SW);

  //If we detect LOW signal, button pressed => clear counter 
  if (btnState == LOW) {
    //if 50ms have passed since last LOW pulse, it means that the
    //button has been pressed, released and pressed again
    if (millis() - lastButtonPress > 50) {
      Serial.println("Button pressed!, Counter cleared");
      counter = 0;
      Serial.print("Counter = ");
      Serial.println(counter);
    }
    // Remember last button press event
    lastButtonPress = millis();
  }
  // Put in a slight delay to help debounce the ++reading
  delay(10);
}
Serial Monitor Output:

Direction: CW | Counter: 1
Direction: CW | Counter: 2
Direction: CW | Counter: 3
Direction: CW | Counter: 4
Direction: CW | Counter: 5
Direction: CW | Counter: 6
Direction: CW | Counter: 7
Direction: CW | Counter: 8
Direction: CW | Counter: 9
Direction: CW | Counter: 10
Direction: CW | Counter: 11
Button pressed!, Counter cleared
Counter = 0
Direction: CW | Counter: 1
Direction: CW | Counter: 2
Direction: CCW | Counter: 1
Direction: CCW | Counter: 0
Direction: CCW | Counter: -1
Direction: CCW | Counter: -2
Direction: CCW | Counter: -3
Direction: CCW | Counter: -4
Direction: CCW | Counter: -5
Direction: CCW | Counter: -6
Button pressed!, Counter 
cleared
Counter = 0
Direction: CW | Counter: 1
Direction: CW | Counter: 2
Direction: CW | Counter: 3


0Comments

For Comment you need to Login


Related Product

Rotary Encoder Module KY-040 with Knob

Rotary Encoder Module KY-040 with Knob

Description:As we rotate our encoder, the two output (DT & CLK) will change depending upon the p..

$3.75