I have an Arduino Mega, and I run out of interrupt pins. I have only pin 18 available, but I need 2 of them. What can I do?
pin | INTx | digitalPinToInterrupt(pin) |
---|---|---|
3 | INT5 | 1 |
2 | INT4 | 0 |
18 | TX1 INT3 | 5 |
19 | RX1 INT2 | 4 |
20 | SDA INT1 | 3 |
21 | SCL INT0 | 2 |
-
1there are pin change interrupts too (not on all ports unlike on smaller AVRs)KIIV– KIIV01/09/2025 17:40:48Commented Jan 9 at 17:40
2 Answers 2
You can double-up on pins if both devices signal in the same direction (HIGH or LOW) or their signals be conditioned to do so. When the interrupt fires, your interrupt routine first polls the devices to determine which one fired and then dispatches to the code for that one. If a race-condition is possible, you should remember the dual-firing (store it in a bool or as two one-bit flags) and dispatch to one after the other.
Update:
What do you mean with 'double-up on pins'?
Connect two devices' "ready" outputs to the same interrupt pin. The interrupt routine will have to determine which device is asking for service and act accordingly.
If you read data sheet of AVR chip, you might come across PCINT
in addition to INT
pins.
INT
refers to the dedicated hardware interrupt pins described in Arduino Reference. The INT pin is linked to a dedicated interrupt vector so you always know what pin caused the interrupt when in the ISR.
PCINT
refers to Pin-Change Interrupt that can be generated by almost(not always though) any of the I/O pins. PCINT has more overhead in determining what pin caused the interrupt as a group of pins(pins on the same GPIO port) share the same PCINT vector so you need to determine which pin caused the interrupt within the ISR before acting on it. Furthermore, PCINT can wake from sleep mode power down on change, while INT can only wake on low level. Depends on your application, PCINT is very good if you want to look at several pin status at once, for example, when using a rotary encoders.
This Article provides a good description and example on using PCINT, there is a library PinChangeInterrupt by NicoHood for using Pin Change Interrupt.
Specifically for ATmega2560, there are 24 PCINT-capable pins.