What is Fading LED:

If the Brightness of the LED decrease from time to time, it is known as fading LED.In this Tutorial, we will write a code for a led light to blink for a time of ‘a’ milli seconds with some intensity and we will increase the intensity of glowing continously i.e the LED glows with X intensity for few milli seconds and the intensity increases as time flows and after reaching the peak intensity we will decrease the intensity of the light with time till the light turns off and this process repeats.

Code for fading LED :

int led_pin = 9;   
int bright = 0;
int fade = 10;
void setup() {
  pinMode(led_pin, OUTPUT);
void loop() {
analogWrite(led_pin, bright);
 bright = bright + fade;
 if (bright <= 0 || bright >= 255)
   fade = -fade;

Code explaination:

In this code, we gave a variable ‘led_pin’ to 9th pin of arduino , initialized a variable brightness as 0, and fade as 10 (this tells by how much LED should fade). Then we declared ‘led’ as OUTPUT in void setup. In void loop, we dumped brightness value in led, and we are increasing brightness by value of fade. If the value of brightness exceeds 255 or is less than 0, we give value of fade as -fade. This loop repeats for every 50 milli seconds(as we wrote delay(50) at the end of the code).

Categories: ArduinoTutorials


Leave a Reply