Can I put clockwise and counterclockwise on photoelectric rotary encoder?

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 :

batch-upload_e23898d1-5e02-4e91-8d22-ac4f1270e89c

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);

}
void loop()
{
if (millis() - millisBefore > 500) {
millisBefore = millis();

a=8;
hole = (holes-a);
Serial.print("Holes : ");
Serial.println(hole);

b=20;
long = (hole/b);
Serial.print("Long : ");
Serial.println(long);

}

delay(200);
}

void count() {
holes++;
}

It is considered good form when code, properly formatted is placed in code tags.

How Rotary Encoder Works and How To Use It with Arduino - How To Mechatronics

RotaryEncoder - Arduino Reference

1 Like

It's also considered Bad Form to post topics that are not tutorials in the tutorial section.

Please use the flag icon to attract the attention of a moderator, and ask them to move it for you.

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.

Your post was MOVED to its current location as it is more suitable.

Could you also take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.

It will help you get the best out of the forum in the future.

1 Like

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.

1 Like

I noticed that too.

I figured from the 4 wires a phase shifted wave would be produced by 2 electric eyes in the sensor for direction.

Or it's just 2 wires for the emitter and 2 wires for the detector ...

EDIT: The disc shows 1 "ring" of slots. I think you need a second "ring" of slots slightly out of phase with the first to determine direction.

1 Like

Hi,

2 wires for emitter and 2 wires for detector, possibly only one set of sensor.

@elizzz123
Welcome to the forum.

Can you please post a link to data/specs and/or where you purchased your encoder assembly?

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

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.

No I would say two wires for the power and two for the optical signals.

There is no need to activate the emitters because once power they are on all the time.

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.

Hi,

Yes could be.... :+1:

We will have to wait as the world turns, so do the...etc etc etc.

Tom... :smiley: :+1: :coffee: :australia:

"two quadrature signal output"

So you can detect the direction of the rotation.

thank u for ur notice

thank you, i'm sorry i not notice the topics before i made this question

here

so i must make a code for read the signal output when the encoder turn CW or CCW? and from that signal i convert that to lenght measurement? Thank you

Hi,

Tom.... :smiley: :+1: :coffee: :australia:

1 Like

Thank you, how about make a signal A to measure length for cw and when i turn direction to ccw, i decrease the value with signal B?