How to get number of counts/rev of Gear Motor with Encoder

Hello everyone,
I’ve just bought a Gear Motor with Encoder (pololu.com/product/1447) and integrated to my APM but don’t know how to get number of count per revolution. I did get the output value from A pin and B pin but can’t understand the formula that described in datasheet. Anyone can help?

Thanks in advance.

Hello.

I am not sure what datasheet you are referring to, but we give the counts per revolution on that gearmotor’s product page. As stated at the top of the product page, you should get 8400 counts per revolution of the gearbox’s output shaft. You can also find more information about the encoder signal in the “Using the Encoder” section of the product page. If there is something specific you don’t understand, please let me know and I will try to clarify.

- Grant

Hello Grant,
Thank you for your response. I think I made a stupid question while I weren’t understanding how to connect Encoder to right pins. As the image below I connected the YELLOW to pin 2 and WHITE to pin 3 (on ArduPilot board) then play with this code but it doesn’t work:

http://s30.postimg.org/sojsids0g/photo_6.jpg

int pulses, A_SIG=0, B_SIG=1;

void setup(){
  attachInterrupt(0, A_RISE, RISING);
  attachInterrupt(1, B_RISE, RISING);
  Serial.begin(115200);
}//setup


void loop(){
    
}

void A_RISE(){
 detachInterrupt(0);
 A_SIG=1;
 
 if(B_SIG==0)
 pulses++;//moving forward
 if(B_SIG==1)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(0, A_FALL, FALLING);
}

void A_FALL(){
  detachInterrupt(0);
 A_SIG=0;
 
 if(B_SIG==1)
 pulses++;//moving forward
 if(B_SIG==0)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(0, A_RISE, RISING);  
}

void B_RISE(){
 detachInterrupt(1);
 B_SIG=1;
 
 if(A_SIG==1)
 pulses++;//moving forward
 if(A_SIG==0)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(1, B_FALL, FALLING);
}

void B_FALL(){
 detachInterrupt(1);
 B_SIG=0;
 
 if(A_SIG==0)
 pulses++;//moving forward
 if(A_SIG==1)
 pulses--;//moving reverse
 Serial.println(pulses);
 attachInterrupt(1, B_RISE, RISING);
}

Did I do something wrong?

I am not sure exactly what is causing your program to not work, but from briefly looking at your code I noticed two things that would probably be good to change. In general, using commands that take a long time to execute, like serial print statements, inside an interrupt is not good practice, since interrupts are meant to be as short as possible. Also, it seems that your code detaches and attaches the interrupts in order to change them from detecting on one edge to detecting on the other. You should not need to do this. You could use a change interrupt and if statements to determine the direction of the motor instead. There are many examples out there for using encoders with the Arduino, like this example on the Arduino playground. I recommend starting with one of those examples and modifying it to fit your needs.

By the way, I am not sure how much current the power rails on your board are capable of delivering, but if you are going to use it to power the motors, you should make sure it can handle the amount of current the motor draws. You should also be careful about shorting connections, as it looks like your pins do not have housings.

There was an issue with the image you posted. It seems that the site that you hosted the photo on prevented the image from properly displaying on our forums. We have edited your post to include a link to the image instead.

- Grant