# Is it possible attachInterrupt for 11 pin?

**URL:** <https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009>\
**Category:** Programming\
**Created:** [February 24, 2022, 1:09pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009 "2022-02-24T13:09:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 24, 2022, 1:09pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/1 "2022-02-24T13:09:24Z")

</div>

hi

I know this is possible manually.  
but can I somehow in arduino  
attachInterrupt for digitalPinToInterrupt(11)

PS: NANO

---

<div class="post-metadata">

**Author:** ![Deva\_Rishi](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/deva_rishi/32/227254_2.png) [@Deva\_Rishi](https://forum.arduino.cc/u/Deva_Rishi)\
**Post date:** [February 24, 2022, 1:12pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/2 "2022-02-24T13:12:08Z")

</div>

No only pins 2 & 3 have pin change interrupts on a nano  
[See the Reference](https://www.arduino.cc/reference/en/language/functions/external-interrupts/attachinterrupt/)

---

<div class="post-metadata">

**Author:** ![rpiloverbd](https://avatars.discourse-cdn.com/v4/letter/r/cab0a1/32.png) [@rpiloverbd](https://forum.arduino.cc/u/rpiloverbd)\
**Post date:** [February 24, 2022, 1:12pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/3 "2022-02-24T13:12:49Z")

</div>

For UNO and Nano, attachinterrupt() is only available on pin#2 and pin#3.

---

<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:** [February 24, 2022, 1:12pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/4 "2022-02-24T13:12:58Z")

</div>

only pin 2 and 3 have **External Interrupt** support  
all the 20 signal pins can be used as interrupt pins if you go for **pin change interrupts**

But as other stated, you can't use attachinterrupt(). There are many online tutorials for pin change interrupts, here is a first google hit (did not read it)

> **[Arduino Pin Change Interrupts](https://thewanderingengineer.com/2014/08/11/arduino-pin-change-interrupts/)**
>
> I recently needed to do some work with Pin Change Interrupts and it was a bit of a learning experience for me.  As it turns out, they’re actually pretty easy.  I’m posting this so that …

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 24, 2022, 4:12pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/5 "2022-02-24T16:12:30Z")

</div>

Tools-\>Manage libraries...  
Search for "PinChangeInterrupt"

That library will let you use Pin Change interrupts much like External interrupts.

See the documentation here:

> **[GitHub - NicoHood/PinChangeInterrupt: A simple & compact...](https://github.com/NicoHood/PinChangeInterrupt)**
>
> A simple & compact PinChangeInterrupt library for Arduino - GitHub - NicoHood/PinChangeInterrupt: A simple & compact PinChangeInterrupt library for Arduino

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 25, 2022, 1:20pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/6 "2022-02-25T13:20:09Z")

</div>

J-M-L, johnwasser, please help me to figure out my mistake  
in the following code I want to print the pin number (which fired interrupt)  
but always get 0

```cpp
int ledPin = 13; // LED
int PIR = 3; // PIR Out pin
int pirStat = 0; // PIR status
int flag = 0;
int KEYVAL = 0;
int PBLAST = 0;

void setup() {

  DDRB = 0; PORTB = 0;
  cli();
  PCICR |= (1 << PCIE0); // Enables Ports B as Pin Change Interrupts
  PCMSK0 |= (1 << PCINT7) | (1 << PCINT6) | (1 << PCINT5); // PCINT0
  sei();

  pinMode(ledPin, OUTPUT);
  Serial.begin(19200);
}
void loop() {

  if (flag) {
    char PBNOW = PINB ^ PBLAST;
    switch (PBNOW) {
      case (1 << PB7):
        KEYVAL = 11;
        break;
      case (1 << PB6):
        KEYVAL = 10;
        break;
      case (1 << PB5):
        KEYVAL = 9;
        break;
      default:
        break;
    }
    flag = 0;
    Serial.println(millis());
    digitalWrite(ledPin, HIGH); // turn LED ON
    delay(4000);
    digitalWrite(ledPin, LOW); // turn LED OFF if we have no motion
    Serial.println(KEYVAL); // HERE should be pin
KEYVAL = 0;
    PBLAST = PINB;
    PCMSK0 |= (1 << PCINT7) | (1 << PCINT6) | (1 << PCINT5);
  }

}

ISR(PCINT0_vect)
{
  PCMSK0 = 0;
  flag++;
}

```

---

<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:** [February 25, 2022, 1:46pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/7 "2022-02-25T13:46:53Z")

</div>

which pins are you targeting?

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 25, 2022, 1:59pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/8 "2022-02-25T13:59:28Z")

</div>

KEYVAL starts with the value 0 and only changes if exactly one of the PORTB pins has changed.

Try masking off the unused pins before the XOR. That won't help if two pins change before you handle the interrupt.

WARNING! Pin 9 is PB1, Pin 10 is PB2, and Pin 11 is PB3. You are using PB5, PB6, and PB7 for some reason. PB7 is not connected to an Arduino UNO/Nano pin.

If you want to keep track of which pin or pins triggered interrupts, you should probably do that in the ISR.

```cpp
volatile byte Rising = 0;
volatile byte Falling = 0;
volatile byte Changed = 0;

ISR(PCINT0_vect)
{
  static byte oldState = 0;
  byte newState = PINB;
  changed = oldState ^ newState;
  Rising|= newState & changed;
  Falling |= (~newState) & changed; // TYPO: had 'Changed'
  Changed |= changed;
  oldState = newState; // EDIT: Forgot this line!
}

```

Then in loop() you can grab the data an report:

```cpp
void loop()
{
  noInterrupts();
  byte changed = Changed;
  byte rising = Rising;
  byte falling = Falling;
  Changed = 0;
  Rising = 0;
  Falling = 0;
  interrupts();

   if (rising & _BV(PB1)) // Pin 9
   {
      Serial.print(millis());
      Serial.println(" Pin 9 RISING")
   }
   if (falling & _BV(PB1)) // Pin 9
   {
      Serial.print(millis());
      Serial.println(" Pin 9 FALLING")
   }

   if (rising & _BV(PB2)) // Pin 10
   {
      Serial.print(millis());
      Serial.println(" Pin 10 RISING")
   }
   if (falling & _BV(PB2)) // Pin 10
   {
      Serial.print(millis());
      Serial.println(" Pin 10 FALLING")
   }

   if (rising & _BV(PB3)) // Pin 11
   {
      Serial.print(millis());
      Serial.println(" Pin 11 RISING")
   }
   if (falling & _BV(PB3)) // Pin 11
   {
      Serial.print(millis());
      Serial.println(" Pin 911FALLING")
   }
}

```

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 25, 2022, 2:01pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/9 "2022-02-25T14:01:10Z")

</div>

I have connected PB5..PB7 to PCINT  
when interrupt is triggered I want to print respective pin.

I tested little bit now and looks like pin 9 and pin 10 is printing correct to serial...  
only instead 11, I see in the serial 0

cant understand why 11 is not printing

---

<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:** [February 25, 2022, 2:02pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/10 "2022-02-25T14:02:21Z")

</div>

> [@johnwasser](#):
>
> `Changed |= changed`

why `|=` ? maintaining the changed status if not dealt with in loop?

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 25, 2022, 2:03pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/11 "2022-02-25T14:03:05Z")

</div>

> [@johnwasser](#):
>
> WARNING! Pin 9 is PB1, Pin 10 is PB2, and Pin 11 is PB3. You are using PB5, PB6, and PB7 for some reason. PB7 is not connected to an Arduino UNO/Nano pin.

sorry, pb5-pb7 for micro board....

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 25, 2022, 2:17pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/12 "2022-02-25T14:17:55Z")

</div>

> [@J-M-L](#):
>
> maintaining the changed status if not dealt with in loop?

Yes. I figured the main program would want to know that a pin had changed even if it wasn't changed in the latest interrupt.

---

<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:** [February 25, 2022, 2:18pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/13 "2022-02-25T14:18:20Z")

</div>

OK makes sense

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 25, 2022, 2:19pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/14 "2022-02-25T14:19:20Z")

</div>

> [@alexblade](#):
>
> pb5-pb7 for micro board....

So when you put "PS: NANO" in your original post that should have been "PS: Micro"?

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 25, 2022, 2:47pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/15 "2022-02-25T14:47:20Z")

</div>

in original was nano, now on my desktop micro (I know micro has only one port for pcint and nano - 3) sorry. but issue not in port's name

cannt understand why PB6 is printing and PB7 not :\

now , I found one more issue, if arduino goes to sleep, and when wake up from PCINT then

```cpp
PBNOW 

```

is not contain current triggered pin ☹ always prints 0 even for PB6

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 25, 2022, 8:26pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/16 "2022-02-25T20:26:31Z")

</div>

to avoid confusing all I 'll open new topic

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 25, 2022, 11:39pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/17 "2022-02-25T23:39:11Z")

