yahboom
HomeLearn
Solution
Blog
Online Store
About Us
EN/中文
ESP32 Core Board📖
1. ESP32 Introduction
  • 1. ESP32S3 Overview
  • 2. Environment Setup Introduction
  • 3. Dev Board Introduction
2. ESP-IDF Development Tutorial
1. Before Development
  • 1. Environment Setup (Windows)
  • 2. Environment Setup (Linux)
  • 3. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. DMA
  • 8. ADC
  • 9. PWM Breathing LED
  • 10. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. PSRAM External Memory Experiment
  • 5. Flash Read and Write Experiment
  • 6. USB Emulated Mass Storage Experiment
4. FreeRTOS operating system
  • 1.FreeRTOS Core Concepts Theory
  • 2.Task Creation and Management
  • 3.Queue
  • 4.Binary Semaphore
  • 5.Mutex
  • 6.Event Groups
  • 7.Software Timer
  • 8.Task Scheduler
  • 9.Low Power Mode
  • 10.Deadlock
  • 11.Priority Inversion
5. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. Socket Programming Basics
  • 9. HTTP Client Experiment
  • 10. HTTP Server Experiment
  • 11. MQTT IoT Communication Protocol
  • 12. WIFI OTA Upgrade
  • 13. Bluetooth Fundamentals
  • 14. Bluetooth Slave Mode
  • 15. Bluetooth Master Mode
6. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
7. Xiaozhi AI Voice
  • 1. Burn Xiaozhi Firmware
  • 2. Network and Configuration
  • 3. AI Agent Settings
  • 4. Program Analysis
  • 5. Free Chat
3. Arduino Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. ESP_TIMER
  • 5. GPTIMER
  • 6. Watchdog Timer
  • 7. ADC
  • 8. PWM Breathing LED
  • 9. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication
  • 2. I2C OLED Display
  • 3. SPI Bus Communication
  • 4. Flash Read and Write
  • 5. USB Emulated Mass Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Server Experiment
  • 5. UDP Client Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT IoT Communication Protocol
  • 11. WIFI OTA Upgrade
  • 12. Bluetooth Fundamentals
  • 13. Bluetooth Slave Mode
  • 14. Bluetooth Master Mode
5. Audio Applications
  • 1.I2S Microphone Recording and U-Disk Playback
  • 2.I2S Audio Playback Experiment
  • 3.Repeater Experiment
4. MicroPython Development Course
1. Before Development
  • 1. Environment Setup
  • 2. Build and Flash
  • 3. Thonny IDE
2. Basic Peripherals
  • 1. LED Control
  • 2. Button Detection
  • 3. Peripheral Interrupts
  • 4. TIMER_IT
  • 5. Watchdog Timer
  • 6. ADC
  • 7. PWM Breathing LED
  • 8. RGB LED Effects
3. Serial Communication and Storage System
  • 1. Serial UART Communication Experiment
  • 2. I2C_OLED Display Experiment
  • 3. SPI Bus Communication Experiment
  • 4. Flash Read and Write Experiment
  • 5. Print Storage
4. Network Protocols and Wireless Communication
  • 1. WIFI Network Basics
  • 2. WIFI STA Mode
  • 3. WIFI AP Mode
  • 4. UDP Client Experiment
  • 5. UDP Server Experiment
  • 6. TCP Client Experiment
  • 7. TCP Server Experiment
  • 8. HTTP Client Experiment
  • 9. HTTP Server Experiment
  • 10. MQTT
  • 11. Bluetooth Fundamentals
  • 12. Bluetooth Slave Mode
  • 13. Bluetooth Master Mode
5. Audio Applications
  • 1. I2S Audio Playback Experiment
  • 2. Repeater Experiment
Download
  • Code
  • Hardware_Info
  • 3D_Model_File
  • Software_Tools
  • VMware
  • VM_File
Download ZIP

Welcome to ESP32 Core Board repository

