LED Fading Effect using Arduino – just like your Sleeping Mac



This is going to be really simple, It is a kind of Hello World Project for beginners. You might have observed LED which fades in and out when your Mac is sleeping.So we are going to make that cool effect using Arduino.

All you need is Arduino, LED, 220ohm resistor, A Breadboard and some wires.

Ok, analogLet's get started

Connect the Components as I mention in below Breadboard Diagram



Now Upload the following Program to Arduino

 const int LED = 9;
 int i = 0;
 void setup() {
 pinMode(LED, OUTPUT);
 }
 void loop(){
 for (i = 0; i < 255; i++) { analogWrite(LED, i); delay(10); } for (i = 255; i > 0; i--)
 {
 analogWrite(LED, i);
 delay(10);
 }
 }
The catch here is We need to take advantage of Analog output pins of Arduino to linearly Fade in and Fade out the LED,Arduino UNO has six analog pins -pin 3,5,6,9,10 and 11.

The syntax analogue output function

analogWrite(LED,128);
The above function assigns the value 128 to LED, The LED should be assigned with any one of the Analog output pin.

Analog output has a range of 0 to 255, which represents the voltage 0 to 5 volt respectively.Thats is if you give 0 as a output the LED will not be given any voltage and If it is 255(max value) the output will be 5 V.So you can calculate the resolution as 5/1256= 0.0195 V.

In the loop function I am iterating the for loop two times, One on forward direction by increasing the value of variable i and the other on backward by decreasing the value of i to its minimum value.The forward for loop will Increase the brightness linearly and the reverse for loop will decrease the brightness from maxim to minimum.

Now you can ask me why I am using delay function, It is because the change in output is instantaneous so It won’t appear Linearly if brightness is continuously changed, So I am delaying the next state by 10 milli seconds.



Isn’t that so simple ? but I would Like you to extend this project by adding some Other cool features.

Ok next time I will bang up with some complex Arduino Projects.

Thank you for reading my article, If you wish to support me Please share this article in your Social circles and Subscribe our Newsletter to get Instant updates.



Share this

Related Posts

First