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

Thread: 4.1 HW encoder index use

  1. #1
    Junior Member
    Join Date
    Jul 2020

    4.1 HW encoder index use

    I'm new to teensy... pretty exciting, coming from the UNO world.

    I've got a rotary encoder, with an index pulse for each revolution. I want to use that pulse to reset the count to zero for each revolution. I have great monitor output of the encoder position using the following code, but I can't figure out:

    1, where to set the input pin for the index
    2, how to set it up / what else I need to do.

    Also, my secondary goal is to use the index pulse to measure rpm of the encoder. I'm thinking I can use the freq measure feature to do this, but haven't got that far yet.

    Thank you for your help!

    #include "QuadEncoder.h"
    //If INDEXTriggerMode is set to RISING_EDGE or FALLING_EDGE`` the associated interrupt will fire and increment the indexCounter``` but the position counts will be reset to zero.
    //QuadEncoder(uint8_t encoder_ch = 1, uint8_t PhaseA_pin = 0, uint8_t PhaseB_pin = 1, uint8_t pin_pus = 0, uint8_t index_pin = 2, uint8_t home_pin = 255, uint8_t trigger_pin = 255);
    uint32_t mCurPosValue;
    uint32_t old_position = 0;
    uint32_t mCurPosValue1;
    uint32_t old_position1 = 0;
    uint8_t index_pin = 2;
    QuadEncoder myEnc1(1, 0, 1, 0, 4);  // Encoder on channel 1 of 4 available
    // Phase A (pin0), PhaseB(pin1), Pullups Req(0)
    uint8_t INDEXTriggerMode = 1; //0 - disabled, 1 - Use positive going edge-to-trigger initialization of position counters!, 2 - use falling
    void setup()
      while (!Serial && millis() < 4000);
      /* Initialize the ENC module. */
      myEnc1.EncConfig.INDEXTriggerMode = RISING_EDGE;
    void loop() {
      /* This read operation would capture all the position counter to responding hold registers. */
      mCurPosValue =;
      if (mCurPosValue != old_position) {
        /* Read the position values. */
        Serial.printf("Current position value1: %ld\r\n", mCurPosValue);
        Serial.printf("Position differential value1: %d\r\n", (int16_t)myEnc1.getHoldDifference());
        Serial.printf("Position HOLD revolution value1: %d\r\n", myEnc1.getHoldRevolution());
        Serial.printf("Index Counter: %d\r\n", myEnc1.indexCounter);
      old_position = mCurPosValue;

  2. #2
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    New York

    To start if you look at the ReadMe for the library or the Github repository the are some instructions and guidelines:

    But to answer your questions:
    1. If you look at the constructor for the setting your encoder:
    QuadEncoder myEnc1
    you would set the pin within that constructor. The whole constructor is defined as:
    QuadEncoder(uint8_t encoder_ch = 255, uint8_t PhaseA_pin = 255, uint8_t PhaseB_pin = 255, uint8_t pin_pus = 0, uint8_t index_pin = 255, uint8_t home_pin = 255, uint8_t trigger_pin = 255);
    so with the example you posted:
    QuadEncoder myEnc1(1, 0, 1, 0, 4);
    • 1 => encoder number,
    • 0 => Phase A pin number
    • 1 => Phase B pin number
    • 0 => no pullups and

    As to your second question. Use of the index pin is set up with the command:
      myEnc1.EncConfig.INDEXTriggerMode = RISING_EDGE;
    which means to trigger on the rising of the index pulse to reset the counters. Its all set up.

    Suggest you read the github repository instructions or the readme. Think you will find more information there.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Thank you so much! I just had to comment out a line I added that was trying to set the index PIN to 2 and move the index wire to pin 4 and now it works perfectly.

    Now off to try to figure out if I can use the freqcount to ultimately give me an RPM readout from the second index pin.

    Thanks again!

Posting Permissions

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