Dimming, fans, servos. Hardware PWM, not bit-bang.
Sensors and I/OPWMESP32 family
Official path: examples/peripherals/ledc
01 Overview
LEDC is the ESP32's hardware PWM peripheral, generating precise square waves via timers without CPU intervention.
It solves the problem of software bit-banging, which consumes CPU cycles and introduces jitter, making it ideal for applications requiring stable frequency and duty cycle.
Typical uses: LED dimming (breathing lights, illumination), DC fan speed control, and servo control (via pulse-width mapping).
Not suitable for protocols like WS2812 that require strict timing; use the RMT peripheral instead.
LEDC supports multiple channels, but the number is limited, and frequency and resolution trade off against each other.
02 Hardware
MCU: ESP32 series, built-in LEDC controller, up to 16 channels (see chip datasheet).
Power: Logic at 3.3V; high-current loads need a separate supply, with common ground.
GPIO: Any GPIO can output PWM, but verify LEDC support (see schematic).
Driver circuit: High-current loads (e.g., LED strips, fans) must use a MOSFET or driver IC; GPIO only provides the PWM signal.
Servo: Servo power should be separate, with common ground, to avoid resets or damage.
Connectors: Choose terminals or headers based on load current rating.
Layout: Keep high-frequency PWM traces short to avoid interference; thicken high-current paths.
03 Software flow
1. Configure the LEDC timer: choose frequency and resolution (e.g., 5kHz, 13-bit).
2. Bind a channel to a GPIO: call ledc_channel_config to set channel, GPIO, and timer.
3. Set duty cycle: use ledc_set_duty and ledc_update_duty to update the output.
4. Servo control: map angle to pulse width (e.g., 0°~180° to 500~2500us), then convert to duty.
5. Dimming curve: implement exponential or logarithmic curves in software for natural brightness changes.
6. Fan thermal control: read temperature sensor, adjust duty via PID or lookup table.
7. Error handling: check API return values to ensure configuration success.
04 Core points
Resolution and frequency share a budget: Higher frequency reduces resolution (e.g., with 80MHz clock, 1kHz gives 16-bit, 10kHz gives 13-bit).
Channels share timers: Multiple channels can share a timer, but frequency must be the same; otherwise, use another timer.
Duty update requires explicit call: After setting duty, you must call ledc_update_duty for it to take effect.
Not for WS2812: Timing is too strict; use RMT.
Custom work: Dimming curve, fan thermal algorithm, servo travel mapping.
Watch GPIO muxing: Some GPIOs may be used by other peripherals; check the schematic.
中文
传感器与外设
LEDC PWM
调光、风扇、舵机。用硬件 PWM 而不是软件翻转。
传感器与外设PWMESP32 family
官方路径: examples/peripherals/ledc
01 项目概述
LEDC 是 ESP32 的硬件 PWM 外设,通过定时器产生精确的方波信号,无需 CPU 干预。
它解决软件翻转 GPIO 占用 CPU 且抖动大的问题,适合需要稳定频率和占空比的应用。
典型应用:LED 调光(呼吸灯、照明)、直流风扇调速、舵机控制(通过脉宽映射角度)。
不适用于 WS2812 等需要严格时序的协议,这类应使用 RMT 外设。
LEDC 支持多通道,但通道数有限,且频率和分辨率互相制约。
02 项目硬件描述
MCU: ESP32 系列,内置 LEDC 控制器,最多 16 通道(具体见芯片手册)。
电源: 逻辑电源 3.3V,大电流负载需独立电源,且与逻辑共地。
GPIO: 任意 GPIO 均可输出 PWM,但需确认是否支持 LEDC 输出(见原理图)。
驱动电路: 大电流负载(如 LED 灯带、风扇)必须使用 MOS 管或驱动 IC,GPIO 仅提供 PWM 信号。