Tuesday, November 24, 2015

Arduino Tutorial - How to Fade an LED

The example you analog Write () function by using the LED will learn to fade. Analog Write a fading effect to create a digital pin turn on and off very quickly, pulse width modulation (PWM) uses. Before we start let  learn more about 'PWM

 Step 1: Describe of PWM

Pulse Width Modulation , or PWM, to achieve consistent results with digital means is a technique .
Fed to the load voltage ( and current ) of the average value at a faster rate and by turning off the switch between supply and load is controlled . Now switch off period of high total power supplied to the load . Just to open and close very quickly assume a switch .
Digital control for generating a square wave is used , a signal switched between on and off . The on-off pattern signal spends off that spends time on versus time off by changing the portion ( 5 volts ) and ( 0 volts ) can simulate voltages in between full on . " On time " is called the period of the pulse width .
To get varying analog values , you change the pulse width , or to settle . For example , LED together fairly quickly off the repeating patterns , the result signals to control the brightness of the LED in a constant voltage between 0 and 5 V is as if.
Arduino UNO it can be used as PWM outputs 6 digital pin ( 3 , 5 , 6 , 9 , 10 , and 11 ) . Arduino analogWrite () function can send the PWM signal . ( Half- time ) analogWrite (255) , a 100 % duty cycle ( always on ) requests , and analogWrite (127) such that a 50% duty cycle 255 , for - analogWrite () call for a scale of 0 examples on .
Step 2: What you will need

you will need For this tutorial:
220 Ohm resistor
Breadboard
LED
Arduino uno

If you would like you can also use breadboard shield for arduino uno.
Step 3: The Circuit

We mentioned in the previous tutorial, general LEDs have two pins. An LED (large pin ) is called positive end of the anode , and the cathode is called negative .Connections , are pretty easy with breadboard circuit schematic See above image
Step 4: The Code
Here codebender access code embedded faded , is
Setup ( ) routine / re-program that runs only once after power note or press the reset button Under the program , the first thing you pinMode () setup ( ) routine with the function as an output pin to 3 -pin to start .
Loop ( ) routine forever , and re- runs . The main loop will be used in your code that you analogWrite () function requires two arguments: the task to write a PIN telling , and what to write the value of PWM signals.
 /*
 Fade

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

 This example code is in the public domain.

 More info: http://www.ardumotive.com/how-to-fade-an-led-en.html
 */

int led = 3;           // 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 3 to be an output:
  pinMode(led, OUTPUT);


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

You have successfully completed our Arduino Tutorial - How to Fade an LED. 

I hope you liked this.

No comments:

Post a Comment