Tricolor (RGB) LED
Updated: 7/1/2025 Words: 0 words Reading time: 0 minutes
In the previous section, we learned how to light up and control monochrome LEDs. In this section, we will learn how to produce any color using RGB LEDs.
Preparation
Hardware | Description | Image |
---|---|---|
Creative Box Platform | ![]() | |
Tricolor LED | Taobao Purchase Link | |
Tricolor LED | Taobao Purchase Link | |
Power Signal Module | Optional, depends on the voltage requirements of the tricolor LED you purchased whether this module is needed |
Video Explanation
Tricolor LED
【Screen】LOGO 【Narration】Sensor Sparks Creative Box, a one-stop electronic DIY creative platform.
【Screen】Text: Tricolor LED Light 【Narration】Next, we will introduce how to control tricolor LED lights using the Creative Box.
【Screen】Tricolor LED Light 【Narration】Tricolor LED lights integrate three monochrome LEDs: red, green, and blue. By combining the brightness of these three colors, you can create more colors.
Operation Steps 【Screen】Hardware Connection Video 【Narration】R stands for Red, G for Green, B for Blue, GND for Ground 【Narration】Use Dupont wires to connect the first signal pin of the Creative Box's pin header to the red pin, the second signal pin to the green pin, the third signal pin to the blue pin, and connect the Creative Box ground to the tricolor LED ground.
【Screen】Using Tricolor LED Application 【Display Text】Taking this application as an example, you can develop exclusive applications for monochrome LEDs to quickly realize your electronic creativity.
Connection
Pins
RGB LEDs have four pins. The three shorter pins control the three colors respectively; the longest pin is common. If it's a Common Anode type, the longest pin is the common positive pole and needs to be connected to a +5V power supply. If it's a Common Cathode type, the longest pin is the common negative pole and needs to be grounded.

But which pin represents which color? Hold the RGB LED with the longest pin on the left. From left to right, the pins are: Red, Common Negative or Positive, Green, Blue.
Usage
How to Change Colors
Next, let's see how to change the LED color using the development board.
The following test code will loop through displaying red, green, blue, yellow, purple, and cyan. Arduino has an analogWrite function that you can use with pins marked with a ~ to output variable power to the corresponding LED.
Principles
Origin of Different Colors
At first glance, RGB LEDs look no different from ordinary LEDs. But in fact, they integrate three different colored light-emitting elements: Red, Green, and Blue, as if three different colored LEDs are packaged together. By controlling the brightness of each individual LED, almost any color can be mixed.

From Red, Green, Blue to Any Color
Red, green, and blue can be mixed to produce any color because the human eye's perception of light is related to the three primary colors of light (red, green, blue).
The human eye has three types of cone cells, which perceive light corresponding to red, green, and blue, respectively. When these three lights are present simultaneously, they are mixed together, and our brain weights and combines the intensities of these lights, thus producing the colors we see.
By adjusting the intensity of red, green, and blue light, we can control their stimulation in the cone cells, thereby producing different visual effects. For example, when the intensities of red, green, and blue light are uniformly equal, we see white light. If we only light up red and green light and turn off blue light, we will see yellow. By appropriately adjusting the intensities of the three lights, we can create various colors, thereby achieving color mixing and changes.

Adjusting the Brightness of Each Color
To get a specific color, you need to adjust the brightness of the three primary colors: red, green, and blue.
In the previous section, we learned that adjusting the resistance value can change the brightness of an LED. So the traditional method of controlling colors is to connect resistors with different resistance values to the three LED pins to control the current, thereby changing the brightness of each color.
However, this method is quite cumbersome to operate. We can directly use the PWM function in the development board to achieve this effect.
PWM
Pulse Width Modulation (PWM) is a power control technology widely used in various electronic devices and systems, including LED dimming, motor speed control, audio signal generators, etc.
PWM is achieved by changing the high-level time of a signal within a period. A period is the time elapsed from the beginning of one pulse to the beginning of the next pulse.

By changing the high-level time of the pulse, we can control the average power of the output signal. If the high-level time occupies a small portion of the entire period, the average power of the output signal will be very low, and the device will receive less power, thereby reducing brightness or speed. If the high-level time occupies a large portion of the entire period, the average power of the output signal will be very high, and the device will receive more power, thereby increasing brightness or speed.
On our development board, PWM can also be used to control the brightness of each LED. The chart below shows the signal of a PWM pin on the development board:

Every cycle, the PWM pin will generate a pulse. The length of this pulse is controlled by the 'analogWrite' function. So 'analogWrite(0)' will not generate any pulse, while 'analogWrite(255)' will maintain a high potential throughout the entire cycle, just like being directly connected to a 5V power supply.
We can control the length of the output pulse by changing the value input to the 'analogWrite' function. For example, if the output pulse occupies only 5% of the cycle, the LED will only receive 5% of the full power; if the output pulse occupies 90% of the cycle, the LED can receive 90% of the power.
Since the PWM period is very short (approximately 1/500 seconds), the human eye cannot observe the on/off state of the LED. Therefore, for us, the shorter the pulse, the dimmer the LED appears. This is how we control the brightness of the three LEDs.