Arduino and rotary encoders

Hey guys, I’m trying to figure how to manage rotary encoders.
I read they can only be hooked to a pwm pin, I have a Mega 2560 that supposedly has 14 or 15 of those pins, that would be 7 encoders, and I need twice that amount.
What would be my choices besides a second Arduino?

PWM pin? How's that work? Encoders are usually connected to input pins, using interrupts (external or PCINT).

Hey thanks, I guess I misunderstood it.

So can I use regular pins just attaching interrupts via code?

BTW, can encoders pins be demultiplexed?

I think that'd be tricky, especially if you wanted to turn 2 at the same time. Maybe if you used LS7184 chips, then have a dedicated clock line for each encoder, and mux the direction line as the interrupt responded to the clock line.

Thank you, I need to ask again about the pins can be used for this purpose.
Tried some random digital and it miss most encoder changes, I guess it’s limited to those special pins?

I think any pin that supports PCINT can be used.

A lot depends on how fast the encoder is turning and how many pulses it produces per revolution. You haven't told us anything about the specific encoder you are using or how you are using it.


Sorry, I’m using this code I found

//From bildr article:

//these pins can not be changed 2/3 are special pins
int encoderPin1 = 2;
int encoderPin2 = 3;

volatile int lastEncoded = 0;
volatile long encoderValue = 0;

long lastencoderValue = 0;

int lastMSB = 0;
int lastLSB = 0;

void setup() {
  Serial.begin (9600);

  pinMode(encoderPin1, INPUT); 
  pinMode(encoderPin2, INPUT);

  digitalWrite(encoderPin1, HIGH); //turn pullup resistor on
  digitalWrite(encoderPin2, HIGH); //turn pullup resistor on

  //call updateEncoder() when any high/low changed seen
  //on interrupt 0 (pin 2), or interrupt 1 (pin 3) 
  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);


void loop(){ 
  //Do stuff here

  delay(1000); //just here to slow down the output, and show it will work  even during a delay

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) encoderValue ++;
  if(sum == 0b1110 || sum == 0b0111 || sum == 0b0001 || sum == 0b1000) encoderValue --;

  lastEncoded = encoded; //store this value for next time

And this is the encoder

With those pins works fine but with other doesn’t.
I’m planning to use 12 encoders so I would need 24 pins that can handle them.

  attachInterrupt(0, updateEncoder, CHANGE); 
  attachInterrupt(1, updateEncoder, CHANGE);

Those are dedicated external hardware interrupts.
PCINTs are different.
My book discusses them, and Nick’s page here also discusses them
and you can find more discussion here