/**
* ESP32 Fade example
*
* See: https://makeabilitylab.github.io/physcomp/esp32/led-fade
*/
const int LED_OUTPUT_PIN = 13; // GPIO 13
const int PWM_FREQ = 5000; // 5 kHz — fast enough to avoid visible flicker
const int PWM_RESOLUTION = 8; // 8-bit resolution: duty cycle ranges from 0 to 255
const int MAX_DUTY_CYCLE = (1 << PWM_RESOLUTION) - 1; // 2^8 - 1 = 255
const int DELAY_MS = 6; // Small delay between steps for a smooth fade
void setup() {
Serial.begin(115200);
// Attach a PWM channel to the pin with the specified frequency and resolution.
// The library automatically assigns an available hardware channel.
ledcAttach(LED_OUTPUT_PIN, PWM_FREQ, PWM_RESOLUTION);
}
void loop() {
// Fade up: ramp duty cycle from 0% (off) to 100% (full brightness)
for (int dutyCycle = 0; dutyCycle <= MAX_DUTY_CYCLE; dutyCycle++) {
ledcWrite(LED_OUTPUT_PIN, dutyCycle);
Serial.print("Duty cycle: ");
Serial.println(dutyCycle);
delay(DELAY_MS);
}
// Fade down: ramp duty cycle from 100% back to 0% (off)
for (int dutyCycle = MAX_DUTY_CYCLE; dutyCycle >= 0; dutyCycle--) {
ledcWrite(LED_OUTPUT_PIN, dutyCycle);
Serial.print("Duty cycle: ");
Serial.println(dutyCycle);
delay(DELAY_MS);
}
}