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

Thread: Encoder.h

  1. #1

    Encoder.h

    We use Enconder.h library with two Rotary Encoder 400P-R 6mm-Incremental-Optical Encoders.
    https://www.fabtolab.com/optical-rot...oder-400-pulse

    When we use example code TwoKnobs with teensy 3.5 . Program gives result 1600 . but we read with arduino mega before 400 .
    We pulled -up 10K to 5V all signall entries to Teensy .without pull-up , rpogram gives around 2000

    Why ?

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,534
    Might be a speed problem - The Teensy is sooooo much quicker than this old slow Arduino Mega. But if you don't respect the forum rules, sharing code to reproduce the problem and details about wiring (i.e. schematics), pictures of you probing the A and B phase signals with an oscilloscope, etc., it's not easy to help you.

  3. #3
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,534
    Just as a hint : With these "de luxe" encoders which you use and with the Teensy 3.5 you can have much quicker, more precise, and less resource consuming encoder readings if you don't use that slow encoder.h library, but if you use the native encoder reading feature of the Teensy's integrated flex timers. Details can be found in the hardware reference manual.

  4. #4
    Quote Originally Posted by Theremingenieur View Post
    Might be a speed problem - The Teensy is sooooo much quicker than this old slow Arduino Mega. But if you don't respect the forum rules, sharing code to reproduce the problem and details about wiring (i.e. schematics), pictures of you probing the A and B phase signals with an oscilloscope, etc., it's not easy to help you.

    #include <Encoder.h>

    #define motorHizA 5 // enable1
    #define motorA2 7 // motor1ileri //Sag
    #define motorA1 6 // motor1geri
    #define motorB2 9 // motor2iler //sol
    #define motorB1 8 // motor2geri
    #define motorHizB 10 // enable2

    // Change these pin numbers to the pins connected to your encoder.
    // Best Performance: both pins have interrupt capability
    // Good Performance: only the first pin has interrupt capability
    // Low Performance: neither pin has interrupt capability

    Encoder knobLeft(28, 27);
    Encoder knobRight(29, 30);
    // avoid using pins with LEDs attached

    void setup() {
    Serial.begin(9600);
    Serial.println("TwoKnobs Encoder Test:");

    pinMode(motorA1, OUTPUT);
    pinMode(motorA2, OUTPUT);
    pinMode(motorHizA, OUTPUT);
    pinMode(motorB1, OUTPUT);
    pinMode(motorB2, OUTPUT);
    pinMode(motorHizB, OUTPUT);
    }

    long positionLeft = -999;
    long positionRight = -999;
    long newLeft, newRight;

    void loop() {
    newLeft = knobLeft.read();
    newRight = knobRight.read();

    analogWrite(motorHizA, 255);
    digitalWrite(motorA1, HIGH);
    digitalWrite(motorA2, LOW);
    analogWrite(motorHizB, 255);
    digitalWrite(motorB1, HIGH);
    digitalWrite(motorB2, LOW);

    Serial.print("Left = ");
    Serial.print(newLeft);
    Serial.print(", Right = ");
    Serial.println(newRight);

    if (newLeft != positionLeft || newRight != positionRight) {
    positionLeft = newLeft;
    positionRight = newRight;
    }
    // if a character is sent from the serial monitor,
    // reset both back to zero.
    if (Serial.available()) {
    Serial.read();
    Serial.println("Reset both knobs to zero");
    knobLeft.write(0);
    knobRight.write(0);
    }
    }wi
    Code is Teensy sample code of teensy library Encoder .https://www.pjrc.com/teensy/td_libs_Encoder.html .Sorry i do not have oscilloscope .Wiring GND-GND , 5v to Encoder 5V and PİN27,28,29,30 connect to 5V with 10 K

  5. #5
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    354
    1600 counts per revolution is correct when using a true quadrature decoder with a 400 pulse two-phase Incremental Rotary Encoder.
    Last edited by Wozzy; 01-15-2018 at 02:25 PM.

  6. #6
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    2,534
    Quote Originally Posted by Wozzy View Post
    1600 counts per revolution is correct when using a true quadrature decoder with a two-phase Incremental Rotary Encoder.
    You mean that since the encoder gives 400 pulses per revolution, I have 4 counts per pulse (A up, B up, A down, B down for example) ? In that case, the problem can be solved for the OP by a (x >> 2) operation on the readings if he wants backwards-compatibility to his old AtMega stuff.

  7. #7
    Senior Member Wozzy's Avatar
    Join Date
    Jan 2013
    Location
    Philadelphia, Pennsylvania USA
    Posts
    354
    Yes.
    A 2 phase quadrature encoder has 400 pulses/rev for each channel, 90 deg out of phase. This gives 1 count per edge, and direction.

Posting Permissions

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