</div>

This sketch works on my UNO and can be switched between UNO and Micro.

```cpp
#if 1 // 1 for Arduino Micro, 0 for UNO or Nano
const byte Mask11 = _BV(PCINT7);
const byte Mask10 = _BV(PCINT6);
const byte Mask9 = _BV(PCINT5);
#else
const byte Mask11 = _BV(PCINT3);
const byte Mask10 = _BV(PCINT2);
const byte Mask9 = _BV(PCINT1);
#endif

volatile byte Rising = 0;
volatile byte Falling = 0;
volatile byte Changed = 0;

ISR(PCINT0_vect)
{
  static byte oldState = 0;
  byte newState = PINB;
  byte changed = oldState ^ newState;
  Rising |= newState & changed;
  Falling |= (~newState) & changed;
  Changed |= changed;
  oldState = newState;
}

void setup()
{
  Serial.begin(115200);
  delay(200);

  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);

  noInterrupts();
  PCICR |= _BV(PCIE0); // Enables Ports B as Pin Change Interrupts
  PCMSK0 = Mask11 | Mask10 | Mask9;
  PCIFR = _BV(PCIF0);
  interrupts();
}

void loop()
{
  noInterrupts();
  // byte changed = Changed;
  byte rising = Rising;
  byte falling = Falling;
  Changed = 0;
  Rising = 0;
  Falling = 0;
  interrupts();

  if (rising & Mask9) // Pin 9
  {
    Serial.print(millis());
    Serial.println(" Pin 9 RISING");
  }
  if (falling & Mask9) // Pin 9
  {
    Serial.print(millis());
    Serial.println(" Pin 9 FALLING");
  }

  if (rising & Mask10) // Pin 10
  {
    Serial.print(millis());
    Serial.println(" Pin 10 RISING");
  }
  if (falling & Mask10) // Pin 10
  {
    Serial.print(millis());
    Serial.println(" Pin 10 FALLING");
  }

  if (rising & Mask11) // Pin 11
  {
    Serial.print(millis());
    Serial.println(" Pin 11 RISING");
  }
  if (falling & Mask11) // Pin 11
  {
    Serial.print(millis());
    Serial.println(" Pin 11 FALLING");
  }
}
```

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 26, 2022, 3:05pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/18 "2022-02-26T15:05:38Z")

