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

9.7:1 Gearmotor getting wrong counts per gearbox revolution


#1

I am using your 9.7:1 Metal Gearmotor 25Dx48L mm HP with 48 CPR Encoder (item #2271) with an Arduino Mega. I have it set up to use only encoder A, on pin 18 (interrupt 5). The interrupt is triggered on CHANGE. Given that the gear ratio is 9.68:1, I was expecting that 232.32 counts would equal one gearbox revolution. But, in fact, I seem to get roughly 260 counts per gearbox revolution. Any idea what is going on? Thanks.

Here is my code:

[code]/*
Rotary Encoder Interrupt sketch
*/

const int encoderPinA = 18;
const int encoderStepsPerRevolution = 260;
int pos, oldPos;
volatile int encoderPos = 0, revolutions = 0; // variables changed within interrupts are volatile

void setup()
{
pinMode(encoderPinA, INPUT);
Serial.begin(9600);
Serial.println(“Start”);
attachInterrupt(5, doEncoder, CHANGE);
}

void loop()
{
pos = encoderPos;
if (pos != oldPos)
{
float angle = (pos % encoderStepsPerRevolution) * (360 / (float)encoderStepsPerRevolution);
Serial.print(pos, DEC);
Serial.print ("\t");
Serial.print(revolutions);
Serial.print ("\t");
Serial.println (angle);
oldPos = pos;
delay(250);
}
}

void doEncoder()
{
encoderPos++; // count up if both encoder pins are the same
if (encoderPos % encoderStepsPerRevolution == 0) {
revolutions++;
}
}

[/code]


#2

Hello.

How are you determining when exactly one revolution occurs? There could be some error caused by trying to do it visually. Also, since there is no hysteresis on the encoder signals, and you are only monitoring one signal, a stray backwards tick or extra transition could be getting mistakenly added to the counts.

-Brandon


#3

Thanks Brandon. The problem was stray backwards ticks (lots of them). I was able to fix it. Lesson learned. Thanks!