Arduino interrupts beyond Pins 2 & 3

Pins 2 & 3 on the Arduino are great to attach interrupts to; to pick up input even when the Arduino is asleep. The AVR chip though provides 3 more interrupt vectors that can be used to catch interrupts on all pins – even the crystal pins.

These interrupts though are change interrupts and so requires a clock to be present. Therefore, will not be suitable to wake up the Arduino from deep sleep.

Below is example code where interrupts are configured on Pin 8 & Pin 9.

See the Basic AVR page for details on the registers used.

volatile boolean pin8or9changedState = false;

ISR(PCINT0_vect) {
  pin8or9changedState = true;
}

void setup()
{
  Serial.begin(9600);
  Serial.println("Ready...");

  // Enable pull-up on Pin 8 (PB0) & Pin 9 (PB1)
  PORTB |= _BV(PORTB0) | _BV(PORTB1);

  // Enable interrupt on Pin 8 (PCINT0) & Pin 9 (PCINT1)
  PCICR |= _BV(PCIE0);
  PCMSK0 |= _BV(PCINT0) | _BV(PCINT1);
}

void loop()
{
  if (pin8or9changedState) {
    Serial.println("Pin 8 or 9 changed state...");
    pin8or9changedState = false;
  }
}

Leave a Reply