// Pin for the LED
const int LED_PIN = 2;
// Fade delay time in milliseconds
const int FADE_DELAY = 10;
// LED brightness
int brightness = 0;
// Fade direction
int fadeDirection = 1;
void setup() {
// Initialize LED pin as an output
pinMode(LED_PIN, OUTPUT);
// Set up PWM on LED pin
ledcAttachPin(LED_PIN, 0);
ledcSetup(0, 5000, 8); // Channel 0, 5 kHz frequency, 8-bit resolution
}
void loop() {
// Fade in and out
brightness += fadeDirection;
// Reverse fade direction at limits
if (brightness <= 0 || brightness >= 255) {
fadeDirection = -fadeDirection;
}
// Set LED brightness using PWM
ledcWrite(0, brightness);
// Delay for fade effect
delay(FADE_DELAY);
}