SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By marcusbarnet
#145912
I've bought this encoder from sparkfun website: http://www.sparkfun.com/products/10790 but i'm having some problems to make it work since the datasheet is not very clear.

Can you help me to understand how to connect this encoder to my Arduino board, please?
Is there any sample sketch to use in order to verify if the encoder is working?

Thanks a lot, guys!
By marcusbarnet
#145917
I solved it!

I was looking for the second channel wire as specified in the product page on sparkfun but then i've read the comments and I realized that I have to use just the wire for channel A.

Now it works very well. Thanks!
By fll-freak
#145920
Leon is correct in claiming its not an encoder, but you still can use this sensor for speed and distance measurement as long as your motor only rotates in one direction.

The datasheet is indeed pretty terrible.

You will want to connect the leads to power and ground and the signal lead to an interrupt pin (2 or 3) on your Arduino. Next you need to connect the interrupt with a routine that will get called each time the interrupt sees an edge (in setup).
Code: Select all
  /* Attach interrupt on pin 2 (irq0) */
  attachInterrupt(0, encoderIsr, RISING);
Now write the interrupt service routine:
Code: Select all

unsigned long encoderCnt;

void encoderIsr(void) {
  encoderCnt++;
}
Next use the count inside your loop function:
Code: Select all
  unsigned long cnt;

  /* Get current encoder count atomically */
  noInterrupts();
  cnt = encoderCnt;
  interrupts();
Disabling interrupts around the transfer to cnt is to prevent an interrupt that would modify encoderCnt while you are copying the bytes. It could be possible to get very strange results when one byte rolls over to the next. This does introduce the possibility of losing an update by locking out the interrupt. I would not call this section of code at a very high rate, but only when you need to know the current encoder count.