0

I am testing 400 PPR(Pulses Per Revolution) rotary encoder using code.I am rotating it by using the hand. I have connected Phase A and Phase B and also, ground and Vcc properly. The code is giving less than 400 counts in every rotation.eg. sometimes 385,350,360,390. Maximum time the range is in between 350 to 400. The code is given below:

#define PIN1 14
#define PIN2 10

volatile long pos;

volatile uint8_t state;

void pin_isr(void) 
{
    uint8_t p1val = digitalRead(PIN1);

    uint8_t p2val = digitalRead(PIN2);

    uint8_t s = state & 3;

    if (p1val) s |= 4;
    if (p2val) s |= 8;
    state = (s >> 2);

    switch (s) {
        case 1: case 7: case 8: case 14:
            pos++;
            return;
        case 2: case 4: case 11: case 13:
            pos--;
            return;
        case 3: case 12:
            pos += 2;
            return;
        case 6: case 9:
            pos -= 2;
            return;
    }
}
int main(int argc, const char * argv[]) {

    // sets up the wiringPi library
    if (wiringPiSetup() < 0) {
        perror("wiringPiSetup");
        exit(EXIT_FAILURE);
    }   
    pinMode (PIN1,  INPUT) ;
    pinMode (PIN2,  INPUT) ;
    pullUpDnControl(PIN1, PUD_UP);
    pullUpDnControl(PIN2, PUD_UP); 
    if ( wiringPiISR (PIN1, INT_EDGE_BOTH, &pin_isr) < 0 ) {
        perror("wiringPiISR");
        exit(EXIT_FAILURE);
    }

    if ( wiringPiISR (PIN2, INT_EDGE_BOTH, &pin_isr) < 0 ) {
        perror("wiringPiISR");
        exit(EXIT_FAILURE);
    }

    // Show position every second
    while ( 1 ) {
        printf( "%ld\n", pos);
        delay( 10 ); // wait 1 second
    }   
    return 0;
}

I have written similar code for Arduino which perfectly working fine. Every time I am getting exactly 400 counts per rotation. Does anyone know what is the exact problem?

0

You have not given enough details. The most important factor is the frequency of pulses. Is that 400 pulses an hour, a minute, a second, a millisecond?

I am going to assume the pulses are too frequent and the Linux interrupt system can not keep up.

Try one of the following examples to see if you get the same problem.

C: http://abyz.me.uk/rpi/pigpio/examples.html#C_rotary_encoder_c

C + pigpio daemon: http://abyz.me.uk/rpi/pigpio/examples.html#pdif2_RED

Python + pigpio daemon: http://abyz.me.uk/rpi/pigpio/examples.html#Python_rotary_encoder_py

  • I have mention PPR (Pulses per revolution). I am rotating the encoder by using the hand.So, whenever I am rotating it I am getting pulses less than 400 in one revolution – Ashish Jan 2 '18 at 12:07
  • @Ashish Are you doing one rotation an hour, a minute, a second, a millisecond? – joan Jan 2 '18 at 12:18
  • 1
    maybe you have misunderstood the operation of the encoder. Encoder gives the pulses when we revolve (its shaft) it by using external force.Here is the link of the encoder which I am using: robu.in/run-rotary-encoder-arduino-code – Ashish Jan 2 '18 at 12:26
  • Also, i am using wiring pi library and revolution of the encoder to complete the one rotation is taking at least one second. – Ashish Jan 2 '18 at 12:41
  • @Ashish You need to try one of the examples I gave. Your code will not magically start working. – joan Jan 2 '18 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.