Fading LED with Arduino

Fading LED is an Arduino Application, that one can start with while getting started with the Arduino Practical Application. This is a small but interesting project to start with and will give you a hands-on experience of using the Arduino Board.

See LIVE demo of the Project- Click here to see

This small application gives you the idea of how the PWM(Pulse Width Modulation) Feature of the Arduino can be used.

Concept of PWM(Pulse Width Modulation)

Generally, the Digital I/O Pins on the Arduino Board are purely digital pins, that is, they can accept or output either a HIGH or LOW voltage. But there are some scenarios where we require to accept analog voltage (such as from sensors) or give an analog output.

Therefore, some Digital I/O Pins are marked with a sign(~) on the Arduino Board. In the case of Arduino UNO Rev 3 board, the Digital PINS- 3,5,6,9,10 and 11 are marked with (~), which means these PINS can be used for Pulse Width Modulation.

Requirements

Hardware Components Required :

  • Arduino UNO Board
  • One LED
  • Bread Board
  • One 220 ohm resistor
  • jumper wires.

Software Required

  • Windows 32 OS or higher.
  • Arduino Software.

A Brief Description of LED

arduino-led-details

In the diagram, you can see that the Positive terminal is a little larger length then than the negative terminal. This is the way to figure it out, the terminals of the LEDs. (Any LED brought from the market)

Steps to prepare the project

Step 1: Preparing the Circuit Connections

Connect all the components as shown below:

  • The Negative Terminal of the LED is connected to the GND of the Arduino Board.
  • The Positive Terminal of the LED is connected to the PIN 9 (PIN that has PWM feature) of the Arduino Board via a 220-ohm Resistor.

arduino-fading-led

Step 2: Writing the Fading LED Sketch

To write the sketch(Program) for the Fading LED, just open the Arduino Software And Open a new empty Sketch. Edit the following code into the Arduino IDE.

int led = 9; 
int brightness = 0; 
int fadeAmount = 5; 
void setup() {
    pinMode(led, OUTPUT);
}
void loop() {
    analogWrite(led, brightness);
    brightness = brightness + fadeAmount;
    if (brightness <= 0 || brightness >= 255) {
        fadeAmount = -fadeAmount;
    }
    delay(20);
}

Program Analysis :

  • The variable called led is declared and assigned the PIN 9 (which is a PWM Pin).
  • The variable brightness is declared and initialized to 0.
  • The variable fadeAmount is used to indicate that how much value should the brightness value of the LED should be increased at a time. The voltage level is divided from 0(means a LOW) to 255(means a HIGH). The fadeAmount is 5, means, each time the voltage level increases or decreases by 5 unit between 0 to 255.
  • The line brightness= brightness + fadeAmount; will increase the brightness of the LED by 5 points, every time the loop iterates.
  • The condition if(brightness<=0 || brightness>=255) { fadeAmount= -fadeAmount; } ,will set the fadeAmount to -fadeAmount when the brightness reaches more than 255 or more. And similarly, set the fadeAmount to -(-fadeAmount) which means +fadeAmount, when the brightness reaches less than or equal to zero. This is done to maintain the LED glowing or fading.

The next step is to verify and then upload the code.

FAQ

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions