Quadrature Encoder Libraries - Only Works in One Direction

Hi All,

I've tried getting a couple encoders working with both an Arduino Uno and Mega, with the Encoder libraries on the Playground page ( specifically Encoder Library, for Measuring Quadarature Encoded Position or Rotation Signals ) but for some reason I can't get them to work in both directions. I have a 24 PPR encoder, with both channels hooked up to interrupt pins, and with the above library, I should be getting 96 counts per revolution. This holds true for the clockwise direction, but when I turn it counter-clockwise, instead of decrementing the counter one by one, it gets stuck in a loop, with a counting sequence that goes like so, as an example:

42
41
40
42
41
40
42
41
40

I verified the output from each channel with an oscilloscope and see that the clockwise sequence for each channel is:

A & B
1 - 1
0 - 0
1 - 0
1 - 1

and counter-clockwise, the sequence is:

A & B
1 - 1
1 - 0
0 - 0
1 - 1

So going back to the loop it gets stuck in, it seems that in two of the three counter-clockwise "steps", it correctly detects that its going in the counter-clockwise direction. But then on the third step, it falsely detects that it's going clockwise and jumps back up two increments. The loop keeps repeating and overall it doesn't increment correctly.

I hope this made sense, I'll post the code, but it's the generic code with the only modification being the pins I'm using. I'm honestly unsure of what the problem could be, as it's a very simple circuit and it's all wired properly. I'm wondering if anyone's experienced this as well, and maybe the program needs a slight adjustment. I'm going to look into modifying the library myself, and will post any solution I may find. Thanks in advance for the help.

#include <Encoder.h>

Encoder myEnc(2, 3);

void setup() {
  Serial.begin(9600);
  Serial.println("Basic Encoder Test:");
}

long oldPosition  = -999;

void loop() {
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    oldPosition = newPosition;
    Serial.println(newPosition);
  }
}

Pat

I've figured it out; I may have a different kind of encoder or something in which the sequence is different, but as I suspected there was a sequence in the counter-clockwise direction that needed to be changed from +2 on the counter to -2. Now it decrements perfectly and the resolution is in fact 96 counts to a turn.

Pat

It is not a Gray code - the sequence 11 to 00 or 00 to 11 is incorrect - changing two positions same time.
Only one should change irregardless of direction.

Agreed. I think the encoder is bad.