Thursday, 14 May 2015

Arduino lesson 9 : How to fade LED brightness continously :

Intro to topic :

Some times we need that some variable or some analog voltage continuously varies from one value to another one like starting LED from zero brightness to its max and then reverting all the process can be tricky and will be useful for us to handle situations like this.

Hardware required :

  1. Arduino UNO board.
  2. LED (of any color)
  3. Resister 330ohm (not mandatory)
  4. Connecting wires/Jumper wires.
  5. PC to configure arduino board (with arduino software installed).
  6. Bread board.


Fritzing project
Schemetic diagram

Steps to follow :


  1. Connect the Led by identifying the positive lead of led according to Fritzing image
  2. The resister if added good otherwise it is not necessary to put on resister because the current is limited to each pin of arduino boards.
  3. After this connect the board to PC by USB cable and then open up the IDE in PC.
  4. Goto File>Examples>01.Basics>fade
  5. The following Code will open.



Code to be uploaded :

/*
 Fade

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

 This example code is in the public domain.
 */

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

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

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 13:
  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);
}




Once you have uploaded the code correctly just see that Led will start fading and increasing its brightness to zero and max positions.

No comments:

Post a Comment