SparkFun Electronics Commentsurn:uuid:214d0e4e-f1b1-d287-ce26-ac5b4c9f82492019-09-15T04:38:32-06:00SparkFun ElectronicsCustomer #184164 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #184164urn:uuid:cad0686d-eb35-a2fe-d2fc-d0cc45ba4d162017-11-10T15:33:52-07:00<p>Don't forget the <strong>pull-up resistors!</strong><p>On the Arduino, you can use the internal resistors by defining your inputs with <em>INPUT_PULLUP</em>.</p><p>This is my code to use it:</p><pre><code> #define rotaryA 2
#define rotaryB 3
#define LED 13
volatile int stateA = LOW;
volatile int stateB = LOW;
volatile int counter = 0;
volatile int lastCounter = 0;
void setup() {
pinMode (rotaryA, INPUT_PULLUP);
pinMode (rotaryB, INPUT_PULLUP);
pinMode (LED, OUTPUT);
Serial.begin (9600);
attachInterrupt(digitalPinToInterrupt(rotaryA), changeA, CHANGE);
attachInterrupt(digitalPinToInterrupt(rotaryB), changeB, CHANGE);
stateA = digitalRead(rotaryA);
stateB = digitalRead(rotaryB);
}
void loop() {
digitalWrite(LED, stateA);
if (counter != lastCounter) {
Serial.println(counter);
lastCounter = counter;
}
}
void changeA() {
stateA = !stateA;
if (stateA == HIGH) {
if (stateB == LOW ) {
counter++;
if (counter == 200) {
counter = 0;
}
} else {
counter--;
if (counter == -1) {
counter = 199;
}
}
}
void changeB() {
stateB = !stateB;
}
</code></pre></p>
Customer #721850 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #721850urn:uuid:7788dbea-2fe6-4aec-619f-87b9e14d8ab92015-11-20T09:04:58-07:00<p>I&Acirc;&acute;m trying to read the encoder with the Arduino and everything is okay. But when I read an analog signal the arduino only reads the analog input. I connect both GND, but its still not reading. Does anyone know how can I fix this? Thanks</p>
KevCarrico on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)KevCarricourn:uuid:ccaea8d9-affb-3512-a560-f1dc5de2a37e2013-09-13T15:07:52-06:00<p>lol - that looked a lot better when I pasted it!!!</p>
KevCarrico on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)KevCarricourn:uuid:dd4b12d1-1649-9df9-510a-4e0d536e14042013-09-13T15:06:52-06:00<p>as opposed to the example above, here's some code that will get this working right-away in true quadrature fashion.
[model #E6A2-CW3C]<p>sorry if it is wrong to post this here, but it's free to the world, have spark-fun!</p><pre><code>// code start
[put pound sign here!]include "digitalWriteFast.h" // google this and install it
const byte pin_A = 2; // connect white wire here
const byte pin_B = 3; // connect black wire here
int A_set = 0;
int B_set = 0;
long pulses = 0;
void setup()
{
pinMode(pin_A, INPUT);
digitalWrite(pin_A, HIGH); // enables pull-up resistor
pinMode(pin_B, INPUT);
digitalWrite(pin_B, HIGH); // enables pull-up resistor
A_set = digitalRead(pin_A);
B_set = digitalRead(pin_B);
attachInterrupt(0, encoderPinChange_A, CHANGE); // pin 2
attachInterrupt(1, encoderPinChange_B, CHANGE); // pin 3
Serial.begin(115200);
}
void loop()
{
Serial.println(pulses);
}
void encoderPinChange_A()
{
A_set = digitalReadFast2(pin_A) == HIGH;
pulses += (A_set != B_set) ? +1 : -1;
}
void encoderPinChange_B()
{
B_set = digitalReadFast2(pin_B) == HIGH;
pulses += (A_set == B_set) ? +1 : -1;
}
</code></pre></p>
Customer #338967 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #338967urn:uuid:b587e528-c045-beca-5851-f30b969634522012-07-22T17:31:03-06:00<p>I'm having a hard time figuring out how to wire the shield wire. It says "Shield: GND" on the rotary encoder. But it's very confusing because the blue wire is "0V(Common)", which is what I connect the negative terminal of the power source to (I think). And the negative side is usually called "ground" right? So, Why is the shield called ground? And what do I connect that to? I'm planning to use an arduino mega with the encoder.</p>
tyggerjai on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)tyggerjaiurn:uuid:e9088a59-cd0b-b218-6d83-244784672f832012-01-14T05:56:11-07:00<p>Sorry for the delay! I'm in the middle of moving house, but I'm also in the middle of building a mini motion control system, so I'll put up some photos and instructions for both those approaches.</p>
Shannon2 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Shannon2urn:uuid:0d52effe-dd7a-d63b-1d54-32f8db9e81ab2011-12-09T14:26:27-07:00<p>Mine came with a wiring diagram. Also the data-sheet has a wiring diagram.<p>As for the 5th wire. Mine also has a yellow wire, but it is clipped short. No doubt a 5 wire cable is used for all of their encoders and is simply disconnected for this model. Ignore it if you have it and don't worry about it if you do not. Good luck.</p></p>
michaelshiloh on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)michaelshilohurn:uuid:ee4a00b9-4017-2b81-14c2-7aacae8cdb8e2011-12-07T19:42:20-07:00<p>Great little encoder for the price. Here's my experience:<ol>
<li><p>Easy hookup, especially since the wiring is documented on the encoder. Wish other products did this! Ground, +5, Out A and Out B. I wired the outputs to my Arduino interrupts, in the case of my Uno that's D2 and D3.</p></li>
<li><p>Don't forget the pullups between each output and +5! (I did at first.) The output is open collector and so won't go up to +5 otherwise. I used 10K.</p></li>
<li><p>I used the Quadrature encoder library QuadratureEncoder.h pretty much right out of Dr. Rainer Hessmer blog
<a href="http://www.hessmer.org/blog/2010/04/08/building-a-self-balancing-robot-%e2%80%93-motor-driver-and-wheel-encoder/" rel="nofollow">here</a>. Thanks Dr. Hessmer!</p></li>
<li><p>In Arduino 1.0 Wprogram.h has been replaced by Arduino.h</p></li>
<li><p>In my sketch I copied the example usage from the library and added the following:</p>
<p>Serial.println ( encoder.getPosition());
delay (10);</p></li>
<li><p>Don't forget to initialize the serial port in setup()</p></li>
</ol><p>That's it! worked right away.</p></p>
Customer #138252 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #138252urn:uuid:2d849d08-6deb-2e3a-0ac4-0c1b4fba75432011-11-11T20:23:39-07:00<p>Hmm... Why is there a photo of 5 wires? This rotary encoder only comes with 4 wires, there is no yellow wire. I'm new to all this stuff, a wiring diagram would be fantastic</p>
S1 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)S1urn:uuid:a14ad449-1bad-d1f5-7a94-195ec90aae062011-11-06T14:45:38-07:00<p>I played with an implementation of a quadrature encoder on Arduino and <a href="http://www.10dogs.net/quadrature-encoder/" rel="nofollow">documented</a> some of my understandings / learnings. Might help someone understand the basics...</p>
Lex E on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Lex Eurn:uuid:0faa48fc-610f-dbfc-14c1-645b7c64d37d2011-11-04T00:52:42-06:00<p>According to the photos and the datasheet, it's definitely quadrature: two square waves, 90 degrees out of phase. These two signals give direction of rotation, as well as the usual speed.<p>It's a kind of gray code, since only one bit changes at a time, but because there are only two bits it only counts 0, 1, 3, 2, 200 times per revolution.</p><p>0 0</p><p>0 1</p><p>1 1</p><p>1 0</p></p>
Customer #75535 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #75535urn:uuid:8c5d04d4-768c-97cf-626e-ce144cd608942011-10-31T23:29:31-06:00<p>many thanks indeed for the detailed reply. could you direct me to some practical examples online?</p>
ironyeti on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)ironyetiurn:uuid:d2041249-a742-fb69-59d3-e9e93af1b6132011-10-31T16:09:45-06:00<p>Index tic would be nice.</p>
Dizzy on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Dizzyurn:uuid:90063434-46bd-a543-54dc-2dbf64310d612011-10-31T10:55:07-06:00<p>Outputs gray code? What I could gather from the datasheet is that the encoder outputs quadrature signals, is that the case really?</p>
tyggerjai on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)tyggerjaiurn:uuid:673ddaf9-4ac7-4e81-7b53-bcad50ce466d2011-10-31T05:46:07-06:00<p>There are a few ways, but you don't really mount it on the motor itself, generally. For small motors, one of 2 solutions:<br>
a) On the shaft of the motor, in addition to whatever you're using for drive, you can have a gear/pulley that links to the encoder. Depending on your setup, this can also let you have a large gear/pulley on the motor going to a small gear/pulley on the encoder, so a 200 ppr encoder will have a much finer resolution in terms of motor revolutions. So whatever you've bolted your motor to, bolt your encoder to the same thing and have gears/belts/whatever between them.<br>
b) Attach the encoder to the thing actually being driven. We use encoders like this in theatre a lot for automated scenery, and for a lot of things, you don't actually care about how far the motor has moved, only how far the scenery has moved. While they are almost always the same, if your drive system has any potential for slip (belt drive, friction drives, etc), you want to measure the thing moving, not the motor itself.</p>
Customer #75535 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)Customer #75535urn:uuid:c8803184-1619-c63d-dd2e-ddc8d76c5e8d2011-10-30T16:09:23-06:00<p>how one could mount it on a DC motor?</p>
L1011 on COM-10932 - Rotary Encoder - 200 P/R (Quadrature)L1011urn:uuid:09e5f19a-a64c-0f2a-6ca1-e7527614ea432011-10-29T12:32:05-06:00<p>This would of been very handy for when I converted my L1011 altitude encoder to digital output for flight simulation.. Better yet, a board that could interpret the 400hz ac resolvers that were originally inside the instrument would of been best!</p>