# External Interrupt

The onboard **USR** button connects to **P6 (GPIO37/EINT20)**. To use this button as an input, you can either poll the pin state with `digitalRead,` or use the EINT APIs in Arduino.

### With Digital Read <a href="#externalinterrupt-withdigitalread" id="externalinterrupt-withdigitalread"></a>

1. Change the  **LED** pin from  **13** to **7**, for example:

   ```
   const int ledPin = 7; // the number of the LED pin
   ```
2. Change the  **button** pin from **2** to **6**, for example:

   ```
   const int buttonPin = 6; // the number of the pushbutton pin
   ```

Now upload the sketch to the board. The onboard  **USR** LED should light up when the **USR** button is pressed.

### With EINT API <a href="#externalinterrupt-witheintapi" id="externalinterrupt-witheintapi"></a>

Arduino provides APIs to handle EINT(**external interrupts**), and only 4 pins on LinkIt 7697 support EINT function. As an example, you'll learn how to use **USR** button (**P6/GPIO37/EINT20**) with the EINT API.

Open Arduino IDE and copy the following code to the editor:

```
#include "Arduino.h" 
 
const int led = 7; // USR LED pin is P7 
const int usr_btn = 6; // USR BTN pin is P6 
int val = 0; // Variable that stores LED state. 
 
void pin_change(void) 
{ 
    digitalWrite(led, val); 
    val = !val; 
    Serial.println("button pressed"); 
} 
 
void setup() { 
    pinMode(led, OUTPUT); 
    attachInterrupt(usr_btn, pin_change, RISING); 
    Serial.begin(9600); 
} 
 
void loop() { 
    delay(1000); 
}
```

In the example code above, when the button is pressed (RISING edge), the `pin_change` callback will be invoked. In the function, you change the state of the LED. After uploading this sketch to the board, press the  **USR** button to turn on the **USR LED**, and press again to turn it off.

Note that only  **P1**, **P2**, **P3**  and **P6** support the `attachInterrupt` API.
