Pololu Robotics & Electronics
Menu
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

RPM of gearmotor with integrated encoder

Hello!

I’m using a gearmotor with 19:1 ratio and this Motor Driver. I want to read out the RPM of the motor. Therefore I connected the yellow cable of the motor, which is the encoder A output, to the arduino. To calculate the RPM I am using the following code:

     //DC-Motor
     #define InA 6
     #define InB 7
     #define PWM 3
     #define EncA 2

      volatile double revolutions = 0;
      double rpm = 0;
      unsigned long lastmillis = 0;

     void setup() 
     {
      Serial.begin(9600);
      pinMode(InA, OUTPUT);
      pinMode(InB, OUTPUT);
      pinMode(PWM, OUTPUT);
      pinMode(EncA, INPUT);
      digitalWrite(EncA, HIGH);
      attachInterrupt(0, count_revolutions, FALLING);  // encoder on PIN 2
     }

     void loop() {
       dcStart(255);
       if (millis() - lastmillis == 1000) { //Uptade every one second
         detachInterrupt(0);    //Disable interrupt when calculating
         rpm = 60 * revolutions / 16.;      //16 interruptions per full rotation
         Serial.print("RPM =\t");
         Serial.println(rpm);  
         revolutions = 0;
         lastmillis = millis();
         attachInterrupt(0, count_revolutions, FALLING);
       }
     }

     void count_revolutions()
     {
       revolutions++;
     }

     void dcStart(int PWM_val){    //PWM_val 0-255
       analogWrite(PWM, PWM_val);
       digitalWrite(InA, LOW);
       digitalWrite(InB, HIGH);
     }

Since the motor can do 500 rpm it should run at almost this speed, when using “dcStart(255)” in my code. But when running my code it gives values, that are about 10500 rpm. Is there any mistake I am doing?

Thanks in advance :slight_smile:
Daniel

Hello, Daniel.

The RPM reading sounds about right. The encoder for that gearmotor is on the motor’s output shaft, instead of on the output shaft of the gearbox. With a 19:1 ratio, the motor’s output shaft will rotate 19 times faster (9500 RPM) than the gearbox’s output shaft (500 RPM). Getting a rate of about 10500 RPM is a little on the high side, but still within the +/-10% range we expect for our metal gearmotors.

-Josh

Hello Joshd!

Thank you for your quick response :slight_smile:

I had a little missunderstanding about the encoder output. I thought it counts the rotations of my gearbox shaft.

Thank you for explaning!

Daniel

Why this ```
//16 interruptions per full rotation? Where does this come from?

Best regards

Hello.

The encoder on the 37D gearmotor provides a resolution of 64 counts per revolution (CPR) of the motor shaft. That count is achieved by counting the rising and falling edges of both encoder channels; however, Schlingek’s code only counts the falling edges of one of the encoder channels. That means he is only getting a quarter of the total possible counts he could be getting, 16. Keep in mind that this is 16 CPR for the motor, not the gearbox output shaft.

- Patrick

Thank you for the quick reply, but how does it looks like when I use this kind of gearmotor?

Link to the micro metal gearmotor with encoder:
https://www.mouser.at/ProductDetail/DFRobot/FIT0485?qs=kE1vTINknaUyCn%2BsFnZ%2BDg==

This encoder provides me a revolution of 420 counts per revolution.

Best regards,

16_mp

Something seems to be not consistent at all, because I used the same code, but the rpm that I am getting as a result seems not credible.

Assumptions:
Setup should be the same compared with my used micro metal gear motor. As you have mentioned the 420 CPR are the counts of the edges of both channels.

Unfortunately, we cannot provide support for using a program we did not write with motors we do not sell.

You might try checking out this Arduino Playground page about rotary encoders for more general information about how you might read incremental encoders.

- Patrick