Description

The digitalPinToInterrupt() function takes a pin as an argument, and returns the same pin if it can be used as an interrupt. For example, digitalPinToInterrupt(4) on an Arduino UNO will not work, as interrupts are only supported on pins 2 and 3.

See attachInterrupt() for a full list of supported interrupt pins on all boards.

Syntax

To verify if a given pin can be used an interrupt use the following function:

digitalPinToInterrupt(pin)

Parameters

The function admits the following parameter:

  • pin: the pin we want to use for an interrupt.

Returns

  • If the pin is available for interrupt, it will return the given pin (e.g. 2).
  • If the pin is not available for interrupt, it will return -1.

Example Code

This example checks if a pin can be used as an interrupt.

int pin = 2;

void setup() {
  Serial.begin(9600);
  int checkPin = digitalPinToInterrupt(pin);

  if (checkPin == -1) {
    Serial.println("Not a valid interrupt pin!");
  } else {
    Serial.println("Valid interrupt pin.");
  }
}

void loop() {
}

See also