Arduino Mega R3 Interrupt pins

I'm working on a project with an Arduino Mega that uses Wiegand RFID readers, and I need to use hardware interrupts for the D0 and D1 lines of each reader. I know that on the Mega, only pins 2, 3, 18, 19, 20, and 21 are hardware interrupt-capable, which limits the number of readers I can connect.

Is there any way to add more interrupt-capable pins or expand the hardware interrupts for handling more RFID readers?

Thank you

In addition to the "external" interrupts (INT0 to INT7) there are a large number of pin change interrupts, which are hardware, but less flexible in interrupt conditions. See Interrupts - Section 14 of the data sheet.

I moved your topic to an appropriate forum category @onlinegill .

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

There are several good libraries available through the library manager which can simplify using pin change interrupts on the Mega.

I recommend EnableInterrupt.h by GreyGnome and PinChangeInterrupt.h by Nico Hood.

Hi @jremington Thank you for your response. When I connect D0 and D1 pins to 2, 3, 18, 19, 20, and 21, I am able to read a Wiegand 26-bit card. However, if I switch to different pins, it does not work.

I'm using the Wiegand-Protocol-Library-for-Arduino from this repository: GitHub - monkeyboard/Wiegand-Protocol-Library-for-Arduino: Wiegand 26 and Wiegand 34 Protocol Library for Arduino.

Here’s the code I am using:

#include <Wiegand.h>  // Include the Wiegand Protocol Library

// Create WIEGAND instances for each reader
WIEGAND wg1;
WIEGAND wg2;
WIEGAND wg3;
WIEGAND wg4;
WIEGAND wg5;
WIEGAND wg6;

void setup() {
  Serial.begin(9600);  // Start serial communication to output card data

  // Initialize WIEGAND readers with their respective D0 and D1 pins
  wg1.begin(2, 3);     // Reader 1 (D0 on pin 2, D1 on pin 3)
  wg2.begin(12, 13);   // Reader 2 (D0 on pin 12, D1 on pin 13)
  wg3.begin(50, 51);   // Reader 3 (D0 on pin 50, D1 on pin 51)
  wg4.begin(52, 53);   // Reader 4 (D0 on pin 52, D1 on pin 53)
  wg5.begin(14, 15);   // Reader 5 (D0 on pin 14, D1 on pin 15)
  wg6.begin(A8, A9);   // Reader 6 (D0 on pin A8, D1 on pin A9)
}

void loop() {
  // Check if card data is available and output it for each reader
  if (wg1.available()) {
    Serial.print("Reader 1: ");
    Serial.println(wg1.getCode(), DEC);  // Print the card number
  }
  if (wg2.available()) {
    Serial.print("Reader 2: ");
    Serial.println(wg2.getCode(), DEC);
  }
  if (wg3.available()) {
    Serial.print("Reader 3: ");
    Serial.println(wg3.getCode(), DEC);
  }
  if (wg4.available()) {
    Serial.print("Reader 4: ");
    Serial.println(wg4.getCode(), DEC);
  }
  if (wg5.available()) {
    Serial.print("Reader 5: ");
    Serial.println(wg5.getCode(), DEC);
  }
  if (wg6.available()) {
    Serial.print("Reader 6: ");
    Serial.println(wg6.getCode(), DEC);
  }
}

if I switch to different pins, it does not work.

No surprise there. Did you check whether the library supports pin change interrupts?

I am using

And

The serial monitor only display results when using pins 2, 3, 18, 19, 20, and 21, but nothing happens if I use pins like 22, 23, etc.

#include <Wiegand.h>
#include <PinChangeInterrupt.h>  // Include the PinChangeInterrupt Library

