LED Brightness Control Using Potentiometer

Solving Approach:

How do you plan to solve it?

Initially, I  have assigned A0 pin as input pin which read divider voltage by constant R and Potentiometer. Initialize pin 3 as output which supports PWM output. 

To determine the range of divider voltage, first reading is read and stored in new max and min variables. Now when upcomming readings are greater than max, max gets updated. Similarly, when upcomming variable is less than min, min gets updated. 

After getting range, convert the divider voltage into scale of 0 to 255 which supports PWM.  Based on obtained value, find actual value of PWM.

 

Code

int pot = A0;
int led = 3;
float max, min;

void setup(){
  Serial.begin(115200);
  pinMode(pot, INPUT);
  pinMode(led, OUTPUT);
  max = analogRead(pot);
  min = max;
}

void loop(){
  float adcval = analogRead(pot);
  // Finding out real range of potentiometer.
  if(adcval > max)
    max = adcval;
  if(adcval < min)
    min = adcval;
  float val = (adcval - min) * 255 / (max - min); //Converting into scale of 0 to 255 (for PWM);
  analogWrite(led, val);
  Serial.print("min = " + String(min) + " | ");
  Serial.print("max = " + String(max) + " | ");
  Serial.println(val);
  delay(500);
}


 

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!