Pages: [1]   Go Down
Author Topic: Rotary encoder works one way but not the other.  (Read 794 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I thought I had found what I was looking for with a rotary encoder, but it is giving me a lot of headache = smiley-sad-blue.

I spend a few nights already reading many posts about how they work, debouncing them, etc. I have tried out many configurations, with internal pull ups, with different capacitors, with different encoders I have and with the many sketches I have come across. Unfortunately I have not been able to solve my problem. That is, that the encoders works well in one direction (counting up), but not the other direction, when it counts up and down and up and down, etc. Somehow I have a feeling it is not a problem of bouncing, because one direction works very well.

I connected the encoder to pin2 and 3 of the arduino and to ground (middle pin). When I run the sketch (below), I get the following reading:

start
A:0 B:0 pos:102
         Here I turn it in the working direction
A:1 B:1 pos:103
A:0 B:0 pos:104
A:1 B:1 pos:105
A:0 B:0 pos:106
A:1 B:1 pos:107
A:0 B:0 pos:108
A:1 B:1 pos:109
A:0 B:0 pos:110
A:1 B:1 pos:111
A:0 B:0 pos:112
A:1 B:1 pos:113
A:0 B:0 pos:114
A:1 B:1 pos:115
A:0 B:0 pos:116
A:1 B:1 pos:117
A:0 B:0 pos:118
A:1 B:1 pos:119
A:0 B:0 pos:120
A:1 B:1 pos:121

A:0 B:0 pos:122       Untill here it works perfect, but now I turn it the other direction
A:1 B:0 pos:121
A:0 B:0 pos:122
A:1 B:0 pos:121
A:0 B:0 pos:122
A:1 B:0 pos:121
A:0 B:0 pos:122
A:1 B:0 pos:121
A:0 B:0 pos:122
A:0 B:0 pos:123
A:1 B:1 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:1 B:0 pos:124
A:0 B:0 pos:125
A:0 B:0 pos:126
A:1 B:1 pos:127
A:0 B:0 pos:128
A:1 B:0 pos:127
A:0 B:0 pos:128
A:1 B:0 pos:127
A:0 B:0 pos:128
A:1 B:0 pos:127
A:0 B:0 pos:128
A:1 B:0 pos:127
A:0 B:0 pos:128
A:1 B:1 pos:129


When I turned it in the other direction, pin B kept on being read as 0 (with the odd 1). Does anyone has any idea what causes this and how to solve it? Interestingly, when I switch the connection to pin 2 and 3, I get constant 1's for pin B in stead of 0's in the non-accumulation direction.

Thanks, Rutger

p.s. I had a look at optical encoders, but they are a bit too expensive for me, so I hope I can solve it with the ones I have.






#define encoder0PinA  2
#define encoder0PinB  3
volatile unsigned int encoder0Pos = 100;

void setup() {
  pinMode(encoder0PinA, INPUT);
  digitalWrite(encoder0PinA, HIGH); 
  pinMode(encoder0PinB, INPUT);
  digitalWrite(encoder0PinB, HIGH);
  attachInterrupt(0, doEncoder, CHANGE);
  Serial.begin (9600);
  Serial.println("start");     }

void loop(){delay (100);}

void doEncoder() {
  int A =  digitalRead(encoder0PinA);
  int B =  digitalRead(encoder0PinB);
  if (A == B) {encoder0Pos++;  }
  else {encoder0Pos--;}
  Serial.print ("A:");         
  Serial.print (A);   
  Serial.print (" B:");       
  Serial.print (B);         
  Serial.print (" pos:");       
  Serial.println (encoder0Pos);}
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2857
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to attach interrupt 1 as well. You are only processing encoder changes when A changes and are entirely missing changes on B. The expected sequence is:

A:0 B:0
A:1 B:0
A:1 B:1
A:0 B:1
A:0 B:0

and so on.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, speaker, microphone, light sensor, potentiometer, pushbuttons
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

My best guess is that two of the wires are swapped.  That would prevent one switch from grounding its data pin unless the other switch was also.  Double check that the terminal on the encoder you have connected to Ground is, indeed, the common.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Värmland, Sweden
Offline Offline
Sr. Member
****
Karma: 9
Posts: 262
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have to attach interrupt 1 as well. You are only processing encoder changes when A changes and are entirely missing changes on B.

That's not correct. One interrupt will do.
A will always change no matter what direction you turn the knob. The question is if B has already changed when A changes or if it hasn't.

It could still be bounce. Have you tried to even out bounce using capacitors and adding schmitt triggers?

What model is your encoder?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

John, you are right. It sounds stupid, but it appears that I have mistaken the middle pin for ground. The encoder came without any documentation and all configutions I came across online have the ground in the middle. Now it works perfectly, I do see a bounce, but by now I have read enough to solve that.

Again, thanks for the quick reply
Logged

Denver
Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I bet I can sound even more stupid. OK, you have realized that the center pin is not ground. Will you PLEASE tell us where you connect it, if not to ground?

I'm trying to get this to work too, and like you all I have in the way of a hookup diagram is mfr datasheets. Apparently no one in Arduino land thinks its necessary to post a hookup diagram. grr.

[later same day]

OK, updating my own post here. The problem here was me trying to use the "test circuit" from the datasheet. A and B pulled up to 5V with 10K resistors, C to ground.

None of that is needed on the Arduino, which has its own configurable internal pull up resistors. (you set it up with a command like pinMode(5, HIGH) for internal pull up) I directly connected my encoder pins A to 5, B to 6, C to gnd.
C is the middle pin! like this: ACB

(I designated 5 and 6 instead of 2 and 3 in the example code above) It works like it's supposed to. 
« Last Edit: February 08, 2012, 06:37:18 pm by dkathrens77 » Logged

Any answer to any question about 'everything' must necessarily be very long and very detailed. But in short, I can safely say that 'everythi

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The encoder came without any documentation and all configurations I came across online have the ground in the middle.

I had the same problem. Ground is not the middle pin but one on the side.
Logged

NSW Australia
Offline Offline
Faraday Member
**
Karma: 80
Posts: 3243
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  attachInterrupt(0, doEncoder, CHANGE);

No loop!
Logged

Pages: [1]   Go Up
Jump to: