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

2. Button Detection

Button Detection


Button Detection1. Button Detection Overview2. Core Concepts2.1 Button Bounce and Debouncing2.2 Button Connection and Pull-Up/Pull-Down2.3 Edge Detection Logic2.4 Core API — machine.Pin Input Mode3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Import Modules and Initialization5.2 Main Loop — Polling with Debounce6. Operation Procedure6.1 Flashing and Running6.2 Normal Phenomenon6.3 Verification Methods7. Common Problems


1. Button Detection Overview

GPIO input is the most basic way for an MCU to interact with the outside world. This experiment reads the level of the BOOT button (GPIO0) to control the LED on/off with the button — press once to turn on, press again to turn off. The source code demonstrates the complete flow of GPIO input, software debouncing, edge detection, and a polling architecture.

Typical application scenarios:

Application TypeExample
Digital inputButtons, DIP switches, door magnet sensors
Pulse countingRotary encoders, flow meters
Status detectionWater level switches, limit switches
Wake-up sourceDeep Sleep button wake-up

2. Core Concepts

2.1 Button Bounce and Debouncing

When a mechanical button is pressed/released, the metal contacts bounce repeatedly for 5~20ms, generating a series of glitch pulses:

Ideal waveform:  ────┐             ┌────
              └────────────┘
Actual waveform:  ────┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌┐ ┌────
              └┘ └┘ └┘ └┘ └┘ └┘ └┘
               ├─ bounce period ─┤
              (typically 5~20ms)

Software debouncing: after detecting a level change, wait 20ms before confirming, skipping the bounce region.

2.2 Button Connection and Pull-Up/Pull-Down

The BOOT button on this board has one end connected to GND and the other end directly to GPIO0:

xxxxxxxxxx
GND → Button → GPIO0
Button StateGPIO0 Levelkey.value()
ReleasedFloating (indeterminate)Internal pull-up → 1
PressedPulled low to GND0

Pin.PULL_UP must be enabled, otherwise the pin floats when released and the level jumps randomly.

2.3 Edge Detection Logic

What is detected is the falling edge from "released" to "pressed":

xxxxxxxxxx
if last_key == 1 and current_key == 0:
    time.sleep_ms(20)       # avoid bounce
    if key.value() == 0:    # double-check
        led.toggle()        # toggle LED
last_key = current_key      # must update, otherwise detection fails next time

Only the falling edge (1→0) is detected to avoid repeated triggering while holding the button.

2.4 Core API — machine.Pin Input Mode

xxxxxxxxxx
key = Pin(0, Pin.IN, Pin.PULL_UP)  # input + internal pull-up
val = key.value()                   # read level: 0 or 1

3. Hardware Dependencies

GPIOPeripheralModeDescription
0KEY (BOOT)Pin.IN + Pin.PULL_UPPressed=0, released=1
43LED0Pin.OUTOnboard LED, common-anode, 0=on

The BOOT button is onboard; no external components are required.


4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ led = Pin(43, Pin.OUT)          // GPIO43 output, on by default (0=on)
  ├─ key = Pin(0, Pin.IN, PULL_UP)   // GPIO0 input + pull-up
  ├─ led_state = 0, last_key = 1     // state variable initialization
  │
  └─ while True:                     // polling loop
       ├─ current_key = key.value()   // read button
       ├─ if last==1 and cur==0:      // falling edge detection
       │    ├─ sleep_ms(20)            // debounce
       │    ├─ if key.value() == 0:    // double-check
       │    │    ├─ led_state ^= 1     // toggle LED state
       │    │    └─ led.value(state)   // update LED
       └─ last_key = current_key      // update historical state

5. Source Code Details

Full source code: 5.Source Code → MicroPython → 1.Basic Peripherals → 2.KEY_LED → 2.KEY_LED.py

5.1 Import Modules and Initialization

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

led = Pin(43, Pin.OUT)                  # GPIO43 push-pull output, common-anode: 0=on,1=off
key = Pin(0, Pin.IN, Pin.PULL_UP)       # GPIO0 input + internal pull-up

led_state = 0  # LED state: 0=on, 1=off
last_key = 1   # last key reading

5.2 Main Loop — Polling with Debounce

xxxxxxxxxx
while True:
    current_key = key.value()  # read current key level

    # detect button press: falling edge 1→0, with debounce
    if last_key == 1 and current_key == 0:
        time.sleep_ms(20)       # 20ms debounce
        if key.value() == 0:    # confirm still pressed
            led_state = not led_state  # toggle LED
            led.value(led_state)       # apply to LED

    last_key = current_key  # save for next iteration

6. Operation Procedure

6.1 Flashing and Running

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

  1. Connect the development board with a USB cable

  2. Select the ESP32S3 interpreter at the bottom right of Thonny

  3. Open 2. KEY_LED.py → click the green Run button

  4. Press the onboard BOOT button and observe the LED

6.2 Normal Phenomenon

Before pressing the button

After pressing the button

  • After power-up, the LED is on (led_state=0, 0=on in common-anode)

  • Press BOOT once → LED off

  • Press again → LED on

  • A complete trigger is registered only after releasing and pressing again

6.3 Verification Methods

OperationExpected Result
Power upLED is on
Press BOOT onceLED is off
Press againLED is on
Hold the buttonLED unchanged (no repeated triggering)
Change time.sleep_ms(20) to time.sleep_ms(2)Repeated triggering may occur (insufficient debounce)

7. Common Problems

SymptomCauseSolution
Button has no responsePull_UP not enabled, pin floatingConfirm the Pin.PULL_UP parameter exists
Pressing once toggles multiple timesBounce not eliminated or debounce time too shortIncrease the delay in sleep_ms(20)
LED keeps toggling while holding the buttonOnly low level detected, no edge detectionConfirm the last_key == 1 and current_key == 0 logic
Multiple presses only respond onceDebounce time too long or poor button contactReduce the delay or replace the button
BOOT button conflicts with downloadingHolding BOOT during power-up enters download modePress it after normal operation; it has no effect
LED Control
Peripheral Interrupts

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/中文