Fade in the LED

const int ledPin = 9; // PWM pin connected to the LED

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
}

void loop() {
// Fade in the LED
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // Set the brightness
delay(10); // Delay for smooth fading
}

// Fade out the LED
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // Set the brightness
delay(10); // Delay for smooth fading
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *