Arduino Tutorial 2# LED Light fade on and off.

How to fade on & off LED with Arduino

If you are visual guy you can watch this tutorial in video mention below this lesson.

In this lesson we will see how to make LED light fade on and off. If you have learned my previous lesson 1 then you will have to change only sketch (programming).

All the parts, circuit diagram, assembly remain same as previous lesson LED blinking to see that click here.

After assembly you need to change sketch (programming).

int led_light = 6;
 int brightness = 0;
 int fadeamount = 5;

void setup() {


void loop() {
 brightness = brightness + fadeamount;
 if (brightness == 0 || brightness == 100 )
 {fadeamount = -fadeamount ;}

  1. first we need to store pinmode number which is our output in this sketch is 6.
  2. at initial position LED brightness is zero
  3. fadeamount is 5. it means it will take 5 value per delay.
  4. this is analogWrite you can give any value. As shown in graph brightness is 0 to 100. there is condition if brithness is 0 then fadeamount will add i.e +5 till 100 when it reach 100 fadeamount will become negative i.e -5 and it will bring to 0 birghtness.
  5. This  is|| OR Boolean operator. If either a or b is true. In our case if brightness zero or brightness 100 is true.  Then accordingly fadeamount will switch.
  6. the delay time is a time between each fadeamount take place means 100/5=20 so it will take total 20 steps. 10 for reaching birghtness 100 and 10 for reaching again 0 from 100 brightness.
  7. i have given delay time 20 millisecond so you can calculate the one cycle time of fade on and off. total step X delay time= 20 X 20 = 400 millisecond.
  8. You can give any value for brightness, fadeamount, and delay time but make sure brightness value should be divisible by fadeamount value so that you get both fade on and fade off properly. Otherwise you will get either fade on or fade off. Only one you will get.
Note: for teaching purpose i have taken 0 to 100. you should take 0 to 255. beacuse at 0 value voltage will be 0 volt & at 255 value voltage will be 5 volt.


LIST OF COMPONENT BUY ONLINE:   (Arduino)  (Breadboard)   (Jumper wire)  (Resistor)   (LED)


Leave a Reply

Your email address will not be published. Required fields are marked *