Arduino Tutorials:Fading LED Program in Arduino IDE

Published by sandesh on

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;
 }
delay(50);
}

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).

Next Post:Arduino and DC motor using L293D

Previous Post:Reading analog data from temperature sensor (LM35)


1 Comment

Arduino Tutorials:Reading analog data from temperature sensor (LM35) - projectsflix · January 3, 2021 at 6:29 pm

[…] Next Post:Fading LED Program in Arduino IDE […]

Leave a Reply

Avatar placeholder

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.