1

I have a HP Optical Incremental Encoder (256 CPR) in which

  • Pin 1 = A,
  • Pin 2 = VCC,
  • Pin 3 = GND,
  • Pin 8 = B.

I already read and see tutorials for rotary encoder such this one:
https://playground.arduino.cc/Main/RotaryEncoders

I have written a program that is supposed to print out the position of my encoder, however I am only getting zeros in my serial monitor. I want to be able to use interrupts. I would like to actually return the position in degrees of my encoder as we rotate it.

I was able to get it to work. I wasn't connecting my pin properly to my Arduino.

    #include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);


#define encoderPinA 2
#define encoderPinB 3
#define CPR 256
volatile int counter =0;
volatile boolean flag;

volatile int var_degrees =0;

void setup() {

  pinMode(encoderPinA, INPUT);
  pinMode(encoderPinB, INPUT);
  Serial.begin (9600);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), isr, RISING);
  lcd.clear();

}

void loop() {

   if(flag == true){     
        var_degrees = ((360/256.0)*counter);
        Serial.println(var_degrees);
        lcd.setCursor(0, 1);
        lcd.print("Degrees: ");
        lcd.setCursor(9, 1);
        lcd.print(var_degrees);
        flag = false;
  }

}
//Interrupts 

void isr_2(){
flag = true;
  if(digitalRead(encoderPinA) == HIGH){
    if(digitalRead(encoderPinB) == LOW){
      counter = counter -1; //COUNTER CLOCK WISE
    }
    else{
      counter = counter +1; //CW
    }
  }
  else{
    if(digitalRead(encoderPinB) == LOW){
      counter = counter + 1; //CW
    }
    else{
      counter = counter - 1 ; //CCW
    }
  }
}
1

You are attaching your interrupt incorrectly. You should not pass in the pin number, but the interrupt number. There is also a handy function that converts a pin to the right interrupt number:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode);

So, your code should be:

attachInterrupt(digitalPinToInterrupt(encoderPinA), isr,CHANGE);

Also, you should declare your counter variable to be volatile, since it is modified within an ISR, and thus the compiler should not assume that it doesn't change even though the loop() never changes it:

volatile int counter = 0;
| improve this answer | |
  • Quick question for this great community. I was able to read the position of my encoder and converted that position to an angle. However when I rotate the encoder right it gives me a negative output and when I rotate it left it gives me a positive output. It is backwards, I checked my Code and I can't find the issue. Output rotating right starting from zero: -1 -2 -4 -5 -7 -8 -9 -11 Output rotating right starting from zero: 0 1 2 4 5 7 8 9 11 – crucl Mar 28 '18 at 22:59
  • Switch the counter+1 and counter-1 – jose can u c Mar 28 '18 at 23:23
  • what do you mean? could you more specific please? – crucl Mar 28 '18 at 23:39
  • I'm not sure I can be more specific. There are places where you add one to counter and places where you subtract one from counter. Switch the adds with the subtracts... Or just swap the two encoder phases physically, or define the pins swapped. – jose can u c Mar 29 '18 at 0:50

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.