Motor speed measurement

will this code be able to measure the speed of the motor correctly. I used it and the rpm output was actually higher than that it should be , like 6th times higher. The motor i am using is CH-N20-3.

const int encoderPinA = 2;
//const int encoderPinB = 3;
volatile unsigned long pulseDuration;
const int interval = 1000;
volatile unsigned long lastTime = 0;
const int PPR = 44;
void setup() { 
  Serial.begin(9600);
  pinMode(encoderPinA,INPUT);
  //pinMode(encoderPinB,INPUT);
  attachInterrupt(digitalPinToInterrupt(encoderPinA), encoderISR, FALLING);
  lastTime = micros();
} 
void loop(){
  //lastTime = micros();
  // noInterrupts();
   //lastTime = 0;
   Serial.print("pulseduration: ");
    Serial.print(pulseDuration);
    Serial.println("microseconds");
    noInterrupts();
unsigned long duration = pulseDuration ;
interrupts();
float speed = (60.0*1000000.0)/(duration*(float)PPR);
//interrupts();
Serial.print("speed:");
Serial.print(speed);
Serial.println("RPM");
//interrupts();
//delay(interval);
}
void encoderISR(){
  unsigned long currentTime = micros();
  pulseDuration = currentTime - lastTime;
  lastTime = currentTime;
}

@jishnu777

You need to know the gear ratio and the PPR of the encoder.
Seems most encoders for the motor are 12PPR
Do you know the gear ratio?
I think it may be 20. so the PPR in your code should be 20*12 = 240 not 44

Are you using the following Motor (Fig-1)?

image
Figure-1:

Yes this one is what i am using. Can you please provide the speciation

No, I don't know the specification of the motor. I tried to search in online but the datasheet was not matching for the motor name.By the way how do I know the gear ratio from the datasheet, like it has number of column showing rated rpm for gear ratio in the datasheets or is it fix for a motor. Thanks for the help.

It should say gear ratio = 20:1 or 100:1 something like that
The ratio is fixed, you can't change it

I've seen those motors with ratios from 5:1 to 1000:1

How did you come up with the number 44?
How do you know it's actually rotating 6 times slower?
Did you try it with 240 like I suggested?

const int encoderPinA = 2;
//const int encoderPinB = 3;

volatile unsigned long  counter = 0;
const int interval = 1000;
const int PPR = 44;

void setup() {
  Serial.begin(115200);
  pinMode(encoderPinA, INPUT_PULLUP);
}
void loop() {
  static unsigned long duration = 0;
  counter = 0;
  unsigned long lastTime = micros();
  attachInterrupt(digitalPinToInterrupt(encoderPinA), encoderISR, FALLING);
  delay(interval);
  detachInterrupt(digitalPinToInterrupt(encoderPinA));
  unsigned long currentTime = micros();
  unsigned long pulseDuration = (currentTime - lastTime) / counter;
  duration = (duration + pulseDuration) / 2 ;
  unsigned long RPM = 60000000UL / (duration * PPR);
  Serial.print("pulseduration: ");
  Serial.print(duration);
  Serial.println("microseconds");
  Serial.print("speed:");
  Serial.print(RPM);
  Serial.println("RPM\n");
  Serial.flush();
}
void encoderISR() {
  counter++;
}

from chat gpt i got the ppr as 44 , as it is a quadrature encoder. One of my friend said that the rated rpm was 300 or something under 300 but the serial monitor showed 1600 something. I think gear ratio might be the issue as I have totally ignored it . Also tell me do we have to multiply the ppr by 4 for a quadrature encoder? Thanks a lot for your help.

Chat is never right.
No, do not multiply by 4 but you must know the PPR (sometimes called CPR) of the encoder. I think for your motor it is 12
Knowing the rated RPM does not help
So how do you know the motor is rotating 6 time slower?

sorry for the inconvenience and my lack of clarity . It was 6 times higher than the rated rpm. Rated rpm(300 or something I am not clear) was 1/6th of the rpm shown in serial monitor. Thanks a lot for your help.

Thanks i will try this code.

why does this code doesn't work in tinkercad . It works well in arduino ide.

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