Hi! Can you please help me with my code? Can I put clockwise and counterclockwise for photoelectric rotary encoder? This project i use for length measurement.
Any help appreciated. Thanks.
here's the pict :
and here's my code :
#include <Wire.h>
float hole = 0;
int a;
int b;
float long;
unsigned long millisBefore;
volatile int holes;
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
attachInterrupt(digitalPinToInterrupt(2), count, FALLING);
delay(1000);
Regardless of your code, with only a single sensor, you will not be able to detect the direction the encoder wheel is moving, assuming that was your question. This will need two sensors with a 90 degree phase difference. i.e., position the sensors so when the wheel is stationary, one is aimed at a "hole" and the other is aimed at an opaque part of the wheel.
Then you can use one of the arduino encoder libraries to read the position.
Or you could just use an off the shelf quadrature encoder.
The photograph shows there are four wires from that sensor so it clearly has two photo sensors adjusted to the precise relative position to allow that disc to produce a quadrature signal.
Yea, an emitter detector arrangement may be the case, I do not see any A B dots. I was giving in to the OP's description and convinced myself that the A B dots could be under the standoff.
The OP already has one sense wire connected and working. Change to the other sense wire and see if that also works. If "YES", then two sensors, if "NO" then only one sensor.