3. Peripheral Interrupts

Peripheral Interrupt Experiment


Peripheral Interrupt Experiment1. Peripheral Interrupt Overview2. Core Concepts2.1 Interrupt Trigger Modes2.2 ISR Design Principles2.3 Software Debouncing2.4 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Pins and Global Variables5.2 Interrupt Service Routine (ISR)5.3 Main Loop — Flag Handling + Debounce6. Operation Procedure6.1 Flashing and Running6.2 Normal Phenomenon6.3 Verification Methods7. Common Problems


1. Peripheral Interrupt Overview

The previous experiment used polling to detect the button — the CPU continuously reads the GPIO state. Polling is simple but inefficient: the CPU idles most of the time. Interrupts are another paradigm: the CPU executes the main loop normally, and when the GPIO level changes, the hardware automatically pauses the current code, jumps to the ISR (Interrupt Service Routine) to handle the event, then returns to the breakpoint and continues.

External Interrupt (EXTI) is the hardware mechanism by which an MCU responds to external asynchronous events. When the GPIO pin detects a specified edge (rising edge / falling edge / any edge), the hardware automatically pauses the current program execution, jumps to the preset Interrupt Service Routine (ISR) to handle the event, and returns to the original program to continue after completion.

This experiment demonstrates GPIO external interrupts: the falling edge of the BOOT button (GPIO0) triggers the interrupt, the ISR sets a flag, and the main loop toggles the LED after detecting the flag.

Polling vs Interrupt comparison:

ComparisonPollingInterrupt
CPU usageContinuously polls, consuming CPU even when nothing happensCPU is idle when nothing happens
Response latencyDepends on the polling intervalMicrosecond-level (triggered directly by hardware)
ComplexitySimpleISR has constraints (no delay/print/memory allocation)
Applicable scenarioSimple local controlLow power consumption, real-time response

2. Core Concepts

2.1 Interrupt Trigger Modes

Trigger ModePin ConstantDescription
Falling edgePin.IRQ_FALLINGTriggered at the high→low moment
Rising edgePin.IRQ_RISINGTriggered at the low→high moment
Both edgesPin.IRQ_RISING | Pin.IRQ_FALLINGTriggered on any change
Low levelPin.IRQ_LOW_LEVELContinuously triggered while low (rarely used)
High levelPin.IRQ_HIGH_LEVELContinuously triggered while high (rarely used)

The button experiment uses Pin.IRQ_FALLING: pressing the button (1→0) triggers once, releasing does not trigger.

2.2 ISR Design Principles

MicroPython ISRs run in the hardware interrupt context and have the following constraints:

AllowedForbidden
Set a flag (global flag = True)time.sleep() / time.sleep_ms()
Simple assignmentsprint() / memory allocation
Pin.value() readsCreating objects
—Long loops

Golden rule: the ISR only sets a flag, and the main loop does the actual processing. print() inside the ISR may cause the board to crash.

2.3 Software Debouncing

The interrupt captures the moment of level change, but it may still trigger multiple times in succession due to bounce. The handling approach is the same as polling: ISR sets a flag → main loop delays 20ms → double-check.

2.4 Core API

key.irq(trigger=Pin.IRQ_FALLING, handler=key_isr)
ParameterDescription
triggerTrigger condition: IRQ_FALLING / IRQ_RISING / combination
handlerISR callback function, receives one argument pin (the triggering source)

3. Hardware Dependencies

GPIOPeripheralModeDescription
0KEY (BOOT)Pin.IN + Pin.PULL_UP, falling-edge interruptPressed=0
44LED0Pin.OUTOnboard LED, common-anode, 0=on

