yahboom
HomeTutorialsProduct
Solution
Blog
Online Store
About Us
EN/中文
Store
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

8. PWM Breathing LED

PWM Breathing LED


PWM Breathing LED1. PWM Breathing LED Overview2. Core Concepts2.1 PWM Principle2.2 Breathing Algorithm: Triangular Wave3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Macro Definitions and Global Variables5.2 setup() — Initialize PWM5.3 loop() — Breathing Algorithm6. Operation Flow6.1 Build and Flash6.2 Normal Phenomena6.3 Verification Methods7. Common Issues


1. PWM Breathing LED Overview

PWM (Pulse Width Modulation) controls LED brightness by changing the ratio of high/low level time. This experiment uses analogWrite on GPIO43 to generate PWM waves, changing the duty cycle in a triangular wave pattern to achieve an LED fade-in/fade-out "breathing" effect.

Typical Application Scenarios:

Application TypeExample
LED ControlBreathing light, keyboard backlight, ambient light brightness adjustment
Motor DriveDC motor speed control, servo angle control, stepper motor micro-stepping
Audio GenerationGenerate simple DAC / analog audio output with low-pass filter
Power ManagementDC-DC converter control signals, charging IC enable modulation

Three Core Parameters of PWM

① Period: The 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 the frequency be?

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

  • Moderate (~1kHz): LED appears stable, ordinary motor runs smoothly — this experiment uses 1kHz

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

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

​x
Duty Cycle = (High Level Time / Period) × 100%

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

The beauty of PWM is: you only change the "on/off" time ratio (duty cycle), but the load feels like the voltage is changing continuously. This is the "area equivalent principle" — the average area value of a high-frequency square wave is equivalent to a constant analog voltage value.


2. Core Concepts

2.1 PWM Principle

PWM signal is determined by two parameters: period and duty cycle:

xxxxxxxxxx
Period = 1/frequency (fixed, e.g. 5000Hz → 200μs)
Duty Cycle = High Level Time / Period × 100%

0% duty cycle:  ▏▁▁▁▁▁▁▁▁▏  — always low
50% duty cycle: ▏▔▔▔▔▁▁▁▁▏  — half high half low
100% duty cycle:▏▔▔▔▔▔▔▔▔▏  — always high

ESP32-S3 uses LEDC (LED PWM Controller) peripheral, supporting up to 16-bit resolution. Arduino's analogWrite wraps LEDC, with duty cycle range 0~255 (8-bit effective).

2.2 Breathing Algorithm: Triangular Wave

xxxxxxxxxx
Brightness
255 ┤        ╱╲        ╱╲
    │       ╱  ╲      ╱  ╲
    │      ╱    ╲    ╱    ╲
    │     ╱      ╲  ╱      ╲
  0 ┤────╱        ╲╱        ╲──── Time
       Fade dim    Fade bright    Fade dim

Each iteration updates the brightness value by the step size, reverses direction downward when hitting the upper limit, and reverses direction upward when hitting the lower limit. breathStep controls the change speed (larger = faster but more stepping artifacts), delayMs controls the animation rhythm.


3. Hardware Dependencies

PinPeripheralConfigurationDescription
GPIO43LEDOUTPUTCommon anode connection, PWM=LOW = on

With common anode connection, duty cycle is inversely related to brightness: analogWrite(0) = always low = LED brightest; analogWrite(255) = always high = LED off.


4. Project Architecture and Flow

xxxxxxxxxx
setup()
  ├─ pinMode(43, OUTPUT)       // Configure GPIO as output
  └─ analogWrite(43, 0)        // Initial duty cycle 0 = LED brightest

loop()
  ├─ brightness += direction * step   // Update brightness by direction
  ├─ if brightness >= 255 → clamp + reverse direction (downward)
  ├─ if brightness <= 0   → clamp + reverse direction (upward)
  ├─ analogWrite(43, brightness)      // Output PWM
  └─ delay(10ms)                      // Control change rate

5. Source Code Details

Complete source code: Source Code → Arduino → 1.Basic Peripherals → 8.PWM_LED → 8.PWM_LED.ino

5.1 Macro Definitions and Global Variables

Three parameters control the breathing rhythm. direction of +1 means fade in, -1 means fade out.

xxxxxxxxxx
#define LED0_PIN  43

const int breathStep = 5;         // Step size: change of 5 units per 10ms
const int delayMs = 10;           // Delay per step: 10ms
const int maxBrightness = 255;    // Maximum duty cycle

int currentBrightness = 0;        // Current brightness
int direction = 1;                // Direction: +1 fade in, -1 fade out

Complete breathing cycle ≈ (256/5) × 10ms × 2 ≈ 1024ms ≈ 1 second. Modify breathStep and delayMs to adjust speed.

5.2 setup() — Initialize PWM

analogWrite(0) sets initial duty cycle to 0, corresponding to LED brightest.

xxxxxxxxxx
void setup() {
  pinMode(LED0_PIN, OUTPUT);
  analogWrite(LED0_PIN, 0);     // 0% duty cycle = always low = LED brightest
}

After setting pinMode to OUTPUT, analogWrite automatically assigns LEDC channel and configures PWM parameters for that pin, no additional initialization needed.

5.3 loop() — Breathing Algorithm

Core of triangular wave brightness: reverse direction when hitting boundaries.

xxxxxxxxxx
void loop() {
  currentBrightness += direction * breathStep;

  if (currentBrightness >= maxBrightness) {
    currentBrightness = maxBrightness;
    direction = -1;                        // At top → reverse downward
  } else if (currentBrightness <= 0) {
    currentBrightness = 0;
    direction = 1;                         // At bottom → reverse upward
  }

  analogWrite(LED0_PIN, currentBrightness);
  delay(delayMs);
}

The purpose of clamping currentBrightness is to prevent the brightness from getting "stuck" outside the boundary when the step size does not divide evenly into 255.


6. Operation Flow

6.1 Build and Flash

Arduino flash process: See Arduino → 1. Before Development → 2. Build and Flash.

6.2 Normal Phenomena

After powering on, the LED cycles fade-in→fade-out with approximately 1 second period, simulating a breathing effect.

6.3 Verification Methods

OperationExpected Result
Power on and observeLED fades in and out smoothly, no flickering
Change breathStep = 5 to breathStep = 20Breathing speeds up, stepping artifacts more noticeable
Change delayMs = 10 to delayMs = 30Breathing slows down, smoother

7. Common Issues

PhenomenonCauseSolution
LED not litIncorrect analogWrite value (with common anode, 255=off)Confirm initial value is 0 not 255
Obvious stepping artifacts in breathingStep size too large or delay too shortDecrease breathStep or increase delayMs
Low PWM frequency causing flickerDefault LEDC frequency ~5kHz, not perceivable by eyeNormal, no adjustment needed
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/中文