# How to attachInterrupt to a function inside a class

**URL:** <https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507>\
**Category:** General Guidance\
**Created:** [April 3, 2023, 4:30pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507 "2023-04-03T16:30:45Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![drakejest](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drakejest/32/1013044_2.png) [@drakejest](https://forum.arduino.cc/u/drakejest)\
**Post date:** [April 3, 2023, 4:30pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/1 "2023-04-03T16:30:45Z")

</div>

Im making a class for my encoder that uses interrupts and in begin() function i need it to attach to a function of the class. but it throws me an error

`error: invalid use of non-static member function 'void ENCODER::recordTime()'`

Minimal Code that throws error

```cpp
Class ENCODER{
private:
   void recordTime(){
   }

public:
   void begin(){
      attachInterrupt(digitalPinToInterrupt(5), this->recordTime, RISING)
   }
};

```

I have tried and they all met the same error.

```cpp
attachInterrupt(digitalPinToInterrupt(5), recordTime, RISING)
attachInterrupt(digitalPinToInterrupt(5), this.recordTime, RISING)
attachInterrupt(digitalPinToInterrupt(5), this->recordTime, RISING)

```

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 3, 2023, 4:44pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/2 "2023-04-03T16:44:43Z")

</div>

Did you see this clue in the error message?

> [@drakejest](#):
>
> non-static member

---

<div class="post-metadata">

**Author:** ![Railroader](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/railroader/32/274550_2.png) [@Railroader](https://forum.arduino.cc/u/Railroader)\
**Post date:** [April 3, 2023, 5:13pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/3 "2023-04-03T17:13:54Z")

</div>

What makes You think You should use class?  
Interrupt is a hardware dependant function. You can't "class", like clone, one piece of hardware inte several instances.

---

<div class="post-metadata">

**Author:** ![drakejest](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drakejest/32/1013044_2.png) [@drakejest](https://forum.arduino.cc/u/drakejest)\
**Post date:** [April 3, 2023, 5:47pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/4 "2023-04-03T17:47:17Z")

</div>

although true that you cant clone one piece hardware to several instance, it is possible to pass a pointer to the class , just like how you can pass the SPI or Wire pointer to multiple class instance.

but im not sure though that attachInterrupt is a class or just an ordinary function

---

<div class="post-metadata">

**Author:** ![drakejest](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/drakejest/32/1013044_2.png) [@drakejest](https://forum.arduino.cc/u/drakejest)\
**Post date:** [April 3, 2023, 5:49pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/5 "2023-04-03T17:49:42Z")

</div>

I apologize but im going to need a bit more explination...

---

<div class="post-metadata">

**Author:** ![b707](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/b707/32/854321_2.png) [@b707](https://forum.arduino.cc/u/b707)\
**Post date:** [April 3, 2023, 6:38pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/6 "2023-04-03T18:38:35Z")

</div>

You can't attach the interrupt to the class method.

You should create an intermediate non-class static function, from which you will be able to call a class method for specific class instance:

```cpp
class Example_class
{
  public:
   void foo() {};
};

// pointer to the our class instance
Example_class* E;

// interrupt function, calling the class method
static void irq_func() {
  E->foo();
}

Example_class A;

void setup() {
  E = &A; 
  attachInterrupt(2, FALLING, irq_func);
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 3, 2023, 6:46pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/7 "2023-04-03T18:46:28Z")

</div>

> [@drakejest](#):
>
> im going to need a bit more explination...

Think about it for a moment.  
A class is designed to have multiple instances.  
But an interrupt is bound to a single hardware resource.  
If you had an ISR that was a class member, then every instance of the class would contend for the same hardware resource.

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [April 3, 2023, 10:02pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/8 "2023-04-03T22:02:39Z")

</div>

> [@anon56112670](#):
>
> But an interrupt is bound to a single hardware resource.

That's not necessarily true. Consider multiple sensors, all tied to the same external interrupt signal with a big "or" gate. However, ISRs are pure C functions, and do not know how to set up "this" for calling a class function.

b707's suggestion of an intermediate non-class function is the way to go.

You can look at the way Serial is handled in the core (on boards with multiple serial ports, like a Zero or Nano Every), for an example.

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 3, 2023, 10:06pm UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/9 "2023-04-03T22:06:27Z")

</div>

> [@westfw](#):
>
> That's not necessarily true.

I meant it in the sense of "it's bound to a single _processor_ hardware resource" - like a falling or rising edge on a particular pin, or a receiver register full condition

---

<div class="post-metadata">

**Author:** ![westfw](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/westfw/32/213443_2.png) [@westfw](https://forum.arduino.cc/u/westfw)\
**Post date:** [April 4, 2023, 12:12am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/10 "2023-04-04T00:12:39Z")

</div>

> [@anon56112670](#):
>
> "it's bound to a single _processor_ hardware resource"

I dunno. Quibbling over semantics. There are CPUs without the concept of "vectored" interrupts, with on

---

<div class="post-metadata">

**Author:** ![corneo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/corneo/32/936959_2.png) [@corneo](https://forum.arduino.cc/u/corneo)\
**Post date:** [April 4, 2023, 1:30am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/11 "2023-04-04T01:30:48Z")

</div>

How would a static member function get the right context? Can they access class variables using this. to this-\> perhaps?

---

<div class="post-metadata">

**Author:** ![corneo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/corneo/32/936959_2.png) [@corneo](https://forum.arduino.cc/u/corneo)\
**Post date:** [April 4, 2023, 2:08am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/12 "2023-04-04T02:08:26Z")

</div>

Not if each class instance deals with interrupts from a distinct pin. I do it in one of my projects it works flawlessly.

---

<div class="post-metadata">

**Author:** ![anon56112670](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@anon56112670](https://forum.arduino.cc/u/anon56112670)\
**Post date:** [April 4, 2023, 7:55am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/13 "2023-04-04T07:55:48Z")

</div>

> [@westfw](#):
>
> Quibbling over semantics

If we didn't, we wouldn't be programmers, would we?

---

<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:** [October 1, 2023, 7:56am UTC](https://forum.arduino.cc/t/how-to-attachinterrupt-to-a-function-inside-a-class/1110507/14 "2023-10-01T07:56:09Z")

</div>

Closed on October 1, 2023, 7:56am UTC

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