Hello,
I'm reading a Polulu Magnetic Encoder incorrectly. I have this encoder attach to my Arduino/motor.
One full rotation is outputting 3 counts and it should be outputting 20 counts. I tried the following code with a rotary encoder and it was giving me the right cpr. I'm not sure why this magnetic encoder is outputting the wrong value.
Please post a wiring diagram (hand drawn, not a Fritzing mess).
Note: to get the full resolution of the encoder, you have to have an interrupt or poll event for every transition (rising and falling) on both A and B channels.
One full rotation is outputting 3 counts and it should be outputting 20 counts. I tried the following code with a rotary encoder and it was giving me the right cpr. I'm not sure why this magnetic encoder is outputting the wrong value.
As others have indicated, you are not reading all the available quadrature transitions. Your code is reading only one of the four transitions available. With a 20 cpr encoder, you should be seeing 5 counts/rev with your code.
There is a very similar encoder with 12 cpr, and that would give 3 counts with your code. Are you certain that you have the 20 cpr encoder?
Thank you for your reply. I appreciate it a lot. I used this code to read a rotary encoder with 256 cpr. and it seemed to be working right or at least outputting the write output which is 256 in one full rotation. I need to admit I am a little bit lost about reading encoder using interrupts. Does this mean I need to have two ISR one for A and one for B? why? if yes, I need to attach two interrupts as well for pinA and Pin B? why?. How do I properly read all the available quadrature transitions?
I used the example provided from the Arduino playground. I really hope I can get guidance in the code and hoping I will get a fully understanding of reading encoders using interrupts.
Updated. I actually found out that the encoder is 12 CPR and not 20.
Here is my updated code.
1st test
This is the output I am getting in one full rotation:
1
4
9
11
12
2nd test
How is it incrementing from 1 to 4 then 9 to 11 and 12.. dont really get it?
Every time gets me the right output which is 12 but the increment are different. why?
The wiring diagram of my Arduino to the encoder? I am only using GND VDD A AND B. I don't have a wiring diagram. It is pretty straightforward the connection. My apologies.
Encoder : Arduino
GND GND
VDD 5V
A PIN 2
B PIN 3
cattledog:
As others have indicated, you are not reading all the available quadrature transitions. Your code is reading only one of the four transitions available. With a 20 cpr encoder, you should be seeing 5 counts/rev with your code.
There is a very similar encoder with 12 cpr, and that would give 3 counts with your code. Are you certain that you have the 20 cpr encoder?
Yes, I am using 12 CPR encoder. My apologies. I also modified the code but still confused since it seems to get the right output in one rotation but every time the numbers are different.
But you can make one.
Get a pencil.
Get some paper.
Draw the hardware with all important detail.
Take a digital picture of it.
Post it here.
Follow this handy image guide