Write a program demonstrating the use of analog output (Pulse with Modulation- PWM) to fade LED.
Last Updated : 16 Jul 2025
Solution:
int brightness = 0; // Initial brightness level
int fadeamt = 10; // Amount to change brightness
void setup(){
pinMode(6, OUTPUT); // Set pin 6 as output
}
void loop(){
analogWrite(6, brightness); // Set LED brightness
brightness = brightness + fadeamt; // Update brightness level
// Reverse fade direction at limits
if (brightness <= 0 || brightness >= 255) {
fadeamt = -fadeamt; // Reverse fading direction
}
delay(100); // Wait for 100 milliseconds
}
Report an error
Meanwhile you can watch this video
Watch Video