Pages: [1]   Go Down
Author Topic: trouble reading rotary encoder  (Read 885 times)
0 Members and 1 Guest are viewing this topic.
Tilburg, Noord Brabant
Offline Offline
Jr. Member
**
Karma: 0
Posts: 53
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I have received 2 318-ENC130175F-12PS rotary encoders yesterday and have been playing with them all day today, or at least tried to play with them.

I'm using this code, from the Arduino site, and nothing happens.

Code:
#define encoder0PinA 2
#define encoder0PinB 3

volatile unsigned int encoder0Pos = 0;

void setup() {
  pinMode(encoder0PinA, INPUT);
  pinMode(encoder0PinB, INPUT);
  attachInterrupt(0, doEncoderA, CHANGE);  // encoder pin on interrupt 0 (pin 2)
  attachInterrupt(1, doEncoderB, CHANGE);  // encoder pin on interrupt 1 (pin 3)
  Serial.begin (9600);
  Serial.println ("start reading encoder");
  Serial.println (encoder0Pos, DEC);
}

void loop(){ //Do stuff here
}

void doEncoderA(){
  if (digitalRead(encoder0PinA) == HIGH) {   // look for a low-to-high on channel A
    if (digitalRead(encoder0PinB) == LOW) {  // check channel B to see which way encoder is turning
      encoder0Pos = encoder0Pos + 1;         // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }
  else                                       // must be a high-to-low edge on channel A                                       
  {
    if (digitalRead(encoder0PinB) == HIGH) { // check channel B to see which way encoder is turning 
      encoder0Pos = encoder0Pos + 1;         // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }
  Serial.println (encoder0Pos, DEC);         // use for debugging - remember to comment out
}

void doEncoderB(){
  if (digitalRead(encoder0PinB) == HIGH) {   // look for a low-to-high on channel B
    if (digitalRead(encoder0PinA) == HIGH) { // check channel A to see which way encoder is turning
      encoder0Pos = encoder0Pos + 1;         // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;         // CCW
    }
  }
  else {                                      // Look for a high-to-low on channel B
    if (digitalRead(encoder0PinA) == LOW) {   // check channel B to see which way encoder is turning 
      encoder0Pos = encoder0Pos + 1;          // CW
    }
    else {
      encoder0Pos = encoder0Pos - 1;          // CCW
    }
  }
  Serial.println (encoder0Pos, DEC);         // use for debugging - remember to comment out
}

Should I use resistors between the encoder and the pins?
Are the pins analog or digital pins?

There are no images showing how to connect stuff in the playground.

as I rotate the encoder and the serial monitor is started nothing happens.

Did I break my Arduino not using resistors and shorting the pins to 5V? (I have tested all used pins as inputs and they still seem to work fine)

Logged

to baldly code what no man has coded before

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 362
Posts: 17307
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


That encoder uses mechanical switch contacts just like simple switches. So the same rules apply about needing pull-up or pull-down resistors for the input pins used, as simple switches require.

Lefty
Logged

Italy
Offline Offline
Full Member
***
Karma: 3
Posts: 142
jayduino rox
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The arduino will not die using 5V or 0V on the pins. For the input pins you should activate the internal pull-ups:
Code:
  digitalWrite(encoder0PinA, HIGH); // activates PULL-UP resistor
  digitalWrite(encoder0PinB, HIGH);
and obviously the pins need to connect to ground when activated.

That nothing happens is very possibly connected to the fact that you use the Serial library in an interrupt (that will block your CPU AFAIK).
Try first a prove of concept in the loop, once working switch to interrupts (nested maybe) if you don't like the super-loop structure.

I guess you also need to digitally debounce the pin readings (or use capacitors).
Logged

Juergen

0
Offline Offline
Newbie
*
Karma: 1
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That nothing happens is very possibly connected to the fact that you use the Serial library...

That's not strictly true. Having a call to the serial library in an interrupt may mean that encoder pulses are lost (not seen/not counted), and even this will only happen if a new pulse arrives before the serial stuff has finished, but that's a long way from implying it causes nothing to happen.
Logged

Pages: [1]   Go Up
Jump to: