CCS News

Programming RGB LEDs

Tuesday 22 September, 2026

RGB LEDs work by combining three different colors of light - red, green, and blue - to create a wide range of colors. An RGB LED has three tiny light-emitting diodes (one for each color) inside a single unit. By adjusting the brightness of each diode, the RGB LED can produce various colors, including white. Below is the schematic for an RGB LED.

With the specific RGB LED that we used, the red, green and blue LED chips are different brightnesses when operated at the same current. To achieve similar brightnesses for the red, green, and blue chips in the LED package, we chose to use three different resistance values for R1, R2, and R3.

RGB LED schematic

The brightness can also be adjusted by feeding a PWM signal to the LED and varying the duty cycle. The signal is always 0 to Vdd but the time the signal is at Vdd will vary depending on the duty. For example, 100% duty is full brightness and 0% is off.

Initialization of the PWMs on a PIC18F55Q43:

//0 Turns LED on (100%), 32000 turn LED off
#define PWM_RGB_DEFAULT_DUTY 0

//Setup RGB PWM pins
set_pwm1_duty(1, PWM_RGB_DEFAULT_DUTY);
set_pwm2_duty(1, PWM_RGB_DEFAULT_DUTY);
set_pwm3_duty(1, PWM_RGB_DEFAULT_DUTY);

//Setup one of the 2 output slices that the Dual 16-bit PWM module has
setup_pwm1_slice(1, PWM_STANDARD);
setup_pwm2_slice(1, PWM_STANDARD);
setup_pwm3_slice(1, PWM_STANDARD);

setup_pwm1(PWM_ENABLED | PWM_CLK_FOSC, PWM_RGB_PERIOD - 1, 1); //1KHz PWM
setup_pwm2(PWM_ENABLED | PWM_CLK_FOSC, PWM_RGB_PERIOD - 1, 1);
setup_pwm3(PWM_ENABLED | PWM_CLK_FOSC, PWM_RGB_PERIOD - 1, 1);


Setting the color and brightness:

duty1 = ((uint32_t)PWM_RGB_PERIOD * g_LEDColor.red) / 255;
duty2 = ((uint32_t)PWM_RGB_PERIOD * g_LEDColor.green) / 255;
duty3 = ((uint32_t)PWM_RGB_PERIOD * g_LEDColor.blue) / 255;

duty1 = (PWM_RGB_PERIOD - (((uint32_t)duty1 * g_LEDBrightness) / 100));
duty2 = (PWM_RGB_PERIOD - (((uint32_t)duty2 * g_LEDBrightness) / 100));
duty3 = (PWM_RGB_PERIOD - (((uint32_t)duty3 * g_LEDBrightness) / 100));

set_pwm1_duty(1, duty1);
set_pwm2_duty(1, duty2);
set_pwm3_duty(1, duty3);


Watch a video of a project containing programmable RGB LEDs at:
https://www.ccsinfo.com/content.php?page=led-board


Like us on Facebook. Follow us on X (Twitter).

About CCS:

CCS is a leading worldwide supplier of embedded software development tools that enable companies to develop premium products based on Microchip PIC® MCU and dsPIC® DSC devices. Complete proven tool chains from CCS include a code optimizing C compiler, application specific hardware platforms and software development kits. CCS' products accelerate development of energy saving industrial automation, wireless and wired communication, automotive, medical device and consumer product applications. Established in 1992, CCS is a Microchip Premier 3rd Party Partner. For more information, please visit https://www.ccsinfo.com.

PIC® MCU, MPLAB® IDE, MPLAB® ICD2, MPLAB® ICD3 and dsPIC® are registered trademarks of Microchip Technology Inc. in the U.S. and other countries.