Subscribe to Arduino Academy and start learning today for FREE!

Multicolor (RGB) LED with Arduino PWM

This is the next step from our last post, where we take the single color example of PWM brightness control, and expand it to a RGB LED with a whole spectrum of color outputs. Instead of having one potentiometer, we now have three. Instead of using one analog ports, we use 3, and the same on the pwm ports.

The wiring is similar, each pot connects to +5 and ground on the outside legs, and each center pin connects to a analog port. The RGB LED has 4 legs, one for each color and a common anode. Arduino pins 7,6, and 5 connect to R, G, and B respectively (through a 270 ohm resistor). The code is similar, just duplicated lines for the other two colors.

Become the Maker you were born to be. Try Arduino Academy for FREE!

Now you can make pink, turquoise, and more. The next step will be to upgrade to a 1 watt RGB LED, which requires 3 power transistors to handle the increased current.

(video uploaded to youtube)


3 Linear Taper 5k ohm potentiometers
3 270 ohm resistors
1 RGB common anode LED
1 Arduino

Download .pde

int sensorPinR = A0;    // red pot
int sensorPinG = A1;    // green pot
int sensorPinB = A2;    // blue pot
int ledPinR = 7;      // red LED
int ledPinG = 6;      // green LED
int ledPinB = 5;      // blue LED
int sensorValueR = 0;  // red variable
int sensorValueG = 0;  // green variable
int sensorValueB = 0;  // blue variable

void setup() {}

void loop() {
// read the value from the sensor:
sensorValueR = analogRead(sensorPinR);
sensorValueG = analogRead(sensorPinG);
sensorValueB = analogRead(sensorPinB);
// converts 0-1023 to 0-255
sensorValueR /=4;
sensorValueG /=4;
sensorValueB /=4;
// outputs PWM signal to LED
analogWrite(ledPinR, sensorValueR);
analogWrite(ledPinG, sensorValueG);
analogWrite(ledPinB, sensorValueB);


Become the Maker you were born to be. Try Arduino Academy for FREE!

Notify of
Inline Feedbacks
View all comments
13 years ago

This information helped me out with a project I am working on. I bought a LCD display that had a multi color back light. I used your example here to get it working properly.

In my project I have a rotary encoder to select the values for ledPin's. I display the value on the screen and use a push button to switch between R-G-B. Thanks again for the info.

11 years ago

I'm curious as to why you chose resistors and not a driver and why you used the same amount of resistance for each lead since each color uses a different amount of power and it's typically stated that you need to use different resistances for each one?

Steve Spence
11 years ago

I don't need a driver to light a LED on a Arduino. I used the same resistors as I had them handy, and the differences are not critical. The KISS principle.

Mr Coolboy
6 years ago

Questions should the pots be connected in series or parallel?

Steve Spence
6 years ago

Neither. each pot center is connected to a unique analog input.

Mr Coolboy
6 years ago

I mean for the positive and negative

Steve Spence
6 years ago

One outer leg of each pot connects to +5, the other outer leg connects to ground.


Would love your thoughts, please comment.x