Go Down

Topic: Encoder EMS22Q51 trouble (Read 80 times) previous topic - next topic

hi-tone

Dear Arduino friends,

I am having a serious trouble understanding my new encoder usage. I recently received a non-contacting incremental encoder EMS22Q51-D28-LW4. Datasheet:
https://www.bourns.com/pdfs/EMS22Q.pdf

Setup:
EMS22Q51 - Arduino Uno R3
Pin 1 -> Pin 2
Pin 2 -> Pin 3
Pin 3 -> GND
Pin 4 -> GND
Pin 5 -> 5V
Pin 6 -> GND

So to my understanding neither CS or Index is really nessesary to read a stable output from Channel A and B.

I would like just to make a simple incremental increase, however when i use my code I get random numbers even though i am not touching the encoder at all. If I turn the encoder, it has no effect whats so ever either.
I must be doing something totally wrong either programming wise or hardware wise.
Can someone please help.? It should be very basic stuff and this is troubling my mind enormous.


Code: [Select]

#define pinA 2
#define pinB 3

int level = 0;
int state = 0;
int LastState;

String bits[] = {"00","01","10","11"};
int bump[] = {0,0,-1,1};


void setup() { 
  pinMode (pinA, INPUT);
  pinMode (pinB, INPUT);
 
  //digitalWrite(pinA, HIGH);
  //digitalWrite(pinB, HIGH);

  attachInterrupt(0,knob, RISING);
 
  Serial.begin (9600);
  Serial.println("Encoder ready:");
  Serial.print("Level: ");
  Serial.println(level);
 
  // Reads the initial state of the outputA
  LastState = digitalRead(pinA);
}
void loop() {

}

void knob(){

  state = 0;
  state = state + digitalRead(pinA);
  state <<= 1;
  state = state + digitalRead(pinB);

  level = level + bump[state];

  Serial.print(bits[state] + "    ");
  Serial.print(bump[state], DEC);
  Serial.print("    ");
  Serial.println(level);
}

cattledog

#1
Feb 26, 2017, 09:01 pm Last Edit: Feb 26, 2017, 09:02 pm by cattledog
The problem is not in your code. When I run it with a different encoder I have set up, I get this output which is expected given your algorithm.
Code: [Select]
Encoder ready:
Level: 0
11    1    1
11    1    2
11    1    3
11    1    4
10    -1    3
10    -1    2
10    -1    1
10    -1    0


The problem must be in your hardware setup. The data sheet indicates push/pull out put instead of open collector so I don't think there is a need for external pullups. Are you using these?

Quote
So to my understanding neither CS or Index is really nessesary to read a stable output from Channel A and B
That should be correct, but I'm not sure about the grounding of the index pin, as there's nothing in the data sheet about its output. Try taking it to an arduino pin set for INPUT_PULLUP.

Go Up