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

Thread: Teensy 4 - Rotary Encoder Missing Steps ?

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    13

    Teensy 4 - Rotary Encoder Missing Steps ?

    Hi Guys,

    I have a rotary encoder connected to my T4 and for some reason it's losing a few steps / causing a noise. I made a simple sketch and I tested the T3.6 vs T4.0, and the 3.6 doesnt have the miss-steps.

    Please see attachment for a quick chart.

    Here is the Sample Sketch:
    Code:
    #include "USBHost_t36.h"          //5V Activation
    
    USBHost fivevolts;
    const int encoderPinA = 8;  // Green - pin 2 - Digital
    const int encoderPinB = 7; // White - pin 3 - Digital
    volatile unsigned int temp;
    volatile unsigned int encoder_pos = 0;
    volatile unsigned int rev_count_time = 0;
    String data;
    
    /*************************************VOID SETUP***************************************/
    void setup() 
    {
      Serial.begin(115200);                     //USB Baud Rate
      
      fivevolts.begin();                        //Activate 5V
     
      //Encoder
      pinMode( encoderPinA, INPUT_PULLUP ); 
      pinMode( encoderPinB, INPUT_PULLUP );
      attachInterrupt(encoderPinA, encoder_position_A, RISING );
      //attachInterrupt(encoderPinB, encoder_position_B, RISING );
        
      
    } 
    
      
    /*************************************VOID LOOP****************************************/
    void loop() {
      
      if (encoder_pos != temp)
      {
        //rev_count_time = micros();
        data = String(encoder_pos) + "," + String(rev_count_time);
        Serial.println (data);
        temp = encoder_pos;
      }
    
    
    
    }
    
    void encoder_position_A()
    {
      
      rev_count_time = micros();
      if(digitalRead(encoderPinB)==LOW) 
      { 
        if (temp >= 1199) {encoder_pos = 0;}
        else{ encoder_pos++; }
      }
      else 
      {
        if (temp == 0) {encoder_pos = 1199;}
        else{ encoder_pos--;}
      }
    }
    
    void encoder_position_B()
    {
      rev_count_time = micros();
      if(digitalRead(encoderPinA)==LOW) 
      {     
        if (temp == 0) {encoder_pos = 1199;}
        else{ encoder_pos--;}
      }
      else 
      {       
        if (temp >= 1199) {encoder_pos = 0;}
        else{ encoder_pos++; } 
      } 
    }
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Teensy 4.0 with Rotary Encoder.jpg 
Views:	28 
Size:	52.2 KB 
ID:	17657  

  2. #2
    Senior Member
    Join Date
    May 2016
    Posts
    230
    What sort on encoder are you using - optical? mechanical? Is it the quadrature type?

    If it is mechanical - you may need to debounce the switches electrically (R-C network). That could be the source of your problems.

    When I look at your code I can't see that you are using any of the quadrature features of encoders to get 4x the resolution and the direction of rotation. Are you aware of the Arduino/Teensy Encoder library? I've used it very successfully for years.

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    13
    DerekR thanks for your reply.

    I'm using this one: https://www.amazon.com/Signswise-Inc...gateway&sr=8-3

    I will do some reaserch on how to do R-C network (never done it).

    I was not aware of the Encoder Library; I will check it out. thanks...

    Now, even if Im not using the library; I was trying to do an Apples to Apples comparison....same code but different chip. It should work similarlly right? the encoder was not moving that fast either

  4. #4
    Senior Member
    Join Date
    May 2016
    Posts
    230
    Debouncing is real easy. Basically you just put a capacitor across the switch. When the switch opens the capacitor charges through the pull-up resistor, and the voltage rises "slowly". I'm not sure what your rotational speed is speed is - that will determine the R x C time constant you should use.

  5. #5
    Junior Member
    Join Date
    Aug 2018
    Posts
    13
    Thanks. I tried the encoder library and loaded the basic encoder sketch and I can see the noise too. I did a simple chart again. I tried with another encoder and I saw a similar noise so I don't think is the encoder.
    Click image for larger version. 

Name:	ELibrary.png 
Views:	21 
Size:	24.2 KB 
ID:	17658

  6. #6
    Junior Member
    Join Date
    Oct 2014
    Posts
    1
    I've never needed to debounce an optical encoder. Not that it should matter on the T4, but try using digitalReadFast in place of digitalRead.

  7. #7
    Senior Member+ mjs513's Avatar
    Join Date
    Jul 2014
    Location
    New York
    Posts
    4,335
    @mendez
    Curious that it seems your "noise" is isolated to around 5500-5700 (rpm? counts?). In going through the comments on the amazon page I saw that I person actually installed 10k pullups to the VCC (5v) - don't want to do that here since T4 is only rated to 3.3v on the pins so you would need to use a level converter.

    But if you want to give something else I try I am attaching a sketch that uses the HW encoder that is builtin to the T4. Currently set up to run on pins 2 and 3. If you want to give that a try and see if its gives the same values you can:
    Attached Files Attached Files

  8. #8
    Senior Member
    Join Date
    Nov 2017
    Location
    Belgium
    Posts
    167
    Quote Originally Posted by mjs513 View Post
    In going through the comments on the amazon page I saw that I person actually installed 10k pullups to the VCC (5v) - don't want to do that here since T4 is only rated to 3.3v on the pins so you would need to use a level converter.
    No need for a level converter. Just connect the pullups to 3.3V and you're good to go.

  9. #9
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    578
    I don't think the graphs you showed point to missing steps. Missing steps would lead to a parallel shift in the curve. If this curves were created by missed steps you would mysteriously get the same amount of additional steps afterwards to compensate for the previously missed steps... not very likely. I might be wrong but this does not look like a hardware issue to me. Can you post the sketch you used to generate the graph in #5?
    BTW: I assume the x-Axis shows time, the y-Axis encoder position?

  10. #10
    Senior Member
    Join Date
    May 2016
    Posts
    230
    I agree in principle with @luni but look at the y scale: the graph axis is given at intervals of 5000 steps! What I don't understand is that the glitch seems to drop down and then recover almost immediately. I've never seen an encoder do that Could it be a graphing problem???

Posting Permissions

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