Sensors and I/O

LEDC PWM

Dimming, fans, servos. Hardware PWM, not bit-bang.

Sensors and I/O PWM ESP32 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 而不是软件翻转。

传感器与外设 PWM ESP32 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 信号。
  • 舵机: 舵机电源需单独供电,且与逻辑共地,否则可能造成复位或损坏。
  • 连接器: 根据负载类型选择端子或排针,注意电流容量。
  • 布局: 高频 PWM 走线尽量短,避免干扰;大电流路径加粗。

03 项目软件流程描述

1. 配置 LEDC 定时器:选择频率和分辨率(如 5kHz、13 位)。

2. 绑定通道到 GPIO:调用 ledc_channel_config 设置通道、GPIO、定时器。

3. 设置占空比:使用 ledc_set_duty 和 ledc_update_duty 更新输出。

4. 舵机控制:将角度映射到脉宽(如 0°~180° 对应 500~2500us),再转换为占空比。

5. 调光曲线:可软件实现指数或对数曲线,使亮度变化更自然。

6. 风扇温控:读取温度传感器,通过 PID 或查表调整占空比。

7. 错误处理:检查 API 返回值,确保配置成功。

04 项目核心点

  • 分辨率和频率互相占预算: 频率越高,分辨率越低(如 80MHz 时钟,1kHz 时可达 16 位,10kHz 时仅 13 位)。
  • 通道共享定时器: 多个通道可共用定时器,但频率必须相同,否则需另配定时器。
  • 占空比更新需调用 update 函数: 设置 duty 后必须调用 ledc_update_duty 才生效。
  • 不适用于 WS2812: 时序要求严格,应使用 RMT。
  • 可定制: 调光曲线、风扇温控算法、舵机行程映射。
  • 注意 GPIO 复用: 某些 GPIO 可能被其他外设占用,需查原理图。