Calculating the RPM of a DC Motor

Hello there, I am working on a project of mine where I need to calculate the RPM of a DC motor as the title of this post says.

I am not sure if I do this correctly but let me first give you the specification of my setup. I am using:
Arduino Mega
Simple Encoders from Ebay https://cdn.shopify.com/s/files/1/0020/8027/6524/products/Par-de-encoder-rotativo-incremental-para-calcular-distancia-y-velocidad-800x800_x700.png?v=1524155292

My Encoders have 20 slots (holes). No I will paste my code which I actually found somewhere in the internet and modified.

int val;                                                              //calibration value
long last=0;                                                          //used in timing - holds thelast milis count
int prevLEncValue;                                                         //used in calibration
int lEncValue;                                                            //used in calibration
int counter=0;                                                        //used to count light/dark iterations
 
int sens=75;                                                          //calibration value
             
             
int slots=20;                                                         //# slots in disc
 
int milisecs=500;                                                     //time of sample

// some code here...

void loop() {
  moveForward(95);
  //manualControl();
  //autonomousControl();
  
  lEncValue = digitalRead(32);  // Read the value of the left encoder
    
  if(prevLEncValue != lEncValue) {
    counter++;
  }
  prevLEncValue = lEncValue;    // Update the previous left encoder value
   
  if(millis()-last>=milisecs) {    // has time passed?

   double rps=((double)counter/slots)/2.0*1000.0/milisecs;
   double rpm=((double)counter/slots)/2.0*60000.0/(milisecs);
  
   counter=0;            //reset counter
   last = millis();      //remember this moment
   
   Serial.println(rpm);
  }
   
  delay(10);
}

Notice that i pasted only the section this post is about, because my full code is about 700LOC. Thanks for any help... :slight_smile: MY question is actually if this is correct or my approach is wrong?

Does your approach work? That is the real question to ask!

Do you actually have a motor and the "simple encoder" connected and working? If not, you are just wasting your time and ours as well.

Paul

It seems that the motor is spinning and the print does something. I am not sure if this is the correct value though. This is why i am asking for a validation.

xmarkx:
It seems that the motor is spinning and the print does something. I am not sure if this is the correct value though. This is why i am asking for a validation.

I can't be sure, either, because I can't see the number. What number do you get when you turn the motor by hand?

Paul

This link has code that I extracted from a program to control the speed of a small DC motor. My project produces one pulse per revolution so that would need to be modified for your system. One way would be to record the value of micros every time your pulse count gets to 12 - signifying a revolution

...R

Paul_KD7HB:
I can't be sure, either, because I can't see the number. What number do you get when you turn the motor by hand?

Paul

Arround 30.
Powered by USB cable of my laptop arround 50-60 at 180/255 speed.

Is this normal? :confused:

This is not good: delay(10);

as you are missing counts while waiting.

Paul