Rotary encoder troubleshooting

Hello everybody, I am trying to use a rotary encoder with my arduino uno.

Unfortunately I am having many troubles that seems to have no reason to be.

This is the setup:

Arduino with connected a rotary encoder, encoder's pin C to 5V, encoder's pin A and B respectively to PIN 2 and 3 (interrupt). I have added two pulldown resistors (10K?) that connects pins 2 and 3 to ground.

I have just written this simple sketch:

int encoderA = 2;
int encoderB = 3;

void setup() {
  pinMode(encoderA, INPUT);
  pinMode(encoderB, INPUT);
  attachInterrupt(0, doEncoderA, RISING);
  attachInterrupt(1, doEncoderB, RISING);
  Serial.begin(9600);
  Serial.println("Test:");
}

void loop() {
 
}

void doEncoderB(){
  int A = digitalRead(encoderA);
  detachInterrupt(1);
  if(A == LOW){
    Serial.println(0);
  }else{
    Serial.println(1);
  }
  attachInterrupt(1,doEncoderBFall,FALLING);
}
void doEncoderBFall(){
  int A = digitalRead(encoderA);
  detachInterrupt(1);
  if(A == HIGH){
    Serial.println(2);
  }else{
    Serial.println(3);
  }
  attachInterrupt(1,doEncoderB,RISING);
}
void doEncoderA(){
}

What this code should do is to print a pair of numbers, 02 if going clockwise and 13 if going counterclockwise.
And it does, but not always, sometimes I get a 02 03 02 03 12 when turning clockwise and something like 13 12 03 when turning counterclockwise.

This is driving me crazy because it don't make sense at all!!

I was shouting to my arduino and my code when I decided to hook up my oscilloscope to the two signals and what I get is the image attached, each and every single pulse is perfectly identical to the one in the picture, no strange behaviour. So I came up that the problem is in the code.

But it is extremely simple, it's almost bulletproof, what am I doing wrong??

pic_6_2.bmp (1.1 MB)

The scope trace and your description of the circuit don't make sense to me. The usual connection with 3-pin encoders is to have C grounded, with pullups on A and B and normally capacitors would be used to prevent contact bounce from interfering with the pulse detection.

Your code is far from clean and simple -- for one, you should never print from within an interrupt.

Here are two links that might help.

The actual scheme is like in the picture attached, now with this sketch it seems to be working, but with only one interrupt.

int encoderA = 2;
int encoderB = 3;
int encoderPos = 0;
void setup() {
  pinMode(encoderA, INPUT);
  pinMode(encoderB, INPUT);
  pinMode(13, OUTPUT);
  pinMode(11, OUTPUT);
  attachInterrupt(1, doEncoderB, RISING);
  Serial.begin(9600);
  Serial.println("Test:");
}

void loop() {
}

void doEncoderB(){
  int A = digitalRead(encoderA);
  detachInterrupt(1);
   detachInterrupt(0);
   encoderPos = 0;
  if(A == LOW){
    digitalWrite(13,HIGH);
  }else{
    analogWrite(11,255);
  }
    //Serial.print("0: ");
    //Serial.println(A);
  attachInterrupt(1,doEncoderBFall,FALLING);
}
void doEncoderBFall(){
  detachInterrupt(1);
  digitalWrite(13,LOW);
  analogWrite(11,0);
    //Serial.println(2);
    encoderPos = 2;
  attachInterrupt(1, doEncoderB, RISING);
  
}

It blik a led when turning CW and another when turning CCW.

Is there any chance to detect the speed to change the ratio of the increment?

Schermata 2014-08-13 alle 18.44.35.png

no one?

Sure, capture micros() and do a little math,
elapsedTime = laterTime - earlierTime; // time elements are unsigned long
will tell you the between events.

kevinpirola:
no one?

Sure. Bet you are using an inexpensive mechanical encoder switch rather then a more expensive optical or magnetic encoder? If so the dreaded "switch contact bounce" is causing your problem of random intermittent multiple steps.

retrolefty:

kevinpirola:
no one?

Sure. Bet you are using an inexpensive mechanical encoder switch rather then a more expensive optical or magnetic encoder? If so the dreaded "switch contact bounce" is causing your problem of random intermittent multiple steps.

I'm using indeed a mechanical one, but it seems that with a capacitor 100nF I can solve the problem. Maybe I will focus on magnetic or optical for the final project if they are better, I didn't know they existed.

Thanks