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).

RSM
1,4571 gold badge11 silver badges26 bronze badges
asked Jul 10, 2016 at 14:37

1 Answer 1

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.

answered Jul 10, 2016 at 15:11

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.