Problems with encoders on pins 19 and 21 Arduino Mega

Hello everyone! Today we faced a new problem related to coders.Encoders 1, 2, 3 and 5 work fine, but encoders 4 and 6 do not work if they are connected to pins A with interrupts, namely to pins 19 and 21. When changing these pins to others (with interruptions), the encoders start working, which indicates that the motors and encoders themselves are in working order.

To check, I tested pin 19 as follows: when connecting a pin to 5 V, its status in the Serial Monitor changes, which indicates its operability. However, if you install INPUT_PULLUP, the pin stops working - although this configuration works fine on other pins. In addition, pin 21 does not respond in any way, which raises even more questions:
Code for checking the operability of pins:

const int buttonPin = 19; 
int buttonState = 0;      

void setup() {
  Serial.begin(115200);
  pinMode(buttonPin, INPUT);  
}

void loop() {
  buttonState = digitalRead(buttonPin);  

  if (buttonState == LOW) {
    Serial.println("Button Pressed");    
  } else {
    Serial.println("Button Released");  
  }

  delay(100);  
}

Code for manual encoder performance check:

const int encoderPinA = 19;  
const int encoderPinB = 24; 

volatile long encoderPos = 0; 

void setup() {
  Serial.begin(115200);
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);

  attachInterrupt(digitalPinToInterrupt(encoderPinA), updateEncoder, CHANGE);
  attachInterrupt(digitalPinToInterrupt(encoderPinB), updateEncoder, CHANGE);
}

void loop() {
  Serial.print("Encoder Position: ");
  Serial.println(encoderPos);
  delay(100);  
}

void updateEncoder() {

  int valueA = digitalRead(encoderPinA);
  int valueB = digitalRead(encoderPinB);

  if (valueA == HIGH) {
    if (valueB == LOW) {
      encoderPos++; 
    } else {
      encoderPos--; 
    }
  } else {
    if (valueB == HIGH) {
      encoderPos++; 
    } else {
      encoderPos--; 
    }
  }
}

My scheme:

Are there any ways to use other pins for encoder A in Arduino Mega? I saw people on the Internet who used Analog pins for the encoder, it won't be exactly for the encoder, but it didn't work in my code
I will be glad to have any advice that will help to understand this situation!

The first code simply reads the pin 19.

The second code needs both pins to have interrupt functions. All input pins don't provide interrupt functions.
Check the Arduino reference and/or the controller technical manual to findout which pins provide the interrupt function.

1 Like

Try using other pins dedicated to interrupts like 2 or 3 if they're available in your setup. Avoid pins 19 and 21 if they are shared with Serial1 or Serial2.

1 Like

I use 6 encoders in the project and pins 2,3,18,19,20,21 are already used by these enoders

1 Like

Yes, I did, for the second pin I chose a regular pin 24

Are you using the MEGA2560 MINI PRO as shown in your schematic?

Yes

Isn't there a special board package for that board that you need to install?

That is the MEGA2560 PRO, not the MEGAPRO MINI 2560
Are the pinouts the same?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.