Teensy 4 - Rotary Encoder Missing Steps ?

Status
Not open for further replies.

mendez

Active member
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++; } 
  } 
}
 

Attachments

  • Teensy 4.0 with Rotary Encoder.jpg
    Teensy 4.0 with Rotary Encoder.jpg
    52.2 KB · Views: 126
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.
 
DerekR thanks for your reply.

I'm using this one: https://www.amazon.com/Signswise-In...words=encoder&qid=1569032904&s=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
 
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.
 
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.
ELibrary.png
 
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.
 
@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:
 

Attachments

  • ENC1_test_xbar1.zip
    9.4 KB · Views: 89
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.
 
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?
 
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 :confused: Could it be a graphing problem???
 
Status
Not open for further replies.
Back
Top