Encoder - detect single or dual inputs

m_elias

New member
I'm playing around with the Teensy Encoder library here:

Code:
#include <Encoder.h>
Encoder encoder(A13, 37);

void setup() {
  Serial.begin(115200);
  Serial.println("Quad/Single Encoder Test:");
}

long prevRead  = -999;
uint8_t singleCount;

void loop() {
  long newRead;
  newRead = encoder.read();
  if (newRead != prevRead) {
    singleCount++;
    Serial.print("\nPosition = ");
    Serial.print(newRead);
    Serial.print(" single count:");
    Serial.print(singleCount);
    prevRead = newRead;
  }
}

It's working great but I'm trying to figure out a clever way to auto detect when only one input is connected (as in a single encoder) instead of a quadrature (with dual inputs). singleCount increments in both input configurations but obviously the position reported by encoder.read() just toggles back and forth between two values when there's only one input connected. Any ideas how to I can detect when there's only one input connected?
 
There is no precise way since a single input produces a signal indistinguishable from a quadrature encoder oscillating back and forth. You could have a statistical test on the assumption such oscillation is too rare to worry about. Simply count the transitions on the input, and reset the count if the other input changes. If the count exceeds some threshold you could switch to single mode.

However its only statistical and may get it wrong.
 
That occurred to me too but the reason for adding support for quadratures is to prevent counter windup from this exact situation (single input wiggling back and forth on the edge). I wondered about modifying the library to run each input through a test to see if there's anything externally connected, input_pull-up, read input, input_pull-down, read input and compare. Then I can also increment singleCount inside the ISR. Is there a lot of simplification I can do in the library if I'm only going to be using a Teensy 4.1?
1707486918769.png
 
I wondered about modifying the library to run each input through a test to see if there's anything externally connected
Not going to work if that encoder pin happens to be open circuit at the time of the test - as I said there is no precise way to distinguish the cases for standard encoders.
 
Back
Top