LED Brightness Control Using PWM

Solving Approach:

How do you plan to solve it?

My first approach is coding a routine to increase the LED brightness from 0% to 100% using a for cycle where mi iteration variable (i) is also used as a parameter of the function analogWrite(led,i). it worked just fine but I had to match that bright incremet with a proportional time to complete that task in 2 seconds. Then I use a 2000/255 division as a parameter of the delay function achieve that correlation of bright (PWM 0 - 255) and time.

I worked just fine and the last step is doing the same thing for a decrement oof bright in 1 second :) 

Code

/*Paste your code here*/
const int led = 3;

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

void loop() {

  // --- Subida de brillo: 2 segundos (0 → 255) ---
  for (int brillo = 0; brillo <= 255; brillo++) {
    analogWrite(led, brillo);
    delay(2000 / 255);  // ≈ 7.84 ms por paso
  }

  // --- Bajada de brillo: 1 segundo (255 → 0) ---
  for (int brillo = 255; brillo >= 0; brillo--) {
    analogWrite(led, brillo);
    delay(1000 / 255);  // ≈ 3.92 ms por paso
  }

}

Output

Video

Add video of output (know more)

 

 

 

 

 

Photo of Output

Add a photo of your hardware showing the output.

 

 

 


 

Upvote
Downvote

Submit Your Solution

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