Example Code for MakeCode - Color Recognition
Last revision 2026/01/26
This article delves into the principles of color recognition and the RGB color model, offering practical insights with sample code for programming RGB LEDs and understanding color sensors through MakeCode.
Principle
Color principle and color recognition
In this beautiful and colorful world, we can see a variety of colors: green trees, blue sky, rainbows, cartoons, different clothing colors and so on, then how these colors are formed? How do people's eyes see all kinds of colors?

1.The principle of color formation
Light has a variety of color spectra. When visible light projected onto objects, different objects absorb different colors of the spectrum, a part of the wavelength of light is absorbed, a part of the wavelength of light is reflected to stimulate the human eye, forming a different color.
People's retina has three photosensitive pigments red, green, blue. They are not only sensitive to light, but also very sensitive to color. Light into the retina can cause the three cells to change in different degrees. Along different neural channels, the transformed cells transport into the visual center of the cerebral cortex, resulting in the corresponding color perception.
White colors are displayed when the three photosensitive pigments are stimulated equally. When they are stimulated by mixture with different proportions, they can form a variety of color perceptions, and that's how people recognize and perceive this colorful world.
2.Three-color principle
In nature, there are many colors, such as: red, orange, yellow, green, green, blue, purple, white and so on.
Later, it was found that almost all colors of light in nature are obtainable by mixing the colors red, green and blue, that is the principle of tri-color.
Three-base colors refer to red, green and blue, abbreviated as RGB. As shown in the following image we can see:
When red and blue are superimposed, purple is displayed.
When the red and green are superimposed, yellow is shown.
When the green and blue superimposed, it shows cyan.
When red, green and blue are superimposed together, they show pure white.
3.The principle of RGB LED and applications
According to the principle of color formation and the principle of eye recognition of color, people invented colorful lamps, cameras, color recognition sensors and other scientific and technological products.

Preparation
In this section, let's learn about RGB lamps and the color sensor.
- First, we prepare an RGB lamp module, as follows:

- Connect the module to the P1 port, as follows:

- Then, let's look at what a color recognition sensor looks like, as follows:
Maybe you'll ask: Isn't this a light sensor? Yes, this sensor can detect light intensity and different color values.

For more information, refer to the Getting Started guide.
Sample Code
First Part
Let's write a program to intuitively feel the RGB tricolor blending principle:
In the program, we called the RGB lamp library. And set the RGB led connected to the P1, the number is 1.
- First of all, set the value of Red to 255, Green and Blue to 0, displayed in Red.
- After a one-second delay, set the value of Red to 255, Green and Blue to 0, displayed as Green.
- After a delay of one second, set the Blue value to 255, Red and Green to 0, displayed in Blue.
- After a one-second delay, set the value of Red and Blue to 255, Green value to 0, displayed as Purple.
- After a delay of one second, set the value of Red and Green to 255, Blue value to 0, displayed as Yellow.
- After a one-second delay, set the value of Green and Blue to 255, Red value to 0, displayed as Cyan.
- The final execution result of the program: RGB led shows red, green, blue, purple, yellow, green 6 colors in turn.
In the program, we found that the range of RGB tricolor is: 0 to 255. You can try changing the values of the three parameters of the RGB to see what color will show with different values.

Try covering a piece of paper on the RGB lamp and you'll find that the color blends better, as follows:

Second Part
Let's use the sensor to actually test the values of the various colors in daily life. First, we write a detection program:

Result
Download the program and let’s find some objects with bright colors to put above the sensor in about 5mm height, this time, the display can show the current RGB values. Of course, because the influences like light, distance and others, this value may not be as accurate as we think, the accurate data need to be modified by various algorithms. But in this way, we can understand how colors are read. And we can also try recording the color values we read, then light the RGB led in the program, and see what colors the color sensor will sees.

Was this article helpful?
