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

Thread: Encoder library with cheap encoders.

  1. #1

    Encoder library with cheap encoders.

    I have had trouble with encoders skipping counts and sometimes even stepping in the opposite direction.
    I am using a teensy 3.6 with the encoder library 1.4.1

    the sketch is the example on the Encoder library page of PJRC.com

    Code:
    #include <Encoder.h>
    
    // Change these pin 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 knobLeft(15, 16);
    Encoder knobRight(22, 21);
    //   avoid using pins with LEDs attached
    
    void setup() {
      Serial.begin(9600);
      Serial.println("TwoKnobs Encoder Test:");
    }
    
    long positionLeft  = -999;
    long positionRight = -999;
    
    void loop() {
      long newLeft, newRight;
      newLeft = knobLeft.read();
      newRight = knobRight.read();
      if (newLeft != positionLeft || newRight != positionRight) {
        Serial.print("Left = ");
        Serial.print(newLeft);
        Serial.print(", Right = ");
        Serial.print(newRight);
        Serial.println();
        positionLeft = newLeft;
        positionRight = newRight;
      }
      // if a character is sent from the serial monitor,
      // reset both back to zero.
      if (Serial.available()) {
        Serial.read();
        Serial.println("Reset both knobs to zero");
        knobLeft.write(0);
        knobRight.write(0);
      }
    }
    this is the typical output from one "click" sometimes it is more and sometimes fewer outputs, and both encoders do the same thing.

    Code:
    Reset both knobs to zero
    Left = 1, Right = 0
    Left = 0, Right = 0
    Left = 1, Right = 0
    Left = 0, Right = 0
    Left = 1, Right = 0
    Left = 0, Right = 0
    Left = 1, Right = 0
    Left = 0, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 1, Right = 0
    Left = 2, Right = 0
    Left = 3, Right = 0
    Left = 2, Right = 0
    Left = 3, Right = 0
    Left = 4, Right = 0
    Left = 3, Right = 0
    Left = 4, Right = 0
    Left = 3, Right = 0
    Left = 4, Right = 0
    Left = 3, Right = 0
    Left = 4, Right = 0
    Left = 3, Right = 0
    Left = 4, Right = 0
    is that bad contact bounce? if so is there some way to incorporate a debounce function in to the library or add it after the output?

    thanks!

  2. #2
    Member
    Join Date
    Aug 2019
    Location
    Melbourne Australia
    Posts
    55
    If you don't mind some soldering, this usually works.

    Click image for larger version. 

Name:	Bourns encoder debounce.jpg 
Views:	0 
Size:	24.9 KB 
ID:	18806

    Note that for a 3.3v board you would feed it with +3.3v.

  3. #3
    Thanks! I ended up adapting the code at the bottom of this page https://www.best-microcontroller-pro...y-encoder.html one of my encoders was still not so great, but a short delay (conveniently the same length as my control ISR) makes it completely predictable and nice to use, It even saved a few cycles.

Posting Permissions

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