Are there board-independent functions to check pin capabilities at compile time? I.e.

  • Can the pin be used as output, as in digitalWrite()?
  • Does the pin support PWM, as in analogWrite()?
  • Does the pin support ADC, as in analogRead()?
  • Does the pin support interrupts, as in attachInterrupt()?

I'm looking for functions/macros like e.g. isDigitalOutput(pin) which would return false for input-only pins such as A6 and A7 on a 328P, but true on a Mega where those pins are input/output.

  • I'm afraid not, Its best to look up documentation or schematics for the board, if you would like to do it by yourself, you can set any pin to any of the states and test whether it works properly, and take some notes
    – Coder9390
    May 5 at 13:20
  • @Coder9390 I wanted to do a check in a library function which requires an analog pin, but I think I'll just drop the idea. No way I'm going to look up every analog pin on every possible board. May 5 at 14:01
  • maybe a check for a definition of A0 would work
    – jsotola
    May 5 at 14:53
  • 3
    Firmata contains a large list of pin definitions for boards. The official version github.com/firmata/ConfigurableFirmata/blob/master/src/utility/… is a bit outdated, but the most common Arduinos are included
    – PMF
    May 5 at 14:56
  • 2

For compatibility with analogWrite() there's digitalPinHasPWM(p) macro which returns 1 when PWM is supported on pin p.

For analogRead() there's analogInputToDigitalPin() which can be used as

#if ((p-PIN_A0 < 0) || (analogInputToDigitalPin(p-PIN_A0) != p))
#error Not an analog pin!

For attachInterrupt() there is digitalPinToInterrupt(p) which returns NOT_AN_INTERRUPT if the pin p doesn't support interrupts.

I still haven't found anything to test compatibility with digitalWrite().

  • you asked about compile time
    – Juraj
    Oct 2 at 10:40
  • 1
    @Juraj Right, A0 is not a macro (PIN_A0 must be used instead). Oct 4 at 9:37
  • but the answer is about runtime
    – Juraj
    Oct 4 at 11:19
  • 1
    @Juraj No, all of the above checks are macros. Oct 4 at 11:50
  • 1
    sorry, I see. after edit it is a macro
    – Juraj
    Oct 4 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.