// Make sure you're using the ESP32 board
const int pwmPin = 23; // GPIO 23 for PWM output
// PWM properties
const int pwmFrequency = 5000; // 5 kHz frequency
const int pwmChannel = 0; // PWM channel 0
const int pwmResolution = 8; // 8-bit resolution (0-255)
// Brightness and fading variables
int brightness = 0;
int fadeAmount = 5;
void setup() {
// Initialize PWM channel
ledcSetup(pwmChannel, pwmFrequency, pwmResolution);
// Attach the PWM channel to GPIO pin
ledcAttachPin(pwmPin, pwmChannel);
}
void loop() {
// Set the brightness using the PWM channel
ledcWrite(pwmChannel, brightness);
// Update brightness value
brightness += fadeAmount;
// Reverse fading direction at limits
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
// Delay for a smooth fade effect
delay(30);
}