Pololu 48 CPR encoder with Timer/Counter module

Hello,

I bought two of these motors:

I’m using a Timer/Counter module to count the pulses, and consistently only seem to be getting about 600 pulses per revolution of the output shaft, though the datasheet says 2249.

The motors are supposed to give me 2249 pulses per revolution on the output shaft, but I am only reading about 600 pulses per revolution in my Timer/Counter. I’m not sure if this is an issue with how I set the TC registers up, or if it’s an issue with sampling rate, or something else entirely. Looking for possible pointers or advice from someone who has used the TC modules in this fashion before.

Here’s how I set the registers up:

void TCInit() {

TIMSK4 &= ~(1 << TOIE4);  //disable Timer4 interrupt
TIMSK5 &= ~(1 << TOIE5);  //disable Timer5 interrupt

TCCR4A = 0;        // set entire TCCR4A/B register to 0
TCCR4B = 0;

TCCR5A = 0;        // set entire TCCR5A/B register to 0
TCCR5B = 0;
						  //Set timer4 mode 0
TCCR4A &= ~(1 << WGM40);
TCCR4A &= ~(1 << WGM41);
TCCR4B &= ~(1 << WGM43);
TCCR4B &= ~(1 << WGM42);

//Set timer4 external clock, triggered on falling edge (T4 pin)
TCCR4B |= (1 << CS42);
TCCR4B |= (1 << CS41);
TCCR4B &= ~(1 << CS40);

//Set timer5 mode 0
TCCR5A &= ~(1 << WGM50);
TCCR5A &= ~(1 << WGM51);
TCCR5B &= ~(1 << WGM53);
TCCR5B &= ~(1 << WGM52);

//Set timer5 external clock, triggered on rising edge (T5 pin)
TCCR5B |= (1 << CS52);
TCCR5B |= (1 << CS51);
TCCR5B |= (1 << CS50);

}

Here’s how I read the counters:

void getCount() {
cli();
speed_count_rt = TCNT5;
speed_count_lt = TCNT4;
distance_count_lt += speed_count_lt;
distance_count_rt += speed_count_rt;
TCNT5 = 0;
TCNT4 = 0;
sei();
lastCount_rt = speed_count_rt;
lastCount_lt = speed_count_lt;

RPM_lt = (speed_count_lt) * 60 * ((1000 / LOOPTIME)) / (2249);
RPM_rt = (speed_count_rt) * 60 * ((1000 / LOOPTIME)) / (2249);

}

I’m using an Atmega2560 @ 16 MHz for reference.

I’m unable to use interrupts for this due to other constraints, which is why the TC is a must. Looking for any kind of advice I can get, thanks.

I just did a test putting 6V on these motors and measuring the frequency of the pulse train; It only comes in at 2.5KHz, which 2500 pulses per second / 2249 pulses per revolution * 60 seconds = 66.69 RPM. I don’t think these encoders actually do 2249 ppr, but somewhere closer to 650-750. Any way you can confirm this?

Hi.

I only looked briefly at your code, but it looks like you might only be reading one edge of one encoder output from each motor. If that is the case, you should get only 1/4 of the total possible counts, so only about 560 counts per revolution. To get the full CPR, you should count both the rising and falling edges of both encoder outputs from each motor.

-Claire