Getting RPM of a 12 V dc motor w/ its quadrature encoder

Hello All,

I am trying to create a program which can detect the RPM of my motor using its encoder. The motor I have is a 12 V dc motor which has a quadrature encoder on the motor shaft. This is the exact description of my motor from amazon:
"This 2.54" × 1.45" × 1.45" gearmotor is a powerful 12V brushed DC motor with a 29:1 metal gearbox and an integrated quadrature encoder that provides a resolution of 64 counts per revolution of the motor shaft, which corresponds to 1856 counts per revolution of the gearbox’s output shaft. These units have a 0.61"-long, 6 mm-diameter D-shaped output shaft."

Now I am completely new to working with DC motors, so I have a few questions and some sort of a plan to calculate rpm.

First question is there a way to calculate the pulses per revolution of my motor? Would it be 64 / 4(I read somewhere that to get ppr for a quadrature motor you divide the cpr by 4)?

Second question would the following method be appropriate to calculate rpm using an arduino uno or am I just way off:
First in order to calculate revolutions per minute would it be possible for me to calculate how many pulses happen every 1000 milliseconds on the arduino? Because if I can then I could divide the number of pulses by the total number of pulses that happen in one revolution(basically the ppr value) and by 1000 milliseconds to get revolutions per second. For example: Lets say my ppr value is 16, and I know in the last 1000 milliseconds 8 pulses have happened. Then could I just do 8/16 which tells me .5 of a revolution has happened and take .5 and divide it by 1000 milliseconds to calculate revolutions per millisecond(convert to rpm after)?

Here's a simple sketch I wrote for a similar motor, doesn't control the motor, only reads the tach, might help get you started.
EDIT: Whoops, old math.

unsigned long start;
const byte encoderPinA = 2;//A pin -> the interrupt pin 0
volatile long pulse;//the number of the pulses
int fin = 250, rpm;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  attachInterrupt(0, readEncoder, FALLING);
  pinMode(encoderPinA,INPUT);
}

void loop() {
  if(millis() - start > fin){
  noInterrupts();  
  rpm = pulse / 4;
  Serial.print(rpm);Serial.print(" \t");
  Serial.println(rpm / 43.8); // gear ratio
  pulse = 0;
  start = millis();
  interrupts();
  }
}
 
void readEncoder()
{
  ++pulse;

}

There are several ways of connecting a quadrature encoder, which will give 4x, 2x or 1x the "counts per revolution". And "counts per revolution" can be defined in different ways.

It is not clear from your description what you have, so connect up an Arduino to the encoder using one of the many on line tutorials, and rotate the motor shaft by hand, to see what you get.

Once you have figured that out, count pulses for a given amount of time and convert that to RPM using simple arithmetic.

edgemoron:
Here's a simple sketch I wrote for a similar motor, doesn't control the motor, only reads the tach, might help get you started.
EDIT: Whoops, old math.

unsigned long start;

const byte encoderPinA = 2;//A pin -> the interrupt pin 0
volatile long pulse;//the number of the pulses
int fin = 250, rpm;

void setup() {
  // initialize serial:
  Serial.begin(9600);
  attachInterrupt(0, readEncoder, FALLING);
  pinMode(encoderPinA,INPUT);
}

void loop() {
  if(millis() - start > fin){
  noInterrupts(); 
  rpm = pulse / 4;
  Serial.print(rpm);Serial.print(" \t");
  Serial.println(rpm / 43.8); // gear ratio
  pulse = 0;
  start = millis();
  interrupts();
  }
}

void readEncoder()
{
  ++pulse;

}

I was just wondering why do you divide the pulses by 4 and the RPM by 43.8? Sorry i'm new to this!

My encoder is 16 PPR and I'm updating every 1/4 second (250 mS), my gear ratio is 43.8 to 1 so that gives me shaft RPM. Say I were doing 60 RPM or 1 rev per second, I would have 16 * 60 = 960 PPM / 4 updates per second = 240 pulses per update, / 4 = 60 RPM displayed.
That was just a crude test sketch using only one encoder channel (A), did it work with your encoder?

I have tried your code briefly and yes it seems to work. I haven't had a chance to work with it in great detail yet though. And thank you that makes sense! But before I work with the code, I was wondering do you think i'm going about calculating PPR for my motor correctly? I have 64 CPR and I converted that to PPR by dividing by 4.

I was wondering do you think i'm going about calculating PPR for my motor correctly? I have 64 CPR and I converted that to PPR by dividing by 4.

connect up an Arduino to the encoder using one of the many on line tutorials, rotate the motor shaft by hand, to see what you get.

You have a gear ratio of 29:1, set your motor to run at 1740 RPM, and time the shaft RPM against a stopwatch or clock and see how many revs you're getting in 1 minute.
If you get 240 RPM then your PPR is, indeed, 64, change the line:

rpm = pulse / 4;

TO:

rpm = pulse / 16;