Blog

Arduino: Fading LED

This entry is part 2 of 12 in the series All About Arduino

This program will give you the control of changing the brightness of externally connected LED to your Arduino UNO dev. board.

Connection Details

Check the following breadboard connections given in the project photo. Here two LEDs are connected in parallel with a single 330 ohm resistor in series with both LEDs.

Fading LED with Arduino
Fading LED with Arduino Connection setup of Fading LEDs on breadboard
(observe the 330 ohm resistor in series with LEDs)

Tested Code

Use the following code for this project. You can change the value of “fadeamount” and delay(100); as per your requirement.

The lab tested code is given below. You can also download the text file of this code. Copy the complete code and then paste it into your Arduino Software by creating new project.

/*
 * Program of fading LED using Arduino UNO
 * 
 * Vidyasagar Academy, Akola
 * www.vsagar.org
 */

 int LED = 9;          // the LED attached to pin-9
int brightness = 0;    // The variable for controlling the brightness of LED
int fadeAmount = 5;    // dimming amount of the LED

void setup() // we declare input/output pins in void setup
{
  pinMode(LED, OUTPUT);
}

void loop() // infinite loop 
{
  analogWrite(LED, brightness);

brightness = brightness + fadeAmount;

  if (brightness <= 0 || brightness >= 255) 
  {
    fadeAmount = -fadeAmount;
  }
  delay(100);
}

Comments on this entry are closed.

  • Dattaraj Vidyasagar

    November 26, 2018, 8:49 PM

    Note for students: Please keep the value of brightness=0 in above given program. Do not increase or decrease this value. It will not give you proper effect of fading of the LED