Saturday, 30 April 2016

Arduino Tutorial # 3 - Fading a LED with PWM

Fading LED with PWM





Introduction.




In this tutorial we are going learn how to Fade a LED (fade-in and fade-out) by using analog output (Pulse Width Modulation- PWM). We can get a analog like behavior from a digital output of an Arduino by switching it off and on very fast and with different ratio between on and off time. This technique is called Pulse Width Modulation (PWM).



There are 5 pins marked with ‘PWM’ (on some boards as “~” symbol) on most of the Arduino boards next to the pin number. We can use these pins to generate PWM signal.



Parts you will need.



Let's Assemble the parts on a breadboard


  • Connect the positive side / Anode (long leg) to one side of the 220Ω resister and connect the other side of the resistor to Adruino pin 9.
  • Connect the Negative side / Cathode to GND (ground).



Let's see the Sketch


First we have declare 3 variables and initialize them. 



int led = 9; // the PWM pin that the LED connect to

int brightness = 0; //
how bright the LED is

int fadeAmount = 5; // how many points to fade the LED by 




Since we connect the LED to pin 9 we declared it as shown in first line. The brightness variable will store the current brightness value of the LED. fadeAmount is the rate that we are going to fade the LED.(you can experiment by changing this value)

Next we have to set the pin 9 as a output in void setup().


void setup() {

pinMode(led, OUTPUT); // declare pin 9 as an output:




Here we are using pinMode() to set pin 9 as a output pin.


Next we will write the code to loop()

First we will use analogWrite() function to invoke the Pulse Width Modulation capabilities of the Arduino board. 

Pulse Width Modulation



Pulse Width Modulation is a technique to control power output of the pin. Digital output in a arduino will give us 0V or 5V only. But by using PWM we can encode the output into a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between 5V (ON) and 0V (OFF) by changing the portion of the time of the signal spends ON versus the time that the signal spends OFF, this is called duty cycle.

Duty cycle 100% =  5v (always on) - 255 analogWrite()
Duty cycle 50% =   2.5v (on half the time) - 127 analogWrite()


The duration of "on time" is called the pulse width. To get varying analog values we modulate (change) the pulse width. If we repeat this on-off pattern fast with using a LED the result is as if the signal is a steady voltage between 0V and 5V and that can be use to control the brightness of the LED.

we need to tell which pin to modulate and how much power we want to be applied in to analogWrite() function . The scale is from 0 to 255 with zero being the lowest power setting, 127 is the half and 255 being the highest. 




analogWrite() have two arguments first one is pin in this case we have declare it as "led" (pin 9) in our variable section. Other one is value which we declared as "brightness" and the scale is from 0-255 as mention above.


analogWrite(led, brightness);// set the brightness of pin 9:



In next line we take the current value of "brightness" and add the "fadeAmount" to it, then we save this new value back to the brightness variable. Now the ‘brightness’ variable holds the number 5 as we initialize "fadeAmount" as 5 at the beginning  of the code.



brightness = brightness + fadeAmount;// change the brightness for next time through                                                              //the loop:



You can see we are increasing the "brightness" variable by 5 levels every time through the loop so the LED brightness will increase. However we can do this untill we reach the maximum analogWrite(), that is 255. Once it reach to 255 it LED won't fade.So we have to test the value of the "brightness" and change it when it gets to 255.

In order to do that we are going to use "if" statement."if" statement checks a condition and if that condition is met then it do something , if the condition is not met, then it does nothing.

Need  to type the condition inside the parenthesis "()" after the word ‘if’. And inside the curly braces, we have to type the code that you want to execute if the condition is met. If the condition is not met, the instructions are not executed.



if (brightness == 0 || brightness == 255) {fadeAmount = -fadeAmount ; 
}




The "||" is a Boolean Operators "OR" . This condition says, “if the brightness variable equals zero OR if the brightness variable equals 255”.


Noticed that we use a double equal sign. The double equal sign is a comparison operator that asks “are these two values equal?”. If we use a single equal sign, then we would be assigning a value to the brightness variable like we did when we assign "0" at the begging of the code. Here we want to compare! not assigning. 




By assigning a "-" (negative) to fadeAmount variable, the fadeAmount that used earlier to increase the value of the brightness variable will now becomes a negative value so each time through the loop() we will subtract 5 from the brightness variable. Since analogWrite() is decreasing the is LED will start to dim.

Once the brightness variable decreases to zero, it will switch fadeAmount to positive and start the whole process over again.


In finally we have to show down the fading by adding a delay.


delay(30); 
// wait for 30 milliseconds to see the dimming effect




And that's it. Now compile and upload the code to your Arduino.
















No comments:

Post a Comment