const int ledPin = 2; // PWM生成后實際輸出引腳
//設置PWM參數
const int freq = 4000;//PWM頻率
const int ledChannel = 0;//信號生成GPIO
const int resolution = 12;//12位分辨率,by Jason. 可設置1~14bits
void setup(){
//PWM參數設置
ledcSetup(ledChannel, freq, resolution);
//將生成信號通道綁定到輸出通道上
ledcAttachPin(ledPin, ledChannel);
}
void loop(){
//逐漸變亮,12位分辨率因而到4096
for(int dutyCycle = 0; dutyCycle <= 123;dutyCycle++){
// changing the LED brightness withPWM
ledcWrite(ledChannel, dutyCycle);
delay(20);
}
//逐漸變暗
for(int dutyCycle = 123; dutyCycle >= 0;dutyCycle--){
// changing the LED brightness with PWM
ledcWrite(ledChannel,dutyCycle);
delay(20);
}
}
|