0

I have an RGB led and I (just) would like to dim is brightness programmatically.

I searched Google thoroughly without any interesting code. Some of them present only "how to" fade from on to another color.

If I change PWM value from 0 to 255, I can easily change color, but, my need is to change the brightness value from potentiometer (through read from analog pin).

1

To control the brightness of an LED with a POT read the pot position using the ADC, convert the range of values from 0 - 1024 to 0 - 255 then write that to the PWM controlling the LED.

This segment of code came from this discussion:

void loop()
{
  knobvalue = analogRead(potpin);
  knobvalue = map(knobvalue, 0, 1023, 0, 255);
  analogWrite(11, knobvalue);
}

This is just for 1 LED. If you have 3 LEDs connected to 3 Arduino PWM pins, you will have to write the same value to all 3 pins.

If this is 1 RGB LED and you are controlling each of the 3 LEDs with 3 Arduino PWM pins, this will (for most RGB LEDs) cause the RGB LED to always appear White as you are varying all colors by the same amount.

If you are trying to vary the brightness of an RGB LED that is set to an arbitrary color... this is more difficult:

Using 3 variables ranging from 0 to 255 pick your color. Before writing these to the 3 PWMs which control the RGB LED, multiply each by the value read from the ADC connected to the POT. Now divide each by the maximum possible ADC value. Take care you do not exceed the range of your variables! Write these new values to the 3 PWMs which control the RGB LEDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.