Hi,
I'm not sure to are writing in the correct section but I try to describe my problem.
I bougth a CHR-775 motor this link to the shop because I haven't found a datasheet of that product: https://www.banggood.com/it/Chihai-CHR-775S-Magnetic-Holzer-Encoder-DC-Motor-24_0V-8000RPM-12_0V-4000RPM-Robot-Driving-Motor-p-1390076.html?rmmds=myorder&cur_warehouse=CN&ID=49554
I've a 24V power supply for the motor and a 5V power supply for the encoder, with 5V of Arduino doesn't give me a feedback from movement of the rotary encoder.
I also found a code in this link:
https://www.electroniclinic.com/arduino-dc-motor-speed-control-with-encoder-arduino-dc-motor-encoder/
for the motor with L298N to use that motor with encoder but It doesn't move and I don't know why.
The code:
[code]
[code]
#define ENCA 2
#define ENCB 3
#define PWM 5
#define IN2 6
#define IN1 7
int pos = 0;
long prevT = 0;
float eprev = 0;
float eintegral = 0;
void setup() {
Serial.begin(9600);
pinMode(ENCA, INPUT);
pinMode(ENCB, INPUT);
attachInterrupt(digitalPinToInterrupt(ENCA), readEncoder, RISING);
Serial.println("target pos");
}
void loop() {
// set target position
int target = 1200;
//target = 250*sin(prevT/1e6);
// PID constants
float kp = 1;
float kd = 0.025;
float ki = 0.0;
// time difference
long currT = micros();
float deltaT = ((float) (currT - prevT)) / ( 1.0e6 );
prevT = currT;
// error
int e = pos - target;
// derivative
float dedt = (e - eprev) / (deltaT);
// integral
eintegral = eintegral + e * deltaT;
// control signal
float u = kp * e + kd * dedt + ki * eintegral;
// motor power
float pwr = fabs(u);
if ( pwr > 255 ) {
pwr = 255;
}
// motor direction
int dir = 1;
if (u < 0) {
dir = -1;
}
// signal the motor
setMotor(dir, pwr, PWM, IN1, IN2);
// store previous error
eprev = e;
Serial.print(target);
Serial.print(" ");
Serial.print(pos);
Serial.println();
}
void setMotor(int dir, int pwmVal, int pwm, int in1, int in2) {
analogWrite(pwm, pwmVal);
if (dir == 1) {
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
}
else if (dir == -1) {
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
}
else {
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
}
}
void readEncoder() {
int b = digitalRead(ENCB);
if (b > 0) {
pos++;
}
else {
pos--;
}
}
[/code]
With that code It have to move right and left but It stay to initial position. All pin are regular connected and power supply work. I've also tested the motor directly connected to PS and It work.
Anyone can help me? Thank you so much

