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

7. PWM Breathing LED

PWM Breathing LED


PWM Breathing LED1. Breathing Light Overview2. Core Concepts2.1 What is PWM2.2 PWM Parameter Selection2.3 PWM Logic with a Common-Anode Connection2.4 Core API3. Hardware Dependencies4. Project Architecture and Flow5. Source Code Details5.1 Parameter Configuration and Initialization5.2 Breathing Loop6. Operation Procedure6.1 Flashing and Running6.2 Serial Output Example6.3 Normal Phenomenon6.4 Verification Methods7. Common Problems


1. Breathing Light Overview

PWM (Pulse Width Modulation) controls the average power by adjusting the time ratio of high and low levels. This experiment uses machine.PWM to drive the onboard LED (GPIO44). The duty cycle decreases from MAX (off) to VISIBLE (on) and then increases back to MAX, achieving a "breathing" effect of gradually brightening and dimming. The off phase additionally holds for 2s to form a complete breathing rhythm.

Typical application scenarios:

Application TypeExample
Brightness adjustmentLED dimming, backlight control
Motor controlServo angle, DC motor speed control
Audio outputBuzzer tone control
Power managementDC-DC voltage regulation

2. Core Concepts

2.1 What is PWM

A PWM signal is defined by two parameters: period and duty cycle:

Period = 1 / Frequency
   │←────── period ──────→│
   ┌──┐              ┌──┐
   │  │              │  │
───┘  └──────────────┘  └───
   │←─┤               │
   pulse width = duty cycle × period

Average voltage = duty cycle × VDD.

2.2 PWM Parameter Selection

ParameterValue in This ExperimentDescription
Frequency (freq)1000 HzAbove the human flicker perception threshold (~100Hz)
Duty cycle range0 ~ 102310-bit resolution (2^10 = 1024 levels)
MAX_DUTY1023Full high = off (common-anode)
VISIBLE200Brightness threshold; below this value the LED is visibly on

If the frequency is too low (< 50Hz), flicker is perceptible to the human eye. 1kHz is the standard choice for LED dimming.

2.3 PWM Logic with a Common-Anode Connection

The onboard LED uses a common-anode connection: duty=1023 (100% duty cycle, high output) = LED off (equal potential at both ends, no current), duty=0 = LED brightest.

With a common-anode connection, the PWM duty cycle is inversely proportional to brightness — duty↓ = on, duty↑ = off.

2.4 Core API

​x
from machine import Pin, PWM

pwm = PWM(Pin(44))                  # create the PWM object
pwm.freq(1000)                       # set frequency to 1000Hz
pwm.duty(512)                        # set duty cycle 0~1023
pwm.deinit()                         # disable PWM

3. Hardware Dependencies

GPIOPeripheralModeDescription
44LED0PWMOnboard LED, common-anode, duty↑ = brightness↓

4. Project Architecture and Flow

xxxxxxxxxx
Script execution (single-file .py)
  │
  ├─ Parameter config: MAX_DUTY=1023, VISIBLE=200, STEP=5, DELAY=12ms
  ├─ pwm = PWM(Pin(44), freq=1000, duty=1023)  // 1kHz, initially off
  │
  └─ while True:
       ├─ Off hold: pwm.duty(1023), sleep(2s)   // fully off, hold 2s
       │    └─ print("LED OFF (duty=1023)")
       ├─ Fade-in: for d in 1023→200 step -5:   // duty decreases (duty↓=brightness↑)
       │    ├─ print("Fade-in: duty 1023 → 200")
       │    ├─ pwm.duty(d)
       │    └─ sleep_ms(12)
       │    └─ print("LED ON (duty=200)")
       └─ Fade-out: for d in 200→1023 step 5:   // duty increases (duty↑=darkness↑)
            ├─ print("Fade-out: duty 200 → 1023")
            ├─ pwm.duty(d)
            └─ sleep_ms(12)

5. Source Code Details

Full source code: 5.Source Code → MicroPython → 1.Basic Peripherals → 7.PWM_LED → 7.PWM_LED.py

5.1 Parameter Configuration and Initialization

xxxxxxxxxx
from machine import Pin, PWM  # Pin and PWM classes
import time                    # delay

# Configuration parameters
LED_PIN    = 44              # LED pin (common-anode)
PWM_FREQ   = 1000            # PWM frequency 1kHz
MAX_DUTY   = 1023            # 10-bit max duty cycle (=HIGH=OFF)
VISIBLE    = 200             # visible threshold (<this value=LOW=ON)
STEP       = 5               # duty step
DELAY_MS   = 12              # step delay
OFF_HOLD_S = 2               # OFF hold time (s)

# Init PWM: GPIO44, 1kHz, 10-bit
pwm = PWM(Pin(LED_PIN), freq=PWM_FREQ, duty=MAX_DUTY)

VISIBLE=200 is an empirical value — with a common-anode connection, the LED only becomes visibly on when the duty is below ~200. Above this value the LED still looks off, so fading in from MAX_DUTY down to VISIBLE is sufficient.

5.2 Breathing Loop

xxxxxxxxxx
# Breathing LED
# Common-anode: LOW=ON (small duty), HIGH=OFF (large duty)

while True:
    # OFF phase: duty=MAX, hold for OFF_HOLD_S seconds
    pwm.duty(MAX_DUTY)
    print("LED OFF (duty=%d)" % MAX_DUTY)
    time.sleep(OFF_HOLD_S)

    # OFF→ON: duty decrement
    print("Fade-in: duty %d → %d" % (MAX_DUTY, VISIBLE))
    for d in range(MAX_DUTY, VISIBLE - 1, -STEP):
        pwm.duty(d)
        time.sleep_ms(DELAY_MS)
    print("LED ON (duty=%d)" % VISIBLE)

    # ON→OFF: duty increment
    print("Fade-out: duty %d → %d" % (VISIBLE, MAX_DUTY))
    for d in range(VISIBLE, MAX_DUTY + 1, STEP):
        pwm.duty(d)
        time.sleep_ms(DELAY_MS)

6. Operation Procedure

6.1 Flashing and Running

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

6.2 Serial Output Example

xxxxxxxxxx
LED OFF (duty=1023)
Fade-in: duty 1023 → 200
LED ON (duty=200)
Fade-out: duty 200 → 1023
LED OFF (duty=1023)
Fade-in: duty 1023 → 200
...

Serial output

6.3 Normal Phenomenon

The LED "breathes" slowly — off 2s → fade in → fade out → off 2s → repeat.

The TX blue indicator light blinks intermittently, indicating that the program is outputting print information through the serial port.

6.4 Verification Methods

OperationExpected Result
Power upSmooth breathing effect of the LED
Change OFF_HOLD_S=2 to OFF_HOLD_S=0No pause in the off phase, continuous breathing
Change STEP=5 to STEP=20Breathing noticeably faster
Change DELAY_MS=12 to DELAY_MS=50Breathing very slow, visible step changes

7. Common Problems

SymptomCauseSolution
LED does not light upWrong pin configurationConfirm using PWM(Pin(44)) instead of Pin(44, Pin.OUT)
Breathing not smooth, steppySTEP too large or DELAY too shortReduce STEP (e.g., 2) to increase the total number of steps
LED visibly flickersFrequency too lowThe frequency must be at least > 100Hz
duty() errors or has no effectThe pin is not initialized as PWMConfirm PWM(Pin(44)) instead of Pin(44, Pin.OUT)
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/中文