Skip to main content
Engineering LibreTexts

29.2: External Interrupts

  • Page ID
  • Let’s consider an external interrupt first. In this case, the state change seen at an external pin causes an interrupt. We will use External Interrupt Request 0, also known as the INT0 vector. This interrupt examines state changes at PORTD.2, Uno pin 2. Our example will be fairly simple: we attach a passive switch using the internal pull-up to pin 2. When the switch is activated it will toggle an LED connected to Uno pin 8 (PORTB.0). The code follows:

    /* External Interrupt Example, INT0
       External interrupt pin lights an LED
       Active high LED attached to PORTB.0
       Switch on Uno pin 2, falling edge triggers external pin interrupt 0,
       ISR toggles LED at PORTB.0
    #define LEDMASK 0x01
    void setup()
        DDRB |= LEDMASK;
        // set Uno pin 2 for input with pullup
        DDRD &= ~(0x04);
        PORTD |= 0x04;
        EIMSK |= (1<<INT0);  // enable external interupt INT0 (Uno pin 2)
        EICRA |= (1<<ISC01); // trigger INT0 on falling edge
        PORTB ^= LEDMASK;    // toggle LED
    void loop()

    After setting the IO pins, the code sets the INT0 bit in the EIMSK register to enable the interrupt. The External Interrupt Control Register (EICRA) is programmed to trigger on a falling (negative going) edge. When the switch is activated, the falling edge triggers the ISR to run. The ISR simply toggles the bit that the LED is tied to. Note that there is no port setting and pin examination in the main code. In fact, loop() is empty and does nothing.

    Among other things, an external interrupt such as this is very useful for high importance items such as a panic switch.

    • Was this article helpful?