I have a DC geared motor rated at 12V and 60 rpm . It has a quadrature encoder with 700 PPR. I connected it directly to a 9V battery (almost new) and wished to measure the rpm of that motor. To find the nature of the pulses generated by the encoder, I connected "A" channel of the encoder to pin 8 of arduino Mega and tried to plot a graph on Matlab by opening Serial port on Matlab. I sampled data every 50 micros and saved data on an array on arduino IDE, then sent it later so as to avoid the buffer delay and dependance on baud rate. From graph, the time difference between 2 pulses was found to be almost 1.6 millis from which, the speed of motor was determined to be about 53 rpm. Then, I wrote a code using interrupts to measure time between 2 pulses on arduino and print it on serial monitor. I found it close to 3 millis, which is about 30 rpm. What should be approx speed of a 12 motor at 9V? I know it is linear with voltage but practically what it should be? Here is my arduino code for finding the time between two pulses on arduino.
#define ENCODEROUTPUT 700
#define threshold 40
volatile long counter = 0; //This variable will increase or decrease depending on the rotation of encoder
int rpm = 0;
volatile long prevCounter=0;
int arr[threshold];
bool flag=1;
bool flag2=0;
int i=0;
long previousMicros=0;
void setup()
{
Serial.begin (9600);
pinMode(2, INPUT_PULLUP); // internal pullup input pin 2
//Setting up interrupt
//A rising pulse from encodenren activated ai0(). AttachInterrupt 0 is DigitalPin nr 2 on moust Arduino.
attachInterrupt(0, ai0, RISING);
prevCounter=counter;
previousMicros = micros();
}
void loop()
{
if ((prevCounter!=counter)&&flag)
{
arr[i] = micros()-previousMicros;
prevCounter=counter;
i++;
if(i==threshold)
{
flag=0;
flag2=1;
}
previousMicros=micros();
if(flag2)
{
for(int j=0;j<threshold;j++)
{
Serial.println(arr[j]);
flag2=0;
}
}
}
}
void ai0() {
// ai0 is activated if DigitalPin nr 2 is going from LOW to HIGH
// Check pin 3 to determine the direction
if(digitalRead(3)==LOW) {
counter++;
}else{
counter--;
}
}