Skip to content

Latest commit

 

History

History
54 lines (36 loc) · 2.22 KB

README.md

File metadata and controls

54 lines (36 loc) · 2.22 KB

LED Fading Project

This project demonstrates how to gradually fade an LED's brightness up and down using Pulse Width Modulation (PWM) on an Arduino.

Components Required

  • Arduino board (e.g., Uno, Nano)
  • LED
  • 220-ohm resistor
  • Breadboard
  • Jumper wires

Circuit Diagram

  1. Connect the longer leg (anode) of the LED to digital pin 9 on the Arduino.
  2. Connect the shorter leg (cathode) of the LED to one end of the 220-ohm resistor.
  3. Connect the other end of the resistor to the GND (ground) pin on the Arduino.

Code Explanation

Variables

  • int led = 9;: This variable stores the pin number where the LED is connected.
  • int brightness = 0;: This variable stores the current brightness level of the LED (from 0 to 255).
  • int fadeAmount = 5;: This variable determines the amount by which the brightness will change each loop iteration.

Setup

  • void setup(): The setup() function runs once when the program starts. Inside this function:
    • pinMode(led, OUTPUT);: This sets the led pin as an output pin, meaning it will be used to control the LED.

Loop

  • void loop(): The loop() function runs continuously after setup(). Inside this function:
    • analogWrite(led, brightness);: This sets the LED's brightness using PWM. The value of brightness can range from 0 (completely off) to 255 (fully on).
    • brightness = brightness + fadeAmount;: This line increments or decrements the brightness value by fadeAmount to gradually change the LED's brightness.
    • if (brightness <= 0 || brightness >= 255) { fadeAmount = -fadeAmount; }: This checks if the brightness value has reached the maximum (255) or minimum (0) and reverses the direction of fading by changing the sign of fadeAmount.
    • delay(30);: This pauses the program for 30 milliseconds to create a smooth fading effect.

How to Use

  1. Connect your Arduino board to your computer.
  2. Open the Arduino IDE.
  3. Copy and paste the provided code into a new sketch.
  4. Upload the sketch to your Arduino board.
  5. Watch the LED fade in and out continuously.

License

This project is open-source and available under the MIT License.


Enjoy experimenting with PWM and LED fading effects!