Optical Printer Sensor always increments

Hi!
I have a rotary encoder that I salvaged from a dead printer, and I hooked it up to an Arduino.

I'm using this thing here :

When i read the output from the encoder, position A and B is always 0 no matter which direction I turn the disk to.
As a result, it always increments and never decrements, here's the code :

int P_A=2; int P_B=3; //Inputs
long int position = 0; //Position of wheel
long int lastPosition = 0; //last position of wheel : for speed computing
int sA = 0;
int sB = 0;


void setup() {
  // put your setup code here, to run once:
  attachInterrupt(2, wheel, RISING);  //wheel is arbitrary
}

void loop() {
  // put your main code here, to run repeatedly:
  if(lastPosition != position){
        Serial.println(position);
        Serial.print("A IS : ");
        Serial.println(sA, DEC);
        Serial.print("B IS :   ");
        Serial.println(sB, DEC);
    }
}

void wheel(){
  sA = digitalRead(P_A);
  sB = digitalRead(P_B);
  if (sA == sB)   { //Clockwise
  position++;
  }
  else   { //Counterclockwise
  position--;
  }
}

The output in serial monitor shows that the position always increments, and A and B is always 0, and I'm led to believe that this sensor can read directions based on comparison between A and B.

Is there any way to explain why both A and B is always 0?

based on the picture, the output is :

GND
Channel A
Channel B
+5V (the blue-striped wire)

pcb bawah.PNG

pcb bawah.PNG

If A and B are always zero, I don't see how your wheel function ever gets called if it's looking for a rising edge.

Exactly my thought!

but somehow the position variable in serial monitor keeps adding up whenever the wheel moves, but it only moves one way, and not the other way. is something wrong with the hardware, perhaps?

How are you getting serial output? I don't see a serial.begin anywhere.

YOU'RE RIGHT! I FORGOT!
I'm really sorry.

here's the updated code :

int P_A=0; int P_B=1; //Inputs (RX and TX in Leonardo R3)
long int position = 0; //Position of wheel
long int lastPosition = 0; //last position of wheel : for speed computing
int sA = 0;
int sB = 0;


void setup() {
  // put your setup code here, to run once:
  pinMode(P_A, INPUT);
  pinMode(P_B, INPUT);  
  attachInterrupt(P_A, wheel, RISING);  //wheel is arbitrary
  
  Serial.begin(38400);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.println(position);
        Serial.print("     A IS : ");
        Serial.println(sA, DEC);
        Serial.print("             B IS :   ");
        Serial.println(sB, DEC);
  if(lastPosition != position){
        Serial.println(position);
        Serial.print("     A IS : ");
        Serial.println(sA, DEC);
        Serial.print("              B IS :   ");
        Serial.println(sB, DEC);
    }
}

void wheel(){
  sA = digitalRead(P_A);
  sB = digitalRead(P_B);
  if (sA == sB)   { //Clockwise
  position++;
  }
  else   { //Counterclockwise
  position--;
  }
}

I'm really new to Arduino, although I have a bit of experience in coding C, my apologies.

EDIT : i forgot to add pinMode. tried and it's working, but it's still not detecting the change in direction whenever i counter the rotation of the wheel :frowning:

I think you need to get a better idea what the sensor is doing - do you have access to a 'scope?

If not, perhaps dump the interrupt and just read the the two pins states and print them on a change.

You might put them on pins that mean you can read with PORT commands for speed.

Okay I'm trying it out now.
Thanks for the advice!

by the way, can you please explain how serial plotter works?
If this sensor is quadrature i want to see the pattern in the serial plotter..

I don't see any pinMode(xxx,yyy) for the inputs.

edited the code snippet. i forgot to put pinMode..

edit 2 : after adding pinMode the sensor works normally. Thanks a million for the great input, it was a great help!

sA and sB should be declared volatile as you modify them inside an ISR.

The default state for all pins is INPUT, there is no need to set pins to this mode though it does help with clarity of your code to do this explicitly.