I'm starting simple with the Stoffregen Encoder library and the basic example.
/* Encoder Library - Basic Example
* http://www.pjrc.com/teensy/td_libs_Encoder.html
*
* This example code is in the public domain.
*/
#include <Encoder.h>
#define ENCODER_USE_INTERRUPTS
// Change these two numbers to the pins connected to your encoder.
// Best Performance: both pins have interrupt capability
// Good Performance: only the first pin has interrupt capability
// Low Performance: neither pin has interrupt capability
Encoder myEnc(2,3);
// avoid using pins with LEDs attached
void setup() {
Serial.begin(57600);
Serial.println("Basic Encoder Test:");
}
long oldPosition = -999;
void loop() {
long newPosition = myEnc.read();
if (newPosition != oldPosition) {
oldPosition = newPosition;
Serial.println(newPosition);
}
}
The output increments by 2 in both directions. Yes, I mean what I said. Increments by 2 in both directions. Turning CW, increments 2-4-6-8-10, turn CCW and increments 12-14-16-18.
As I understand the library, CCW should decrement the position. I don't know why it increments by two per notch. I have tried a few other libraries with differing issues and some with similar results (one library returns CW motion for both CW and CCW).
If there is a better library available, or what the best library is available, please also let me know.
Not sure how to explain it. It has to do with the state machine to read the encoder. Have a look at the source code. I have to use /4 with my encoders.
Turn one notch right (CW), result increments 2. Turn one notch left (CCW), result increments 2 (now position is 4). Turn one notch right, increments 2 and result is now 6. Turn one notch left, increments 2 and result is now 8.
You got me. I have used that library on several projects and always had to divide by 4 and all of the encoders would increment one way and decrement the other way. However, I use bare encoders, not the ones on breakout boards. Wired like so (caps optional).
Not that it matters much, but no break out board is involved. Just the part purchase from the link in post #1. Other than the notches in the shaft, this is as bare an encoder as I have seen in 25 years of industrial automation.
Some encoders do not make a connection in the detent position, and give one or two pulses when moved from one detent to another. They will not work correctly with that library.
Yours does not seem to have any documentation, so check with a multimeter.
Here is my encoder test code. It does not use interrupts. It counts by 2, incrementing in one rotational direction and decrements in the other. This works with a bare encoder with no caps.
That is weird. Its like the encoder is not quadrature. I can duplicate what you are seeing by wiring the encoder as normal, but also connecting the A and B terminals together with the A still connected to Uno pin 4 and B to Uno pin 5. Like A and B are shorted.
The output from the code in post 15 indicates A and B terminals are independent (ie., not shorted) as b0 and b1 change in the quadrature count (Gray code).