GRBL with Rotary Encoders Problem 2

Hey Guys!

I connected a 4-Pin Rotary Encoder directly to my NEMA 17 Double Shaft Stepper Motor, the problem is the Encoder gets me different values depending on the speed of the Stepper, but the values are always the same at the same speed. The maximum Speed of my Stepper is 3000 mm/min

So I got the following results:

G1 X100 F100 = 100mm --> Correct G1 X100 F200 = 100mm --> Correct G1 X100 F300 = 98,6mm --> Error G1 X100 F400 = 95mm --> Error G1 X100 F500 = 76mm --> Error G1 X100 F600 = 48,7mm --> Error .. .. .. .. G0 X100 = 17,5mm

Can anybody tell me what have I done wrong? Hope you can help

King regards

I think that we need a schematic, encoder data sheet and your code to answer th a question without a lot of guesswork.

Sorry!

This is the encoder: https://www.amazon.com/Industry-Park-Encoder-5V-24V-Incremental/dp/B01LY96PCU/ref=sr_1_1?ie=UTF8&qid=1491759596&sr=8-1-spons&keywords=rotary+encoder&psc=1

Connection: Red - Arduino 5V Black - Arduino GND White - Arduino D9 Green - Arduino D10

This is the Code:

unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
int i = 0;

void setup() {
    Serial.begin(115200);
    pinMode(10, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
}

void loop() {
    encoder_A = digitalRead(10);
    encoder_B = digitalRead(9);   
    if((!encoder_A) && (encoder_A_prev)){
      if(encoder_B) {
        i++;             
      }else {
        i--;
      }
      
     int hmm = i / 0.75;
     Serial.print("Steps: ");
     Serial.print(i);
     Serial.print("   ");
     Serial.print("Hmm: ");
     Serial.println(hmm);
     
    }   
    encoder_A_prev = encoder_A;  
}

Maybe printing inside the loop that reads the encder is taking enough time, at higher rates, to cause missed pulses with a 600 PPM encoder.

Try it like this:

unsigned char encoder_A;
unsigned char encoder_B;
unsigned char encoder_A_prev=0;
int i = 0;

void setup() {
    Serial.begin(115200);
    pinMode(10, INPUT_PULLUP);
    pinMode(9, INPUT_PULLUP);
}

void loop() {
    encoder_A = digitalRead(10);
    encoder_B = digitalRead(9);   
    if((!encoder_A) && (encoder_A_prev)){
       encoder_A_prev = encoder_A; 
      if(encoder_B) {
        i++;             
      }else {
        i--;
      }
     
     int hmm = i / 0.75;
     Serial.print("Steps: ");
     Serial.print(i);
     Serial.print("   ");
     Serial.print("Hmm: ");
     Serial.println(hmm);
     
    }   
   
}

Thanks for your reply!

I've tried your code but with it, it don't sends me any positions back

King regards

You should use an interrupt-driven encoder code, then you'll be able to keep up with the high pulse rates reliably.