// Create Wiegand instances for each reader
WIEGAND wg1;
WIEGAND wg2;
WIEGAND wg3;
WIEGAND wg4;
WIEGAND wg5;
WIEGAND wg6;
WIEGAND wg7;  // Additional Reader
WIEGAND wg8;  // Additional Reader

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

  // Initialize Wiegand readers with external interrupt pins
  wg1.begin(2, 3);    // INT0, INT1
  wg2.begin(18, 19);  // INT3, INT4
  wg3.begin(20, 21);  // INT5, INT6

  // Use PinChangeInterrupt for additional readers on non-external interrupt pins
  wg4.begin(22, 23);  // Using Pin Change Interrupt
  wg5.begin(24, 25);  // Using Pin Change Interrupt
  wg6.begin(26, 27);  // Using Pin Change Interrupt
  wg7.begin(28, 29);  // Extra Reader using Pin Change Interrupt
  wg8.begin(30, 31);  // Extra Reader using Pin Change Interrupt

  // Attach external interrupts for the first three readers
  attachInterrupt(digitalPinToInterrupt(2), ISR_wg1_D0, FALLING);
  attachInterrupt(digitalPinToInterrupt(3), ISR_wg1_D1, FALLING);
  attachInterrupt(digitalPinToInterrupt(18), ISR_wg2_D0, FALLING);
  attachInterrupt(digitalPinToInterrupt(19), ISR_wg2_D1, FALLING);
  attachInterrupt(digitalPinToInterrupt(20), ISR_wg3_D0, FALLING);
  attachInterrupt(digitalPinToInterrupt(21), ISR_wg3_D1, FALLING);

  // Attach Pin Change Interrupts for other readers
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(22), ISR_wg4_D0, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(23), ISR_wg4_D1, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(24), ISR_wg5_D0, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(25), ISR_wg5_D1, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(26), ISR_wg6_D0, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(27), ISR_wg6_D1, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(28), ISR_wg7_D0, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(29), ISR_wg7_D1, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(30), ISR_wg8_D0, FALLING);
  attachPinChangeInterrupt(digitalPinToPinChangeInterrupt(31), ISR_wg8_D1, FALLING);
}

void loop() {
  // Check if card data is available for each reader and output it
  if (wg1.available()) {
    Serial.print("Reader 1: ");
    Serial.println(wg1.getCode(), DEC);
  }
  if (wg2.available()) {
    Serial.print("Reader 2: ");
    Serial.println(wg2.getCode(), DEC);
  }
  if (wg3.available()) {
    Serial.print("Reader 3: ");
    Serial.println(wg3.getCode(), DEC);
  }
  if (wg4.available()) {
    Serial.print("Reader 4: ");
    Serial.println(wg4.getCode(), DEC);
  }
  if (wg5.available()) {
    Serial.print("Reader 5: ");
    Serial.println(wg5.getCode(), DEC);
  }
  if (wg6.available()) {
    Serial.print("Reader 6: ");
    Serial.println(wg6.getCode(), DEC);
  }
  if (wg7.available()) {
    Serial.print("Reader 7: ");
    Serial.println(wg7.getCode(), DEC);
  }
  if (wg8.available()) {
    Serial.print("Reader 8: ");
    Serial.println(wg8.getCode(), DEC);
  }
}

// ISR functions
void ISR_wg1_D0() { wg1.begin(2, 3); }
void ISR_wg1_D1() { wg1.begin(2, 3); }
void ISR_wg2_D0() { wg2.begin(18, 19); }
void ISR_wg2_D1() { wg2.begin(18, 19); }
void ISR_wg3_D0() { wg3.begin(20, 21); }
void ISR_wg3_D1() { wg3.begin(20, 21); }

void ISR_wg4_D0() { wg4.begin(22, 23); }
void ISR_wg4_D1() { wg4.begin(22, 23); }
void ISR_wg5_D0() { wg5.begin(24, 25); }
void ISR_wg5_D1() { wg5.begin(24, 25); }
void ISR_wg6_D0() { wg6.begin(26, 27); }
void ISR_wg6_D1() { wg6.begin(26, 27); }
void ISR_wg7_D0() { wg7.begin(28, 29); }
void ISR_wg7_D1() { wg7.begin(28, 29); }
void ISR_wg8_D0() { wg8.begin(30, 31); }
void ISR_wg8_D1() { wg8.begin(30, 31); }



You are using pins which don't support pin change interrupts. See this list from the "Read Me" section of the library you are using of supported pins for pin change interrupts.

Arduino Mega: 10, 11, 12, 13, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64),
           A11 (65), A12 (66), A13 (67), A14 (68), A15 (69)

https://github.com/NicoHood/PinChangeInterrupt/?tab=readme-ov-file#supported-pins-for-pinchangeinterrupt

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