How to Fade an LED


Introduction 

Picture
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

Picture

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.



Picture

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:
  • Arduino UNO
  • Breadboard
  • LED
  • 220 Ohm resistor
If you would like you can also use breadboard shield for Arduino UNO.

The Circuit

Picture
The connections are pretty easy, see the image above with breadboard circuit schematic.
Picture
Common LEDs have two pins. The positive end of a led (larger pin) is called Anode, and the negative end is called Cathode.

Arduino UNO board have a built-in LED on pin 13, but in this tutorial we are going to be adding our own on digital pin 3.

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.

The loop( ) routine runs over and over again, forever. The analogWrite() function that you will be using in the main loop of your code requires two arguments: One telling the function which pin to write to, and one indicating what PWM value to write.

*******************************************
*
 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:
  • fade an led
  • use PWM pins and analogWrite() function
  • put variables in your code
  • use 'if' statement
I hope you liked this, let me know in the comments.
Picture





Comments

Popular posts from this blog

Arduino ​Obstacle Avoiding Robot Ardunio Computer

Build Your Own Arduino Weather Station

Complete Guide for Nokia 5110 LCD with Arduino