rotary encoder issue

Hi everyone.

Recently I am doing a project with using an incremental rotary encoder (400 per revolution, four wires for V, Gnd, A and B) to control a servo motor. What I want is the servo motor rotates exactly the same as the rotary encoder does.

I know how to control the servo motor, and I am now trying to connect the rotary encoder to my arduino board alone and read data from it. However, after trying different codes from the internet, it doesn't work for me. In the serial monitor, the reading just keep increasing or decreasing even the encoder is not turned.

The encoder photo is attached and the link below is one of the code i tried.

Is there any suggestion that I can read the encoder normally and see changes in the serial monitor?

Thanks everyone.

Are you using external pullups (4.7K) on the A/B outputs? Most encoders like this are open collecor output and need pullups. You can try setting the interrupt input pins to INPUT_PULLUP if you don't have any resisitors.