ESP32 上做调光、调舵机、驱动蜂鸣器变调,最后都落在同一件事上:生成一路频率和占空比都可控的 PWM。ESP32 内部有专门的 LED PWM 控制器,可用通道数依型号从 6 到 16 个不等,每个通道都能独立配置频率和分辨率。这篇按官方库文档把两条路都走一遍:省事的 analogWrite,和可控性更强的 LEDC API,示例是让 LED 做呼吸灯。

电路:GPIO16 接一颗 LED
示例电路很简单:LED 正极经 330 欧电阻接 GPIO16,负极接 GND。源文特别说明所有能当输出的 GPIO 都能输出 PWM,换引脚不用改思路。面包板搭好后先确认板子是 ESP32 开发板加对应分区表,别用成 ESP8266 的配置。

第一条路:analogWrite 三行搞定
新版 arduino-esp32 核心把 Arduino 风格的 analogWrite 直接搬了过来,占空比 0 到 255,写法和其他 Arduino 板一致。想改频率和分辨率,再配 analogWriteFrequency 和 analogWriteResolution 两个函数,分别是按引脚设置频率和位数的写法。下面的示例让亮度在 0 到 255 之间往复,形成呼吸效果:
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-pwm-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO 16
void setup() {
// set the LED as an output
pinMode(ledPin, OUTPUT);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
analogWrite(ledPin, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
analogWrite(ledPin, dutyCycle);
delay(15);
}
}
第二条路:LEDC API 才是主战场
analogWrite 背后就是 LEDC,但要控频率、控通道、控分辨率,直接用 LEDC 函数更清晰。新版核心的用法是先 ledcAttach(引脚, 频率, 分辨率) 绑定,通道自动分配;需要固定通道时用 ledcAttachChannel 指定第几个通道,之后用 ledcWrite(引脚, 占空比) 改占空比。示例把频率设 5000Hz、分辨率 8 位:
/*
Rui Santos & Sara Santos - Random Nerd Tutorials
Complete project details at https://RandomNerdTutorials.com/esp32-pwm-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*/
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16
// setting PWM properties
const int freq = 5000;
const int resolution = 8;
void setup(){
// configure LED PWM
ledcAttach(ledPin, freq, resolution);
// if you want to attach a specific channel, use the following instead
//ledcAttachChannel(ledPin, freq, resolution, 0);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledPin, dutyCycle);
delay(15);
}
}这里有个换算关系要心里有数:LED PWM 控制器共用一路时钟,频率和分辨率互相牵制,分辨率越高能设的最大频率越低。8 位分辨率配 5kHz 很从容,但如果你想要 12 位精度还想要 40kHz,就得查官方文档里频率与位数的对应表做取舍。

什么时候用哪条路
只做调光、风扇调速这类单路简单输出,analogWrite 最省事,代码可读性也高。要同时驱动多路且各路频率不同(比如舵机 50Hz 加蜂鸣器 2kHz),必须走 LEDC,否则同组通道的频率会互相影响。ledcAttach 返回 false 时先查频率分辨率组合是否越界,再查通道是否耗尽,这两个原因占九成。烧录验证时把呼吸灯的 delay 从 15 调到 5,呼吸会明显变快,这是占空比步进节奏最直观的验证方式。











呼吸灯delay改成5瞬间变快,验证节奏这招好用
多路不同频率这个坑踩过,舵机和蜂鸣器共用通道直接乱叫
ledcAttach通道自动分配确实省心,之前手算通道号的时代过去了
频率和分辨率的牵制关系那段讲清楚了,12位下高频确实上不去