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

9. PWM Breathing LED

PWM Breathing LED

PWM Breathing LED


PWM Breathing LED1. PWM Overview1.1 What is PWM1.2 Three Core Parameters of PWM1.3 What This Experiment Does2. Core Concepts2.1 Duty Cycle and Resolution2.2 Common-Anode LED PWM Control2.3 Why VISIBLE_DUTY = 200 Instead of 02.4 LEDC Peripheral Architecture3. Project Architecture and Flow4. Hardware Dependencies5. Source Code Analysis5.1 Macro Definitions5.2 led_pwm_init() — LEDC Initialization5.3 breath_led_task() — Breathing Light Algorithm (Core)5.4 app_main() — Main Entry6. Operation Flow6.1 Build and Flash6.2 Expected Phenomenon6.3 Parameter Adjustment Guide7. Common Issues


1. PWM Overview

1.1 What is PWM

PWM (Pulse Width Modulation) is simply a technique that "uses digital switches to simulate analog voltage".

In digital circuits, GPIO pins can only output two states: high (3.3V) or low (0V), no intermediate values. But PWM cleverly bypasses this limitation through "rapid switching"—the faster the switching, and with different ratios of on-time, the resulting "average effect" on the load differs.

Think of it this way: you can't make a water tap produce "half-thick" water column, but you can rapidly turn it on and off, 50% on / 50% off, and the bucket receives only half the water of a fully open tap. PWM is this concept.

1.2 Three Core Parameters of PWM

① Period: Time for one complete "on→off" cycle, in seconds. Period = 1 / frequency.

② Frequency: How many "on→off" cycles per second, in Hz. How high should frequency be?

  • Too low (<50Hz): Human eye can detect LED flickering, motor vibrates

  • Moderate (~1kHz): LED appears stable, ordinary motors run smoothly—this experiment is 1kHz

  • Too high (>20kHz): Beyond human hearing range, suitable for quiet motor drive scenarios, but switching losses increase

③ Duty Cycle: In one cycle, the ratio of "high level time" to "entire cycle time".

​x
Duty cycle = (high level time / period) × 100%

Example frequency 1kHz (period = 1ms):
  25% duty cycle  = high 0.25ms + low 0.75ms  → average voltage ≈ 0.825V
  50% duty cycle  = high 0.50ms + low 0.50ms  → average voltage = 1.65V
  80% duty cycle  = high 0.80ms + low 0.20ms  → average voltage = 2.64V

PWM's magic is: You only change the "on/off" time ratio (duty cycle), but the load feels like voltage is continuously changing. This is the "area equivalence principle"—the average value of high-frequency square wave equals a constant analog voltage value.

1.3 What This Experiment Does

This experiment uses ESP32-S3's LEDC (LED PWM Controller) peripheral to generate PWM signals, driving an LED to display "breathing light" effect by dynamically changing duty cycle: gradually bright → gradually dim → off stay → repeat.

Application Scenarios:

ScenarioDescription
LED brightness controlBreathing light, keyboard backlight, ambiance light
Motor speed controlServo angle, DC motor speed
Audio outputGenerate analog audio with low-pass filter
Power managementDC-DC converter control signal

2. Core Concepts

2.1 Duty Cycle and Resolution

xxxxxxxxxx
PWM signal (1kHz frequency, period = 1ms):

  0% duty cycle:   ──────────────────────────────  average voltage = 0V
                   (entire low level)

  25% duty cycle:  ┌──┐┌─────────────────────────  average voltage = 0.825V
                   └──┘└─────────────────────────

  50% duty cycle:  ┌─────┐┌──────────────────────  average voltage = 1.65V
                   └─────┘└──────────────────────

  100% duty cycle: ┌──────────────────────────────  average voltage = 3.3V
                   └──────────────────────────────

Duty cycle resolution: LEDC_TIMER_10_BIT means duty cycle divided into 0~1023 (1024 levels total). With 10-bit resolution:

Duty ValueMeaning (common-anode this experiment)
1023 (MAX_DUTY)High level 100% = LED completely OFF
511High level ~50% = LED half bright
200 (VISIBLE_DUTY)High level ~20% = LED relatively bright (low level 80%)
0High level 0% = LED brightest (all low level)

2.2 Common-Anode LED PWM Control

xxxxxxxxxx
Common-anode connection:
  VDD (3.3V) ──→ LED anode ──→ LED cathode ──→ Current-limiting resistor ──→ GPIO44

  GPIO output HIGH (1023): LED both ends at same potential, no current → OFF
  GPIO output LOW  (0):   Current VDD→LED→GPIO→GND  → ON

This experiment's "reverse brightness" relationship:

duty valueLow level ratioLED brightnessState description
10230%OFFOFF phase stay
1023→2000%→80%Gradually brightfade-in
200→102380%→0%Gradually dimfade-out

In common-anode connection, duty decreases → low level ratio increases → LED gets brighter. This is opposite to the conventional understanding "duty increases = brighter", so code uses MAX_DUTY (1023) for OFF, VISIBLE_DUTY (200) for visible brightness.

2.3 Why VISIBLE_DUTY = 200 Instead of 0

If duty drops to 0 (100% low level = brightest), LED will instantly jump from "fully bright" to "fully off" (phase switch), visually discontinuous. 200 provides a "brightest but not fully bright" visible threshold, forming brightness contrast with MAX_DUTY, making breathing effect smooth.

2.4 LEDC Peripheral Architecture

xxxxxxxxxx
┌──────────┐    ┌──────────┐    ┌──────────┐
│ Timer 0  │───→│ Channel 0│───→│ GPIO44   │
│ Timer 0  │    │ Channel 0│    │ (LED)    │
│ 1kHz     │    │ duty=... │    │          │
│ 10-bit   │    │          │    │          │
└──────────┘    └──────────┘    └──────────┘

One timer can be shared by multiple channels (same frequency, different duty cycles),
one channel can be bound to different GPIO pins.

LEDC Resources:

Resource TypeAvailable CountDescription
Timer4Determines PWM frequency and resolution
Channel8Bind GPIO and duty cycle, share timer
Speed modeLow Speed / High SpeedLow speed suitable for LED, high speed for precise edge requirements

3. Project Architecture and Flow

xxxxxxxxxx
app_main()
  ├─ led_pwm_init()              Initialize LEDC timer + channel
ADC
RGB LED Effects

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