Lot of noise on my encoder

I have never worked with Encoder's before, though I've read and learnt a lot about them. I just both a few from seeedstudio and decided to test them on my breadboard. The datasheet for the ones I am using is here: http://www.seeedstudio.com/depot/datasheet/RotEncoderSpec002.jpg

The instructions basicly state to attach the COM to ground and pull up A and B to 5V with 10K resistors, which I am doing. I am not testing the button input on the encoders.

Th code I am using is essentially a modified version of the one on the playground. Here is the playground code (I am on another computer and don't have access to my exact code):

 /* Read Quadrature Encoder
  * Connect Encoder to Pins encoder0PinA, encoder0PinB, and +5V.
  *
  * Sketch by max wolf / www.meso.net
  * v. 0.1 - very basic functions - mw 20061220
  *
  */  


 int val; 
 int encoder0PinA = 3;
 int encoder0PinB = 4;
 int encoder0Pos = 0;
 int encoder0PinALast = LOW;
 int n = LOW;

 void setup() { 
   pinMode (encoder0PinA,INPUT);
   pinMode (encoder0PinB,INPUT);
   Serial.begin (9600);
 } 

 void loop() { 
   n = digitalRead(encoder0PinA);
   if ((encoder0PinALast == LOW) && (n == HIGH)) {
     if (digitalRead(encoder0PinB) == LOW) {
       encoder0Pos--;
     } else {
       encoder0Pos++;
     }
     Serial.print (encoder0Pos);
     Serial.print ("/");
   } 
   encoder0PinALast = n;
 }

I modified the code to use an interupt on for A on pin 3. I didn't put PIN B on an interrupt as I only want to count for when A goes from low to high (since I only want to count the detents - 20 per revolution).

However, when I rotate in one direction my ouput is all skewed. It should come out like:

0/1/2/3/4/5/6/7

but it comes out like:

0/1/0/1/0/2/3/4/3/2/1/2/3/4

The values do kind of increases in the right direction, but with a lot of back and forth and they don't give me accurate results, i.e, if I rotate one full turn in one direction my counter should be at 20, but my counter is between 8 and 14 depending on how fast I turn the encoder.

It seems really strange. My wiring and code seem to be good, so I am wondering if there's a lot of noise present. Would adding a bypass cap help? The only values I have is a 104nF non-polarised and .33uF and a 10uF polarised.

My guess is you have a mechanical encoder. The one (from china) I tried had contact bounce problems and the mechanical detents did not always seem to be spaced properly to switching transistion points. Anyway you might see if you can add some contact debouncing code, see the Arduino reference and playground for information on that topic.

I solved my problems with finding some affordable optical encoders that do not exibit any contact bounce problems at all.

Good luck Lefty

gosh I didn't even think of the rotary encoder being mechanical, I just assumed it was optical.

After your post, a quick google search brought up this thread: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1205879808/0

The user 'SunboX' made a library I am going to try out. Just need to modify it to print out position everytime the ISR function is called.

You need to use capacitors, and resistors for the debounce. 10K pull up to the rotory output, a 10K from this point to the arduino input. Capacitor 1uF to 10uF from arduino input to ground.

Didn't need to add any bypass caps, the library worked fine.