</div>

> [@johnwasser](#):
>
> `oldState = newState; // EDIT: Forgot this line!`

I think this is not enough real case... for example for my interrupt (from PIR sensor) level changed for 2 (or more - can be tuned) seconds (HI) then it self returns to initial it state (LOW). so only HI level meaning that it triggered. and many sensors works in this mode  
so ... when it HI your ISR will trigger and this is line stores HI level to oldState, after 2 seconds level became LOW your ISR will trigger again , it will think that now falling state...

but I understand its not error in the code just one of many cases how it could be implemented/worked

thanks for helping

(now will try to execute your code from this and second topics)

---

<div class="post-metadata">

**Author:** ![alexblade](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alexblade/32/279364_2.png) [@alexblade](https://forum.arduino.cc/u/alexblade)\
**Post date:** [February 26, 2022, 3:33pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/19 "2022-02-26T15:33:52Z")

</div>

> [@johnwasser](#):
>
> ` Changed |= changed;`

why we storing something in Changed if in the loop it always is zero ?

I tried this code on micro

```arduino
17079 Pin 11 RISING
19745 Pin 11 FALLING
36873 Pin 11 RISING
39826 Pin 11 FALLING

```

so Im confused :\ why in your code pin 11 is reading in my - no 🙂

pin 10 reads in both ( my and your)  
hm...

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [February 26, 2022, 3:34pm UTC](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009/20 "2022-02-26T15:34:14Z")

</div>

> [@alexblade](#):
>
> when it becomes HIIGH your ISR will trigger and, after 2 seconds, when the level becomes LOW your ISR will trigger again

That is how 'Pin Change' interrupts work. They interrupt when the state of a pin changes. If you don't care about the pin Falling, don't check the pin's Falling flag.

> [@alexblade](#):
>
> why we storing something in Changed if in the loop it always is zero ?

It isn't always zero. It just isn't used. I'm looking at the Rising and Falling flags so I don't need the Changed flags.

> [@alexblade](#):
>
> why in your code pin 11 is reading in my - no

Maybe you have made some coding mistake? Maybe there is a fault in the "deepSleep" library?

[Next page](https://forum.arduino.cc/t/is-it-possible-attachinterrupt-for-11-pin/963009.md?page=2)
