const int PWM_CHANNEL = 0; // ESP32 has 16 channels which can generate 16 independent waveforms
const int PWM_FREQ = 500; // Recall that Arduino Uno is ~490 Hz. Official ESP32 example uses 5,000Hz
const int PWM_RESOLUTION = 10;
// The max duty cycle value based on PWM resolution (will be 255 if resolution is 8 bits)
const int MAX_DUTY_CYCLE = (0x01 << PWM_RESOLUTION) - 1;
const int LED_OUTPUT_PIN = 18;
const int DELAY_MS = 5; // delay between fade increments
void setup() {
// Sets up a channel (0-15), a PWM duty cycle frequency, and a PWM resolution (1 - 16 bits)
// ledcSetup(uint8_t channel, double freq, uint8_t resolution_bits);
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
// ledcAttachPin(uint8_t pin, uint8_t channel);
ledcAttachPin(LED_OUTPUT_PIN, PWM_CHANNEL);
}
void loop() {
delay(500);
// fade up PWM on given channel
for(int dutyCycle = 0; dutyCycle <= MAX_DUTY_CYCLE; dutyCycle++){
ledcWrite(PWM_CHANNEL, dutyCycle);
delay(DELAY_MS);
}
delay(500);
// fade down PWM on given channel
for(int dutyCycle = MAX_DUTY_CYCLE; dutyCycle >= 0; dutyCycle--){
ledcWrite(PWM_CHANNEL, dutyCycle);
delay(DELAY_MS);
}
}