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

Pololu Forum

RPM of gearmotor with integrated encoder



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:

     #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() 
      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() {
       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");
         revolutions = 0;
         lastmillis = millis();
         attachInterrupt(0, count_revolutions, FALLING);

     void count_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:


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.



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!