I'm doing a project which requires me to use an faulhaber encoder,and the one i have is a 2channel 16lines per revolution. I haven't worked with encoders before and based on what i found the lines per revolution mean how many pulses i get for each revolution and so in my code i used an interrupt and counted the times my channel A changed till it got to 32 counting once for every high to low and once for every low to high for my 16 pulses which then by my belief means that I've had one revolution but when i test the code after around like 1/6 of a spin my counter reaches 32 and I'd appreciate if someone can tell me where I'm going wrong with this .

This is the datasheet of the encoder I'm using IE2_16 .

here is the code :

volatile long int ticks = 0 ;
unsigned long int rev = 0 ;
const int channelA = 2 ;
void setup(){
   attachInterrupt(digitalPinToInterrupt(channelA), numCunts,  CHANGE);
void loop(){

       ticks = 0;


void numCunts(){
  ticks+=1 ;

No, there are several problems with your decoding. First, you are detecting every change. You want to detect only rising or falling edge changes.

Second, the changes are likely noisy, so you need to write your decoder to "debounce" the readings. With a 2-channel encoder like you're using, the usual way to do that is to only respond to rising edge changes on channel A after you've seen a rising edge change on channel B, and visa-versa.

See this link for a description of how they work: http://www.creative-robotics.com/quadrature-intro

I tried my hand at writing my own decoder, but it's harder than you might think to get it right. I'd suggest using somebody else's rotary encoder library.

And a quick google search revealed this library on github: https://github.com/PaulStoffregen/Encoder

(I don't know if it's any good or not. It's the first hit I found.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.