Encoder behaving strangely on Teensy, code works on Uno

Status
Not open for further replies.

metic

New member
Hey all, I've run into a problem which I'm guessing has a simple solution, but it really has me baffled. In the initial stages of my project I developed using an Arduino Uno, and later on when I needed a fast PWM frequency I switched to a Teensy 3.1 I had on hand. When I did so, the rotary encoder inputs I was using all started behaving strangely. I reduced the problem to it simplest form, and tested with the example provided with the library I was using, this library specifically: https://github.com/PaulStoffregen/Encoder

This is the code I'm testing with:

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

On an Arduino Uno, the output is as expected. Turning the encoder in one direction prints an increasing integer value, and turning it in the other direction a decreasing integer value. One the Teensy 3.1, I get this:

Code:
Basic Encoder Test:
0
-1
0
-1
0
-1
0
-1
0
-1
0
-1
0

Depending on which pins I use, I get either -1,0 and 1,0. I've tried different pins, manually disabling and enabling interrupts, testing at 3.3V on the Arduino, and tried several different encoders including one that's on a custom board I designed with hardware debouncing. I'm stumped, so any help would be greatly appreciated.
 
Strange,

I took your sketch, added the Encoder library after installing it, checking version Encoder by Paul Stoffregen v 1.4.2.

Hooked up a cheap encoder on a breakout board with 10K pullup resistor on the board, the marking says so, to gnd, 3.3V, pins 2 and 3 on a Teensy 3.0 on a breadboard.

Compiled and loaded, checked monitor output, looks perfect, counting up and down. Turning faster I got a few, very few bounces with numbers doing a single count in the wrong direction before moving on in the right direction.
 
Wild guess: Can it be that you have your common encoder pin connected to VCC instead of GND?

If you have a scope or an LA you can have a look at the pins if they are actually changing while you slowly rotate the encoder. (note: for most encoders changes only happen between two encoder detents. So, rotate slowly). Alternatively you can do a simple sketch reading in the two pins and see if they change.


'
 
Last edited:
That pattern, alternating between 0 and 1 or between 0 and -1, is exactly what happens when only 1 of the 2 signal wires is connected.
 
Or is shorted, or lacks proper pull-up resistor - I suggest using a multimeter and turning the encoder very slowly to check the
voltages are changing as expected on either signal.
 
Thanks guy, your replies were all very helpful. I managed to do a bit more debugging and discovered that after all that I had a bad jumper wire.
 
Status
Not open for further replies.
Back
Top