# How to detect the direction of rotation with a sensor encoder timing disk (photoelectric speed sensor)?

I am making a DIY gaming steering wheel and am stuck. For measuring the rotation of the wheel, I'm using a sensor encoder timing disk, which is usually found in printers. - https://robu.in/product/photoelectric-speed-sensor-encoder-coded-disc-code-wheel.

A lot of tutorials on youtube use this. But I just cannot seem to understand how to use this sensor.

1. Measure how much the wheel rotated?

2. Know which direction did the wheel rotates in (clockwise, anti-clockwise).

PS: I tried using a rotatory encoder, but it is unable to sense rotation when rotated by a 1000 RPM motor. Would love to understand why.

Thanks!

• From the product description it isn't clear, if the sensor has 1 or 2 phototransistors. If it has only 1, you cannot sense the direction. If it has two, it is simple A/B encoding like any rotary encoder. Can you provide a more specific and detailed datasheet? Dec 27, 2019 at 13:42

It seems to be a A/B coding : if A gets high before B, it's one way (count for exemple), and if B gets high before A, it's other way (decount). So, connect power and ground. Then connect the two wires to input pins :

``````    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
``````

Notice that one pin must be an hardware interrupt pin (Uno 2 an 3 are interrupt pins. Then attach interrupt in the stup :

``````    attachInterrupt(2, wheel, RISING);  //wheel is arbitray
``````

Then create the interruption code :

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

Then, in your loop, you can print position when it changes to read current position :

``````    if(lastPosition != position){
Serial.println(position);
}
``````

i hope it will help... Notice that if you use both interruptions, you get a better resolution, but speed limit is lower.

• I'm having trouble understanding this. Could you please provide a code snippet? Dec 27, 2019 at 14:19