# attachInterrupt Pin Inside of Class

**URL:** <https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549>\
**Category:** Programming\
**Created:** [September 17, 2025, 5:31am UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549 "2025-09-17T05:31:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![viktorthecreator](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/viktorthecreator/32/980204_2.png) [@viktorthecreator](https://forum.arduino.cc/u/viktorthecreator)\
**Post date:** [September 17, 2025, 5:31am UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/1 "2025-09-17T05:31:28Z")

</div>

Hello dear community,

I am a newbie in the microcontroller scene. Recently, I have been trying to integrate attachInterrupt into a class which you can find here at my repository [InputDigital/InputDigital.h at main · DngQucVng/InputDigital](https://github.com/DngQucVng/InputDigital/blob/main/InputDigital.h).

I looked all over the internet and found several solutions. Some require pointer, others require static ISR which come with their own disadvantages. The best method I could find is using the function “std::bind” but this could only worked for the ESP32 core with its \<FunctionalInterrupt.h\> and \<WInterrupt.h\> for the STM32duino core. I tested this way with an ESP32 and a STM32F1 Blue Pill, they worked wonderfully.

I wonder if there is an similar solution for the Arduino core since I tried it with an Uno and the error I got was "error: 'bind' is not a member of 'std’".

Thank you for reading.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [September 17, 2025, 5:50am UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/2 "2025-09-17T05:50:01Z")

</div>

This limitation comes from the compiler and standard library for AVR, not from attachInterrupt itself. On ESP32 and STM32, the cores added extended support for functional callbacks, but the AVR core never did, and with the 8-bit AVR resource constraints it’s unlikely to change.

On AVR, attachInterrupt can only accept a plain function pointer with the ISR signature `void func()`. It cannot accept non-static member functions, lambdas with captures, and as you noted std::function support is not there.

If you want to wrap interrupts in a class, you have to use one of the common workarounds like using a static function (which has the correct signature) inside the class as the ISR, and inside that function call back into a global pointer or reference to the instance (That static function does not know which object to call, so you keep a global table of object pointers and each interrupt number corresponds to an entry in that table. When the interrupt fires, attachInterrupt calls the static ISR. The static ISR looks up the correct object pointer from the table (using the interrupt number as the index) and then calls a normal instance method on that object.).

I’m not convinced we need to push OO to that level on small MCUs.

---

<div class="post-metadata">

**Author:** ![DrDiettrich](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drdiettrich/32/219048_2.png) [@DrDiettrich](https://forum.arduino.cc/u/DrDiettrich)\
**Post date:** [September 17, 2025, 9:00am UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/3 "2025-09-17T09:00:36Z")

</div>

> [@viktorthecreator](#):
>
> trying to integrate attachInterrupt into a class

Why would you do that?

Do you want to attach an interrupt to an object or an object to an interrupt?

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 17, 2025, 10:50am UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/4 "2025-09-17T10:50:08Z")

</div>

> [@viktorthecreator](#):
>
> I wonder if there is an similar solution for the Arduino core since I tried it with an Uno

For examples of the technique described by @J-M-L, see:

[My Post #25 Here](https://forum.arduino.cc/t/class-member-function-as-callback-for-interrupt/1381694/25?_gl=1*1ev4yt5*_up*MQ..*_ga*MTE1NDgwMTM1MC4xNzU4MTA1NzEx*_ga_NEXN8H46L5*czE3NTgxMDU3MDkkbzEkZzEkdDE3NTgxMDU3NDIkajI3JGwwJGgxNTY3OTMwODQ3).  
[My Post #6 Here](https://forum.arduino.cc/t/error-invalid-use-of-non-static-member-function/1240240/6?_gl=1*53tapu*_up*MQ..*_ga*MTE1NDgwMTM1MC4xNzU4MTA1NzEx*_ga_NEXN8H46L5*czE3NTgxMDU3MDkkbzEkZzEkdDE3NTgxMDU5MzgkajYwJGwwJGgxNTY3OTMwODQ3).

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [September 17, 2025, 2:19pm UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/5 "2025-09-17T14:19:20Z")

</div>

> [@viktorthecreator](#):
>
> The best method I could find is using the function “std::bind” but this could only worked for the ESP32 core with its \<FunctionalInterrupt.h\> and \<WInterrupt.h\> for the STM32duino core.

BTW, if you are using one of those processors, I find using a lambda expression to be more straightforward than `std::bind`:

```arduino
#include <FunctionalInterrupt.h>

class MyClass {
  public:
    MyClass(uint8_t p) : interruptPin(p) {}

    bool begin() {
      intNumber = static_cast<InterruptNumberType>(digitalPinToInterrupt(interruptPin));
      if (intNumber == NOT_AN_INTERRUPT) {
        return false;
      }
      auto isr {[this]() {
        this->memberISR();
      }};
      attachInterrupt(intNumber, isr, FALLING);
      return true;
    }

    ~MyClass() {
      if (intNumber != NOT_AN_INTERRUPT) {
        detachInterrupt(intNumber);
      }
    }

  private:
    using InterruptNumberType = decltype(NOT_AN_INTERRUPT);
    uint8_t interruptPin;
    InterruptNumberType intNumber {NOT_AN_INTERRUPT};

    void memberISR() {
      // Handle Interrupt Here
    }

};

MyClass myObject(3);

void setup() {
  auto result {myObject.begin()};
  assert(result && "myObject.begin FAILED");
}

void loop() {
}

```

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [March 16, 2026, 2:20pm UTC](https://forum.arduino.cc/t/attachinterrupt-pin-inside-of-class/1406549/6 "2026-03-16T14:20:08Z")

</div>

Closed on March 16, 2026, 2:20pm UTC

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