ESP8266 External Interrupt Example

In system programming, an interrupt is a signal to the processor emitted by hardware or software indicating an event that needs immediate attention. An interrupt alerts the processor to a high-priority condition requiring the interruption of the current code the processor is executing. The processor responds by suspending its current activities, saving its state, and executing a function called an interrupt handler (or an interrupt service routine, ISR) to deal with the event. This interruption is temporary, and, after the interrupt handler finishes, the processor resumes normal activities.

In this tutorial we learn how to use external interrupts with ESP8266? External interrupts configuration requires three step process.

  1. Initialize IO pin as Input.
  2. Initialize IO with Interrupt Subroutine definition.
  3. Interrupt Subroutine.

The ESP8266 has two different kinds of interrupts: “external”, and “pin change”. ESP8266 all pins have external interrupt except GPIO 16. These interrupts can be set to trigger on RISING or FALLING signal edges, or CHANGE of level.

Interrupt Flow Chart

interrupt flow diagram

Step 1 & 2: Initialize IO pin as Input and Define interrupt

In this example, we use GPIO0 i.e. Flash Button. Note that in the NodeMCU board this pin is labeled as D3. The correct GPIO mapping can be seen here and, as described, the GPIO0 will map to the D3 pin of the NodeMCU board.

First we define pin number to a variable called interruptPin

In setup we configure this pin as input and attach a subroutine to this interrupt pin.
As discussed we can trigger interrupt on three sates FALLING, RISING and CHANG.
here we use change type to trigger interrupt.

Step 3: Write a Subroutine for Interrupt Handler

Write code which you want to execute on interrupt is occurred.

Let’s See Complete Code for Interrupt Handling

This program demonstrate use of interrupt. We are running LED blink program continuously and If interrupt is occurred program prints Interrupt detected on serial terminal.

After uploading program observe the NodeMCU board. Blue LED blinking is our main task running. Open Serial Monitor and Press Flash Button, as soon as you press the button it will print the Interrupt Detected. Without disturbing main task of LED Blinking.

Types of Interrupts

Level-triggered (CHANGE)
A level-triggered interrupt is an interrupt signaled by maintaining the interrupt line at a high or low logic level. A device wishing to signal a Level-triggered interrupt drives the interrupt request line to its active level (high or low), and then holds it at that level until it is serviced. It ceases asserting the line when the CPU commands it to or otherwise handles the condition that caused it to signal the interrupt.

Edge-triggered (RISING, FALLING interrupt)
An edge-triggered interrupt is an interrupt signalled by a level transition on the interrupt line, either a falling edge (high to low) or a rising edge (low to high). A device, wishing to signal an interrupt, drives a pulse onto the line and then releases the line to its inactive state. If the pulse is too short to be detected by polled I/O then special hardware may be required to detect the edge.



Leave a Reply