const int PWM_CHANNEL = 0; // ESP32 has 16 channels which can generate 16 independent waveforms
const int PWM_FREQ = 1000; // Recall that Arduino Uno is ~490 Hz. Official ESP32 example uses 5,000Hz
const int PWM_RESOLUTION = 8; // We'll use same resolution as Uno (8 bits, 0-255) but ESP32 can go up to 16 bits
// The max duty cycle value based on PWM resolution (will be 255 if resolution is 8 bits)
const int MAX_DUTY_CYCLE = (int)(pow(2, PWM_RESOLUTION) - 1);
const int LED_1_OUTPUT_PIN = 18;
const int DELAY_MS = 4; // delay between fade increments
void setup() {
//25% brightness
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
ledcAttachPin(LED_1_OUTPUT_PIN, PWM_CHANNEL);
}
void loop() {
//since freq is 1000, it means it does 1000 actions per 1000 milliseconds, meaning 1 action per ms.
//to make it run through to the max duty circle in 5 seconds, i divided 5/1000 to get 0.005.
for (float dutyCycle = 0; dutyCycle <= MAX_DUTY_CYCLE; dutyCycle+=0.005) {
ledcWrite(PWM_CHANNEL, dutyCycle);
}
for (float dutyCycle = 255; dutyCycle >= 0; dutyCycle-=0.005) {
ledcWrite(PWM_CHANNEL, dutyCycle);
}
}