All submissions

LED Brightness Control Using PWM

Solving Approach:

How do you plan to solve it?

  • PWM Output
    • Use analogWrite(pin, value) (0–255 duty cycle range) to control brightness.
    • The LED must be connected to a PWM-capable pin (like D3, D5, D6, D9, D10, D11 on Uno).
  • Timing Calculation
    • Fade up: 2 seconds → brightness goes from 0 → 255
      • Step delay = 2000 ms / 255 ≈ 7.8 ms per increment
    • Fade down: 1 second → brightness goes from 255 → 0
      • Step delay = 1000 ms / 255 ≈ 3.9 ms per decrement
  • Looping
    • Continuously run the fade-up and fade-down in the loop() function.

 

Code

/*Paste your code here*/
const int ledPin = 9;  // Connect LED (with resistor) to PWM pin D9

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // Fade UP: 0 → 255 in 2 seconds
  for (int brightness = 0; brightness <= 255; brightness++) {
    analogWrite(ledPin, brightness);
    delay(2000 / 255);  // ~7.8 ms per step
  }

  // Fade DOWN: 255 → 0 in 1 second
  for (int brightness = 255; brightness >= 0; brightness--) {
    analogWrite(ledPin, brightness);
    delay(1000 / 255);  // ~3.9 ms per step
  }
}



 

Output 

Video50+ Free Arduino & Microcontroller Images - Pixabay

Add video of output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 

 


 

Submit Your Solution

Note: Once submitted, your solution goes public, helping others learn from your approach!