Arduino Basics - 4

Fading an LED ON/OFF

Creating a Fading Effect

We see how to use an analogwrite() function in order to implement a fading ON/OFF of an LED. The function incorporates PWM pulses across a pinout for accomplishing the suggested fading action over the connected LED.

Hardware Needed

Along with your Arduino, other materials such as a breadboard, an LED and a 220 ohm, 1/4 wat resistor would be required for the experiment.

The Circuit

The steps involved for the proposed LED ON/OFF fading with an Arduino are as follows:

1. Attach the longer terminal of the LED to the digital output pin#9 via the 220 ohmresistor in series, while the cathode or the shorter terminal of the LED with the ground directly or the negative supply rail

The Code

Once pin#9 of the board is integrated as the LED positive pin, the setup() function could be simply left alone and would'nt need any further operations.

The main loop code component in the form of analogwrite() needs a couple of acknowledgments: The first being addressing the function regarding which pin to be used for writing, and the second being the value of the PWM to be determined.

For initiating a fading ON/OFF effect on the connected LED, the PWM could be consistently varied from zero to maximum or to 255 and vice versa, completing the entire single cycle of operation.

The code below shows the PWM magnitude being determined through a variable named as brightness. Also the it increments by variable fadeAmount in the loop.

In a situation where brightness is at the extreme values (either 0 or 255), promptsfadeamount to become negative.

Meaning if suppose the fadeAmount is 5, it changes to -5, and 5 in case it's set a 55. In the later periods in the loop these changes results brightness to vary course of the action as well.

The function analoguewrite() causes quick alterations in the PWM values, such that the delay at the conclusion of the sketch controls the fading speed.
You may experiment with the delay values to investigate the changes produced in the program.

/* Fade

This example shows how to fade an LED on pin 9
using the analogWrite() function.

This example code is in the public domain.
*/

int led =9;// the pin that the LED is attached toint brightness =0;// how bright the LED isint fadeAmount =5;// how many points to fade the LED by

// the setup routine runs once when you press reset:voidsetup(){// declare pin 9 to be an output:pinMode(led,OUTPUT);}

// the loop routine runs over and over again forever:voidloop(){// set the brightness of pin 9:analogWrite(led, brightness);

// change the brightness for next time through the loop: brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade: if(brightness ==0|| brightness ==255){ fadeAmount =-fadeAmount ;}// wait for 30 milliseconds to see the dimming effect delay(30);

We try our utmost best to provide our clients with unique and customize development & design. We create responsive design by HTML & CSS3. Our created web sites are of customize CMS which allows any user or admin to administrate it with comfort..

We are having feedback from our customers which inspire us to create android apps of multi leveled versions. Since clients’ positive responses we are trying to offer the best android apps to the market.

Since our inception of our company we try to give our best for the clients and for their satisfaction and clients also give us positive responses which inspire us to arrange new events for those who are interested in IT sectors.

We try our utmost best to provide our clients with unique and customize development & design. We create responsive design by HTML & CSS3. Our created web sites are of customize CMS which allows any user or admin to administrate it with comfort.

We are having feedback from our customers which inspire us to create android apps of multi leveled versions. Since clients’ positive responses we are trying to offer the best android apps to the market.

Since our inception of our company we try to give our best for the clients and for their satisfaction and clients also give us positive responses which inspire us to arrange new events for those who are interested in IT sectors.