Tuesday, 10 February 2015

#7 Arduino UNO - Program to Fade the LED

This is 9th post on Arduino tutorial.
If you want the list of posts click here.

Now we are somewhat comfortable with Arduino, I guess ! :-P
We are still using LED but instead of using it to switch ON and OFF, we will control the brightness of LED, fade it. [not by using any potentiometer :-P].

Lets program our Arduino to do this.
  • Download the ino file written by me from here.
  • Now all you need is a LED, resistor [to protect the LED from burning] & Arduino board. Connect all the components as shown in the figure above. Connect the Arduino Board to your computer & by selecting a proper COM port burn the ino file you downloaded.
  • You can see the LED fades ON & OFF [you can do this in a dark room for better visibility :-P]
  • So how without any extra hardware like potentiometer, variable resistor etc did we do this. The circuit is just same as it was before and what changes in code made this happen ?
  • This is what makes micro-controller special, programming things in a proper way can do a lot many things with the same hardware as it was before, programming is that powerful.
  • Actually what we did was we applied the voltage to LED using PWM & hence the LED fades ON & OFF.
  • PWM was explained to you all in previous tutorial, click here to read it again.
  • In this code we are actually dividing voltage into 255 parts, and applying it in parts. We declare a variable which stores the brightness value to be applied, which is incremented continuously & decremented after 255.
  • The variable that stores the value is burned onto the LED pin with a command analogWrite()
    • analogWrite(pin no, value);
    • Make sure you choose the PWM enabled PIN on arduino, i.e. pins with '~' symbol. 
  • analogWrite() puts the value continuously on to LED pin and LED fades.
  • Follow the comments given in the code, which will explain the code step by step.
  • Same code is used to apply PWM to a motor [not directly though :-P], we apply this code to enable pin of a motor driving IC. PWM has many applications other then for LED & motors, we will discuss those when we encounter them.
Since now you all can turn ON & OFF the LED, also you can fade them, you can really make exciting things from LED's [could be a Ganapti or Diwali lighting's ;) :-P].

Try different things, make sure you understand every bit of code. If you have any problems understanding the code please do comment.

Visit the next post on 'Arduino UNO - Basic Functions in Programming'.
click here.

Follow the blog, 
Hit like on the Facebook page and stay tuned :)
Comments are welcomed :)
Thank You.