digitalPinToInterrupt syntax question

Hi all -
I see two forms of the attachInterrupt syntax
if I use:

attachInterrupt(digitalPinToInterrupt(interruptPin), myISR, RISING);

then my interrupt works. But I also see this form:

attachInterrupt(interruptPin, myISR, RISING);

I can only get the first syntax to work

  • where does this second notation come from?

Take a view here.
Have a nice day and enjoy coing in C++.

That is an interrupt number, so should be the other first parameter you illustrate, the constant intteruotPin is probably not the right interrupt number.

The quoted above is safer and easier, as it takes care of looking up which interrupt number goes with the pin.

Otherwise you need to know and get right that pin 2 is interrupt 0 and pin 3 is interrupt 1.

Or is it the other way ‘round? Or different entirely on a different Arduino board type? :expressionless:

The call to digitalPinToInterrupt takes a pin number and returns for your convenience the number of the interrupt that is on that pin.


And have a good day coding in C++.

And… have a nice day and enjoy coing in C++.


1 Like

Thanks for the replies. Yes I see now:

attachInterrupt(digitalPinToInterrupt(pin), ISR, mode) (recommended)
attachInterrupt(interrupt, ISR, mode) (not recommended)
attachInterrupt(pin, ISR, mode) (Not recommended. Additionally, this syntax only works on Arduino SAMD Boards, Uno WiFi Rev2, Due, and 101.)

I think the keyword is 'not recommended'
1 Like

Its the other way around on the Leonardo/Micro:
Pin 2 = Interrupt 1
Pin 3 = Interrupt 0

That's why using 'digitalPinToInterrupt(pin)' is important.

1 Like