Rotary Encoder

From Leo's Notes
Revision as of 18:51, 12 October 2020 by Leo (talk | contribs) (Created page with "A Rotary Encoder is a device that detects angular position or rotation. == Usage == There are two types of rotary encoders: Absolute and Incremental. === Absolute === Absolu...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
This page was last edited on 12 October 2020, at 18:51.

A Rotary Encoder is a device that detects angular position or rotation.

Usage

There are two types of rotary encoders: Absolute and Incremental.

Absolute

Absolute rotary encoders can tell us the exact position of the shaft.

Incremental

Incremental rotary encoders can tell us how many increments the shaft has rotated. These encoders have two output pins, sometimes labeled as DT and CLK. The two are essentially interchangeable and are sometimes referred to pin A and pin B. As the shaft is rotated clockwise, A will go high before B. As it continues to be rotated, A will then go low before B. The same happens in reverse when the shaft is rotated counterclockwise. Using this behavior, we can easily count the number of increments and direction that a shaft has rotated.

Arduino Usage

These rotary encoders when used naively seems to result in jitter where it may for example sense correctly a clockwise rotation but incorrectly senses an counterclockwise step midway. This is mitigated by using a state machine so that any unreasonable inputs are ignored. The MD_REncoder library or the Arduino does just that and can be found at: https://github.com/MajicDesigns/MD_REncoder

For a more responsive experience, you may wish to use interrupts rather than polling such as with the case of the MD_REncoder library. This can be done by tying both A and B inputs from the rotary encoder to an interrupt handler. Here is some code I hacked out really late one night. I had to add the direction check in order to mitigate the jittering issue (it isn't perfect but is sufficient).

const uint8_t RE_A_PIN = 3; // dt
const uint8_t RE_B_PIN = 4; // clk

int re_counter = 0;
int re_last_clk;
int re_last_dir = 0;

void input_setup(void) {
  pinMode(RE_PIN,    INPUT_PULLUP);
  pinMode(RE_A_PIN,  INPUT_PULLUP);
  pinMode(RE_B_PIN,  INPUT_PULLUP);

  // see https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/
  attachInterrupt(digitalPinToInterrupt(RE_A_PIN), rotary_isr, CHANGE);
  attachInterrupt(digitalPinToInterrupt(RE_B_PIN), rotary_isr, CHANGE);
}


void rotary_isr() {
  cli(); // stop interrupts

  int re_a = digitalRead(RE_A_PIN);
  int re_b = digitalRead(RE_B_PIN);

  if (re_a && ! re_b) {
    re_counter--;
  }
  if (re_a && re_b) {
    re_counter++;
  }

  // avoid random wrong direction
  if (re_last_clk - millis() < 100) {
    if (re_counter > 0 && re_last_dir < 0) {
      // uh oh.
      re_counter = 0;
    }
    if (re_counter < 0 && re_last_dir > 0) {
      // uh oh.
      re_counter = 0;
    }
  }

  re_last_clk = millis();

  // resume interrupts
  sei();
}

Devices

KY-040

The KY-040 is an incremental rotary encoder. A full rotation produces 20 pulses. times for a complete rotation.