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!
It’s not really a rotary encoder, see the comments.
Can’t I use it to check speed on a wheel which turns only in one direction, so?
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!
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).
/* Attach interrupt on pin 2 (irq0) */
attachInterrupt(0, encoderIsr, RISING);
Now write the interrupt service routine:
unsigned long encoderCnt;
void encoderIsr(void) {
encoderCnt++;
}
Next use the count inside your loop function:
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.
Thanks a lot for your help!
Your suggestion is very good and simple to implement! I’ll proceed in this way