For my Capstone Design project, one of my many tasks was to identify discrete colors on painted blocks (red, green, blue, yellow) using a the TCS34725 color sensor. I configured the color sensor to pass back four 16-bit integers representing the RGB and Clear color values of the reflected object that the sensor is “looking” at. Initially, I tried a simple linear matrix equation to map the input RGBC values to RGBy output numbers. I then took the highest output value as the ‘color’ it represented. This is easily seen in the equations below.

The idea was to use the coefficients in the matrix on the left to scale the incoming data (R_{S}, G_{S}, B_{S}) and produce an output that could be used to identify that color. This works in theory because of color mixing. However, due to imperfect reflection, imperfect lighting, and other types of imperfection, this was not as reliable as I wanted.

The next logical step was to “train” the coefficients by some manner. This was accomplished by taking a block of each color and wiggling it in front of the sensor at varying heights and angles and recording the RGB values from the sensor. I took 10,000 samples of each color and proceeded to do some post-processing in FreeMAT (a MATLAB clone). The post-processing proceeded in the following manner:

- For each set of 10,000 samples, separate the RGB components, and take the mean of each component.
- Assemble those generated means into a three-component array and divide by the 2-norm, thereby normalizing the array
- Repeat the first two steps for each of the the color samples

The arrays that are generated are your coefficients for the previously shown matrix.

This method turned out to be very effective. I also found that if the colors change, it is as simple as regenerating the coefficients to compensate. Then the system will be able to identify the colors again. I am quite confident that this method can be used to identify many other colors as well, however I have only tested on the four colors.

I have attached my .m script below for further exploration.

coeff_make.m