rotary encoder and arduino?

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
kevinrs
 
Posts: 75
Joined: Sun Jul 31, 2011 5:08 am

rotary encoder and arduino?

Post by kevinrs »

I am trying to use the rotary encoders here for a thermostat type purpose, to set the desired temperature. I am using a basic 2 line LCD display, and figured out that I needed code that uses interrupts for the encoder, otherwise the state changes of the encoder get missed in the loop that updates the display. I found some code that works, but at each detent of the encoder, I get a change of 4. there are 24 detents, unless I'm wrong I shouldn't be getting 4 pulses per detent.

This is the relevant code:

Code: Select all

void updateEncoder(){
  int MSB = digitalRead(encoderPin1); //MSB = most significant bit
  int LSB = digitalRead(encoderPin2); //LSB = least significant bit

  int encoded = (MSB << 1) |LSB; //converting the 2 pin value to single number
  int sum  = (lastEncoded << 2) | encoded; //adding it to the previous encoded value

  if(sum == 0b1101 || sum == 0b0100 || sum == 0b0010 || sum == 0b1011) {
    settemp++;
  }
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) {
    settemp--;
  }
  EEPROM.write (1,settemp); /* write the most recent settemp in eeprom data stoage
    so that if the power is disconnected, your settemp is saved!*/
  lastEncoded = encoded; //store this value for next time
}
Am I headed in the wrong direction completely? is there a better way to do this?

User avatar
kevinrs
 
Posts: 75
Joined: Sun Jul 31, 2011 5:08 am

Re: rotary encoder and arduino?

Post by kevinrs »

After I posted this and went to bed, it occurred to me that it's possible that this is normal behavior? Does a rotary encoder cycle through all 4 states with each "pulse"?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: rotary encoder and arduino?

Post by adafruit_support_bill »

Yes. These encoders do have 4 pulses per detent.

User avatar
kevinrs
 
Posts: 75
Joined: Sun Jul 31, 2011 5:08 am

Re: rotary encoder and arduino?

Post by kevinrs »

That's where the datasheet was confusing me, it shows 24 pulses per rotation. But with 24 detents, and 4 pulses per detent, that's 96 pulses per rotation.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”