ESP32 LED PWM控制器具有16个独立通道,可配置为生成具有不同属性的PWM信号。 我们可以使用ESP32的LED PWM控制器和零知开发工具对LED进行调光。
1、零知ESP32:http://www.lingzhilab.com/home/introduction.html?gid=184
2、220Ω电阻
3、LED
// the number of the LED pin
const int ledPin = 16; // 16 corresponds to GPIO16
const int ledPin2 = 17; // 17 corresponds to GPIO17
const int ledPin3 = 5; // 5 corresponds to GPIO5
// setting PWM properties
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
void setup(){
// configure LED PWM functionalitites
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(ledPin, ledChannel);
ledcAttachPin(ledPin2, ledChannel);
ledcAttachPin(ledPin3, ledChannel);
}
void loop(){
// increase the LED brightness
for(int dutyCycle = 0; dutyCycle <= 255; dutyCycle++){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(5);
}
// decrease the LED brightness
for(int dutyCycle = 255; dutyCycle >= 0; dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel, dutyCycle);
delay(5);
}
}
导入程序代码,在右侧选择ESP32开发板,然后验证程序,如图:
然后上传程序到开发板,最终效果见1楼。