How to Fade an LED
Introduction
|
In this example you will learn how to fade an LED by using analogWrite()function. AnalogWrite uses pulse width modulation (PWM), turning a digital pin on and off very quickly, to create a fading effect.
Before we start let's learn more about PWM |
About PWM
Pulse Width Modulation, or PWM, is a technique for getting analog results with digital means. The average value of voltage (and current) fed to the load is controlled by turning the switch between supply and load on and off at a fast rate. The longer the switch is on compared to the off periods, the higher the total power supplied to the load. Just imagine a switch to open and close very quickly.
|
Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called the pulse width. To get varying analog values, you change, or modulate, that pulse width. If you repeat this on-off pattern fast enough with an LED for example, the result is as if the signal is a steady voltage between 0 and 5v controlling the brightness of the LED.
|
The Arduino UNO it has 6 digital pins that can be used as PWM outputs (3, 5, 6, 9, 10, and 11). The Arduino can send PWM signal with the analogWrite() function. A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example. (see above image )
What you will need - Hardware
For this tutorial you will need:
If you would like you can also use breadboard shield for Arduino UNO. The Circuit
The connections are pretty easy, see the image above with breadboard circuit schematic.
The code
Note:: Keep in mind that setup( ) routine runs only once after power on / re-program or press the reset button. In the program below, the first thing you do is to initialize pin 3 as an output pin with pinMode( ) function in setup( ) routine.
*******************************************
*
Fade
This example shows how to fade an LED on pin 3
using the analogWrite() function.
*/
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 ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
*******************************************
In order to fade your LED off and on, gradually increase your PWM value from 0 (all the way off) to 255 (all the way on), and then back to 0 once again to complete the cycle. In the sketch below, the PWM value is set using a variable called brightness. Each time through the loop, it increases by the value of the variable fade Amount.
Try changing the value of the delay by clicking 'Edit' button and see how it changes the program. Well done!
You have successfully completed our second Arduino "How to" tutorial and you learned how to:
I hope you liked this, let me know in the comments. |
|
Comments
Post a Comment