Encoder ad DC motor

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

How much current doues the motor use? The 298 might not manage the motor.

I've already used a 775 DC motor with L298N and I think use the same current to go

Not the best of facts. I use a motor similar and it pulls more than 10 Amp when starting. But Okey. If the motor turns it's alive.
You get no response from the decoder.
Make a setup, code and wiring, running only the encoder in order to handle the issue.

Please post schematics, not toy Fritzings.

In the link posted previously there are a Code that show if the encoder work or not.
I post it below:

[code]
#define ENCA 2 // pin2 of the Arduino
#define ENCB 3 // Pin3 of the Arduino
int ENCA_DATA;
int ENCB_DATA;

void setup() {
  Serial.begin(9600); // Activates Serial communication
  pinMode(ENCA, INPUT); // sets pin2 as the input
  pinMode(ENCB, INPUT); // sets pin3 as the input
}

void loop() {
  ENCA_DATA = digitalRead(ENCA);
  // We simply read Pin2 of the Arduino and store the result in variable ENCA_DATA
  ENCB_DATA = digitalRead(ENCB);
  // We simply read Pin3 of the Arduino and store the result in variable b
  Serial.print(ENCA_DATA * 5);
  Serial.print(" ");
  Serial.print(ENCB_DATA * 5);
  Serial.println();
}
[/code]

The code in serial monitor sho 0 5 5 0 and so on, so it's work, is It correct?
But it work only with a 5V external power supply.

Hi,
Motor specs from Banggood.

Can you please post a circuit diagram of your project, including power supplies, use a pen(cil) and paper and show an image of it would be fine.
Please do not use a Fritzy picture

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


I hope this diagram is fine for you

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.