Arduino Tutorial 3 : Fading a LED

Hello all ! Today in this blog we will discuss about how to fade an external LED using Arduino through programming. Arduino is an open source electronic platform based on easy to use hardware and software.

Please support us on YouTube also. Like Share and Subscribe to our channel : https://youtube.com/c/RatnasRoboLab

Components

Code

int led = 11;           // the PWM pin 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 11 to be an output:
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  // set the brightness of pin 11:
  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 10 milliseconds to see the dimming effect
  delay(10);
}

Code to Note

At first initialize PIN11 as led and set it as Output.

pinMode(11, OUTPUT) – You have to tell ARDUINO that PIN11 is used as Output or Input. so, here we used the built in function called pinMode().

analogWrite(11, brightness) – We can change the PWM value very fast, at the end of the code the function delay() can control the speed of the fade. So, change it and enjoy the effect.

Here we are gradually increases the PWM value from 0 to 255 and again decreases the value from 255 to 0 and this cycle going on.

Result

In this program the external LED brightness changes gradually. Similarly we can also reprogram the ARDUINO and change the rate at which it fades.

Further Readings

If you liked this article, then please subscribe to our YouTube Channel. You can also find us on InstagramFacebook and Twitter.

READ – CONNECT – BOOST – CREATE

Related :

Follow :