issue with quadrature encoder

Hello,

I'm using a Bourns optical quadrature encoder with the high performance encoder library on a Leonardo. Electrically, things look great, but when I run the example sketch I get alternating 1, 0, 1, 0 when I turn the encoder, instead of a pulse count like expected. Does anyone have experience with this issue?

Code?

It’s just the example code that comes w/ the library:

/* Encoder Library - Basic Example
 * http://www.pjrc.com/teensy/td_libs_Encoder.html
 *
 * This example code is in the public domain.
 */

#include <Encoder.h>

// Change these two numbers to the pins connected to your encoder.
//   Best Performance: both pins have interrupt capability
//   Good Performance: only the first pin has interrupt capability
//   Low Performance:  neither pin has interrupt capability
Encoder myEnc(0,1);
//   avoid using pins with LEDs attached

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);
  }
}

Which Arduino are you using? Uno uses pin 0,1 for serial comm, so try using pins 2,3. And when you post code, please use "/code" tags. Makes it easier for us to read.

Sorry about that. I’m using the Leonardo and I have tried many pin combos to no avail.

Ah, Leonardo. I missed that! I would suspect that you have a wiring issue then. One channel is not rising and falling. You should be able to see rising and falling signals with a meter.

Checked with a scope, both channels are rising and falling 90deg apart as expected when measured at header. The weird thing is that it was working briefly, but now is not. It increments a single time when I swap the A and B channels at the pins.

edit: ok now things are weird. When I look at the signals on the scope, everything looks a-ok. When I read the pins in code, one direction, both channels go to zero at the same time, and in the other direction, both channels switch states at the same time. I am very confused.

edit 2: it appears there was a fault with the ground pin on the encoder. It is now detached from the enclosure. Note sure if it’s related to my problem. $30 encoder too. What the heck, Bourn??