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

Thread: Can't get my Teensy 4.1 to read Anmbest EC11 rotary encoder AT ALL

Hybrid View

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    5

    Can't get my Teensy 4.1 to read Anmbest EC11 rotary encoder AT ALL

    Hello all!

    I am trying to use one of these "Anmbest" brand rotary encoders: https://www.amazon.com/gp/product/B0...?ie=UTF8&psc=1

    ...with the tutorial from: https://www.instructables.com/Improv...coder-Reading/

    I'm using a Teensy 4.1 as the microcontroller.

    No matter what I try, I cannot read ANYTHING from the encoder. I've tried every possible combination of pins on the 3-pin side of the unit (on the theory that maybe I'm misreading their diagram and that maybe any one of them could be the intended GND). I've tried having the interrupts watch for FALLING as well as RISING. No dice. It won't read anything. The interrupts never fire, no matter how much I turn the knob or how I wire it. (I've tried several different knobs. Same problem.)

    Code is below.

    Any help would be very much appreciated!

    Best,

    Leah

    Code:
    /*******Interrupt-based Rotary Encoder Sketch*******
    by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
    */
    
    static int pinA = 6; // Our first hardware interrupt pin is digital pin 2
    static int pinB = 7; // Our second hardware interrupt pin is digital pin 3
    volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
    volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
    volatile byte encoderPos = 0; //this variable stores our current value of encoder position. Change to int or uin16_t instead of byte if you want to record a larger range than 0-255
    volatile byte oldEncPos = 0; //stores the last encoder position value so we can compare to the current reading and see if it has changed (so we know when to print to the serial monitor)
    volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent
    
    void setup() {
      pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
      pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
      attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
      attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
      Serial.begin(115200); // start the serial monitor link
    }
    
    void PinA(){
      cli(); //stop interrupts happening before we read pin values
      reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
      if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
        encoderPos --; //decrement the encoder's position count
        bFlag = 0; //reset flags for the next turn
        aFlag = 0; //reset flags for the next turn
      }
      else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
      sei(); //restart interrupts
    }
    
    void PinB(){
      cli(); //stop interrupts happening before we read pin values
      reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
      if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
        encoderPos ++; //increment the encoder's position count
        bFlag = 0; //reset flags for the next turn
        aFlag = 0; //reset flags for the next turn
      }
      else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
      sei(); //restart interrupts
    }
    
    void loop(){
      if(oldEncPos != encoderPos) {
        Serial.println(encoderPos);
        oldEncPos = encoderPos;
      } else {
        Serial.println("Got nothing...");
      }
      delay(500);
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	wiring-test.jpg 
Views:	10 
Size:	119.7 KB 
ID:	23926  

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,939
    This PJRC page might help? : pjrc.com/teensy/td_libs_Encoder.html

    Amazon page doesn't show the pins by function among the 5 - hopefully it is marked or you found notes?

  3. #3
    Senior Member
    Join Date
    Jul 2020
    Posts
    980
    The first thing to do is check the pinout of the encoder by buzzing it out with a multimeter or
    continuity tester. You need to start by being certain which pin is which and connect to A, B, gnd.
    Randomly guessing and hoping is not the way to make progress.

    Secondly you need to then check the encoder pins are connected to the Teensy pins again by buzzing them
    out once wired in the breadboard.

    Then power up and check (voltage setting, not resistance) that the encoder A and B are alternating
    from pulled-up (3.3V) to grounded (0V) as the knob is turned slowly.

    Only then does it make sense to start worrying about the software side of things, get the hardware in
    a known good state first.

  4. #4
    Junior Member
    Join Date
    Feb 2021
    Posts
    5
    I FIGURED IT OUT! :-)

    The problem was in these lines:

    attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)

    attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)

    Per https://www.arduino.cc/reference/en/...tachinterrupt/, I learned that the recommended value for parameter #1 to attachInterrupt() is to pass the output digitalPinToInterrupt(PIN_NUMBER).

    I thus changed my code to:

    attachInterrupt(digitalPinToInterrupt(pinA),PinA,C HANGE); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)

    attachInterrupt(digitalPinToInterrupt(pinA),PinB,C HANGE); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)

    Voila!

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    13,939
    Good the signal pins were found.

    The digitalPinToInterrupt() isn't needed on Teensy, but yes, it needs to see every CHANGE not just RISING.

    Better probably would be using the encoder library as linked in p#2: teensy/td_libs_Encoder.html

    It will have tested library code to best handle the encoder decoding on interrupts in the background.

Posting Permissions

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