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
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.
What you will need
For this tutorial you will need:
- Arduino uno
- 220 Ohm resistor
If you would like you can also use breadboard shield for arduino uno.
As we mentioned in the previous tutorial “How to blink an led”, common leds have two pins. The positive end of a led (larger pin) is called anode, and the negative end is called cathode. (see image above)
The connections are pretty easy, see the image above with breadboard circuit schematic.
Here’s the Fade code, embedded using codebender!
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.
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 calledbrightness. Each time through the loop, it increases by the value of the variable fadeAmount.
Try changing the value of the delay by clicking ‘Edit’ button and see how it changes the program.
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