# How to change brightness of LED light?

I'm trying to change the brightness of an LED light with RGB. I can change the brightness of simple colors like red, green, and blue. But I'm not sure how to change brightness of colors like purple, turquoise, and yellow green. When it comes to adjusting the brightness of red, green, and blue. I have tried changing the values inside colorRGB().

With colorRGB():

``````int redPin = 9;
int greenPin = 10;
int bluePin = 11;

void setup(){
pinMode(redPin, OUTPUT);
delay(500);
pinMode(greenPin, OUTPUT);
delay(500);
pinMode(bluePin, OUTPUT);
delay(500);
}
void loop(){
colorRGB(255, 0, 0); //Brightness level 255
colorRGB(150, 0, 0); //Brightness level 150
colorRGB(50, 0, 0); //Brightness level 50
}
void colorRGB(int red, int green, int blue){
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
``````

I have also tried using analogWrite(), however it needs the pin as one of its arguments so I'm not quite sure how to connect that with the already set up colors inside void loop(){}.

To change the brightness of any RGB color you need to multiply each color component by the brightness ratio. Lets look at some examples:

• Red is `255,0,0` in RGB. Full brightness is represented by 1, half brightness by 0.5, quarter brightness by 0.25 and so on. So we get the following values (we are doing integer calculations, so cutting away any decimal digits):
• half brightness: `255*0.5,0*0.5,0*0.5` --> `127,0,0`
• quarter brightness: `255*0.25,0*0.25,0*0.25` --> `63,0,0`
• Yellow is the combination of red and green, so `255,255,0`. Doing the same math as above we get:
• half brightness: `255*0.5,255*0.5,0*0.5` --> `127,127,0`
• quarter brightness: `255*0.25,255*0.25,0*0.25` --> `63,63,0`
• Cyan is the combination of red green and half blue, so `255,255,127`. Doing the same math as above we get:
• half brightness: `255*0.5,255*0.5,127*0.5` --> `127,127,63`
• quarter brightness: `255*0.25,255*0.25,127*0.25` --> `63,63,31`

So you can change your function to include a brightness parameter:

``````void colorRGB(int red, int green, int blue, int brightness_percent){
analogWrite(redPin, red*brightness_percent/100);
analogWrite(greenPin, green*brightness_percent/100);
analogWrite(bluePin, blue*brightness_percent/100);
}
``````

Here I used not the value range from 0 to 1 but instead in percent from 0 to 100. This is because floating point operations on Arduinos are rather time intensive (because the Arduino has no hardware for that and needs to do all that in software) so its better to multiply by the percent value and then divide by 100. So we have only integer calculations which is way faster. The outcome is the same.

Note: As the the color components get in the lower values you might see color errors due to the quantification. Though normally RGB LEDs aren't really color accurate either so probably you won't notice.

• Thank you so much. I was actually thinking about manually converting it with percentages or fractions in relation to the brightness, but I thought I would be wrong. Thank you very much once again. Oct 28, 2021 at 12:40

I just created a simple simulation for the nice answer written. Here is the code:

``````int redPin = 9;
int greenPin = 10;
int bluePin = 11;

void setup() {
pinMode(redPin, OUTPUT);
delay(500);
pinMode(greenPin, OUTPUT);
delay(500);
pinMode(bluePin, OUTPUT);
delay(500);
}
void loop() {
colorRGB(255, 0, 0, 100); //Brightness level 100%
delay(3000);
colorRGB(255, 0, 0, 25); //Brightness level 25%
delay(3000);

colorRGB(255, 255, 0, 100); //Brightness level 100%
delay(3000);
colorRGB(255, 255, 0, 25); //Brightness level 25%
delay(3000);

colorRGB(255, 255, 127, 100); //Brightness level 100%
delay(3000);
colorRGB(255, 255, 127, 25); //Brightness level 25%
delay(3000);
}
void colorRGB(int red, int green, int blue, int brightness_percent) {
analogWrite(redPin, red * brightness_percent / 100);
analogWrite(greenPin, green * brightness_percent / 100);
analogWrite(bluePin, blue * brightness_percent / 100);

}
`````` i hope this is helpful. I am from the Wokwi team and I will welcome all your questions. You can have a look at the Wokwi Arduino simulation link here:

https://wokwi.com/arduino/projects/313797447551812161