Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 6 of 6

Thread: Encoder behaving strangely on Teensy, code works on Uno

  1. #1
    Junior Member
    Join Date
    Mar 2021
    Posts
    2

    Encoder behaving strangely on Teensy, code works on Uno

    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.

  2. #2
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    414
    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.

  3. #3
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,450
    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 by luni; 03-30-2021 at 11:39 AM.

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,101
    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.

  5. #5
    Senior Member
    Join Date
    Jul 2020
    Posts
    980
    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.

  6. #6
    Junior Member
    Join Date
    Mar 2021
    Posts
    2
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •