In this tutorial we are going to show, how to fade an LED using Arduino…

This is so easy as reading A B C D in early school. Here we will know the use of analogWrite() function in Arduino which uses PWM (Pulse Width Modulation) feature to make the effect.

 

Required components:

  1. Arduino UNO
  2.  LED
  3. 220 ohm resistor
  4. connectors

Steps

  1. Connect the positive (longer) leg of LED to output pin 9 via a 220 ohm resistor and negative (shorter) to GND.
  2. Compile the given arduino code & upload it to the board.
  3. If successfully uploaded, LED will start fading.

Schematic:

Arduino code:

int led = 9;
int brightness = 0;
int fadeAmount = 10;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led, brightness);
brightness = brightness + fadeAmount;
if (brightness <= 0 || brightness >= 255) {
fadeAmount = -fadeAmount;
}
delay(40);
}

Leave a Reply

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