Arduino: Fading LED

This entry is part 2 of 9 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

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.

/*
 * 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);
}
Series Navigation<< Arduino: Basic Servo ControlArduino: Multi Style Blinking LED >>

One comment