4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ led = Pin(44, Pin.OUT, value=0)               // LED initially on (common-anode: 0=on)
  ├─ key = Pin(0, Pin.IN, Pin.PULL_UP)             // button input + pull-up
  ├─ led_state = False, key_flag = False           // global state (led_state=False → 0=on)
  ├─ def key_isr(pin): key_flag = True             // ISR: only sets a flag
  ├─ key.irq(trigger=FALLING, handler=key_isr)     // bind the interrupt
  │
  └─ while True:                                   // main loop
       ├─ if key_flag:                              // detect flag
       │    ├─ key_flag = False                     // clear flag
       │    ├─ sleep_ms(20)                         // debounce
       │    ├─ if key.value() == 0:                 // double-check
       │    │    ├─ led_state ^= 1
       │    │    └─ led.value(led_state)
       └─ sleep_ms(10)                              // idle, reduce usage

5. Source Code Details

Full source code: 5.Source Code → MicroPython → 1.Basic Peripherals → 3.KEY_NVIC → 3.KEY_NVIC.py

5.1 Pins and Global Variables

​x
from machine import Pin  # Pin class: GPIO control
import time              # delay functions

LED_PIN = 44  # onboard LED
KEY_PIN = 0   # BOOT button

led_state = False  # LED state, initially on (0=on)
key_flag = False   # interrupt flag

5.2 Interrupt Service Routine (ISR)

xxxxxxxxxx
led = Pin(LED_PIN, Pin.OUT, value=0)      # LED default on, common-anode: 0=on
key = Pin(KEY_PIN, Pin.IN, Pin.PULL_UP)   # button input + pull-up

def key_isr(pin):  # interrupt callback
    global key_flag
    key_flag = True  # only set the flag, no blocking operations

key.irq(trigger=Pin.IRQ_FALLING, handler=key_isr)  # falling edge trigger

The ISR only does key_flag = True; it does not delay, print, or toggle the LED inside.

5.3 Main Loop — Flag Handling + Debounce

xxxxxxxxxx
while True:
    if key_flag:                    # interrupt fired
        key_flag = False            # clear flag
        time.sleep_ms(20)           # debounce
        if key.value() == 0:        # confirm the button is still pressed
            led_state = not led_state  # toggle LED state
            led.value(led_state)

    time.sleep_ms(10)  # main loop idle, reduce CPU usage

6. Operation Procedure

6.1 Flashing and Running

Thonny IDE steps: See the 1. Before Development → 3.Thonny IDE chapter.

6.2 Normal Phenomenon

Initial state

State after interrupt

  • After power-up, the LED is on and the CPU is idle (no busy-waiting polling loop)

  • Press BOOT once → LED off

  • Press again → LED on

6.3 Verification Methods

OperationExpected Result
Power upLED is on
Press BOOT onceLED is off
Press againLED is on
Add print("ISR") in the ISRMay crash (no print in ISR)
Comment out key_flag = FalseAfter pressing once, the LED toggles every 10ms (flag not cleared)

7. Common Problems

SymptomCauseSolution
Pressing once toggles the LED repeatedlyBounce causes multiple interruptsAdd debouncing when handling the flag
No response after multiple presses, suddenly triggers onceISR constraints not followed, stack overflowCheck whether the ISR does delay/print/object creation
Interrupt does not triggerirq() parameters wrong or handler misspelledConfirm trigger=Pin.IRQ_FALLING and the handler function name
led_state changed but LED unchangedglobal led_state not declaredEnsure the global declaration exists in the ISR or main loop
Button Detection
TIMER_IT

COMPANY

  • About us
  • Team Style
  • Blog

LEARNING & EXPLORATION

  • Tutorials
  • Edge AI hardware
  • Educational robots
  • Embedded intelligent hardware

WHERE TO BUY

  • Yahboom Online Store
  • Amazon
  • Alibaba
  • AliExpress
  • Mercado

TECHNOLOGY SUPPORT

  • Email: support@yahboom.com

BE A DISTRIBUTOR

  • Email: sales@yahboom.com
QR Code

Copyright © 2017-2026 Yahboom. All Rights Reserved.

EN/中文