Fading LED with Arduino: A Step-by-Step Guide

LED lights are widely used in electronics projects, and one of the popular effects that can be achieved with LEDs is fading. Fading LED refers to the gradual change in brightness of an LED light over a certain period of time. In this guide, we will show you how to build a fading LED project using an Arduino board and control the brightness of an LED using pulse-width modulation (PWM).

Materials and Tools:
  • LED (Light-Emitting Diode)

Circuit setup:

To build a fading LED project with Arduino, we need to set up the circuit as shown in the diagram below. Connect the Anode (long leg) of the LED to a 220-ohm resistor and the other end of the resistor to digital pin 9 on the Arduino board. Connect the Cathode (small leg) of the LED to GND on the Arduino board.

Read More: Arduino: Unleashing the Power of Interactive Electronics

Programming:

To control the brightness of the LED, we need to write the code in the Arduino programming language (C++). The code below will gradually increase the brightness of the LED and then decrease it repeatedly.

void setup() 
{
  pinMode(9, OUTPUT); //Setting PinMode As output
}

void loop() 
{
  for(int i = 0; i <= 255; i++) //Turning LED On With PWM
  {
    analogWrite(9, i);
    delay(10);
  }
  for(int i = 255; i >= 0; i--) //Turning LED OFF With PWM
  {
    analogWrite(9, i);
    delay(10);
  }
}

Testing and Demonstration:

Once the code is uploaded to the Arduino board, the fading LED should start to work. You should see the LED gradually increasing and decreasing in brightness. If everything is working as expected, you can further modify the code to achieve different fading effects.

Conclusion:

In this guide, we have shown you how to build a fading LED project with Arduino. By controlling the duty cycle of a PWM signal, we can achieve the desired brightness of an LED. This project is simple and fun to build, and can be used as a starting point for more advanced projects.

Reference and further reading:

Arduino official website

Arduino Project Hub

Pulse Width Modulation (PWM) tutorial

I hope this guide has helped you learn more about fading LED with Arduino. Happy building!
Spread The Knowledge 👨‍💻

Leave a Reply