Friday, 11 November 2016

FADING LED USING PWM:PULSE WITH MODULATION


Fade an LED with Pulse Width Modulation using analogWrite()

IN THIS PROJECT U WILL NEED :- 

1.ARDUINO

2.LED

3.220 OHM RESISTOR

4.JUMPER WIRES

CIRCUIT PICTURE :-  

 

Step-by-Step Instructions

  1. Take the short leg of the LED and insert it in the GND pin.
  2. Take either leg of the resistor and place it in pin 9.
  3. Connect the long leg of the LED with the other leg of the resistor using an alligator clip
  4. Plug the Arduino into your computer with the USB cable
  5. Open up the Arduino IDE

CODE :-
  
  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 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 9 to be an output:
  pinMode(led, OUTPUT);
}
 
// the loop routine runs over and over again forever:
void loop() {
  // 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);
}
 
 6.Click the Verify button (top left). The button will turn orange and then blue once finished.
 7.Click the Upload button. The button will turn orange and then blue when finished.
 
 
EXPLANATION : 
 
Where the real action happens is in loop().
The first function we encounter in the loop() is analogWrite(). This function invokes the Pulse Width Modulation capabilities of the Arduino board. Pulse Width Modulation basically adjusts the power output at the pin. So you can have a lot of power or a little power applied at the pin, it’s your call, just tell the analogWrite() function which pin to modulate and how much power you want to be applied. The scale is from 0 to 255 with zero being the lowest power setting and 255 being the highest.  



                analogWrite(pin, value);
You can utilize analogWrite() with pins 3, 5, 6, 9, 10 and 11 – recall there is a “PWM” or “~” next to the pin number on the board.
In this sketch we use the arguments:
The first thing we do in the loop is write a value to pin 9 (recall that led holds the number 9) where we have our LED attached (through a resistor) – and we set the value to 0 (zero is what our brightness variable initially holds). This will keep our LED dark to start with.
Key Points about the analogWrite function
 
FEEL FREE TO POST ANY DOUBTS AND COMMENT BELOW FOR SUGGESTION .
DO CHECK OUT MY OTHER TUTORIALS AND SUBSCRIBE FOR MORE


 
 
 

No comments:

Post a Comment