Hello everyone. In previous five blogs I evaluated VEML3328 color sensor. Today I will show only software update. Contest ended but because I did not take my application in this category seriously, I am posting it two days after deadline. Outcome from my previous blog was that I have written own library for the sensor and evaluated it by showing values on the display. I also tested and shown behaviour of changing configurable properties of the sensor like digital and analog gain and integration time.
Color Detection Idea
In previous blogs you have seen that if I placed red subject backlighted by white light sensor sensed much more red color than green and blue. Image from 4th blog shows it. Basically, white light is composed by lights oif all visible colors. In electronics we use red, green and blue lights which can be composed to light of almost any color. Experiment worked because red antistatic foam reflect and absorb green and blue and red light passed through it. Our eye also noticed that most of the light coming to the eyes is read, thus we have seen subject as red.
Schematically it looked as follows
My idea for better color detection without need of backlight and possibility able to detect non-transparent object was similar but placed different light source at different position. I placed white RGB LEDs configured for emitting all channels at maximum which resulted to white light. I placed it next to the sensor and pointing upwards.
With this setup if you place target object near to the light and sensor, light get reflected back down. Some amount of the light will reach the nearby sensor. Amount of each red, green and blue light is related to target object color. Green targets will reflect more green light than red and blue lights. Similarly for example yellow subject will reflect lot of red and green lights but almost no blue light. Schematically it looks as follows:
Adding Light
For implementing color detection, I used idea mentioned above. For a light source I used 4 WS2812B LEDs. Because LDO on STM32G031 discovery board can handle only 200 mA I powered LEDs by external 5V power supply. LEDs are good because I can adjust light color and brightness from software. As you can see light is next to sensor and it is very near to sensor.
White Balancing
In 4th blog I mentioned that all channels are differently sensitive to colors. The green channel is more sensitive than red channel and blue channel is the least sensitive to the light. Datasheet contains plot with wavelengths and normalized sensitivity for these colors:
For correct operation of the algorithm, I need to compensate this behaviour. Generally, this kind of compensation is referred as white balancing. Purpose of compensation is by adjusting some coefficients to compensate the channels and achieve that against white target algorithm report all three channels the same value.
I implemented this compensation when user click the button. When he/she click the button, algorithm will compute multiplicative coefficients for each channel that are later used for compensating channels. Precondition is that when user click the button white object must be near to the sensor. I used white paper for this compensation.
Proximity Detection and Ambient Light Compensation
Other detection which I used is proximity detection. Proximity detection can be smartly implemented for example using IR light source (which is invisible to human eye, but sensor can see it) and measuring amount of reflected IR light. I implemented very trivial algorithm which will save value of clear channel (Clear channel is sensitive to all visible colors) at power-up and then compare current value with this initial. If current value reach 1.5× original value it estimate that there is some subject. Otherwise, it suppose that there is no subject in front of sensor. The reason for storing initial value is for compensating ambient light. Sensor will always report some nonzero values even there is no subject and amount of reflected light is negligible. It is because our light source (usually) is not the only light in the room and sensor sense all this other lights also. So, the algorithm at the power-up stores level of light received from other sources and then detect color only in case when number of reflected light increase above this level.
Color detection
Now we know idea for color detection, we know how to compensate differences in channels sensitivity and know how to check if there is or is not subject near the sensor. The last remaining thing is color detection. I implemented very simple algorithm. I took channel which value is maximum from all channels and suppose this is the target color. This is sufficient for detecting red, green and blue targets but it is insufficient for detecting subjects of combined color (for example yellow, cyan, magenta or white). For handling these cases I look other channels and if their values are similar to the maximum channel I mark them also. At the end I deduce color from marked channels. This algorithm support detecting red, green, blue, cyan, magenta, yellow and white colors (7 colors in total).
See it in the action!
At following video, I will run white balancing with white paper at the beginning. Note that after balancing sensor immediately start reporting white color. You can see that when there is no subject in front of sensor, device do not report any color (proximity detection) and at last you can see my algorithm when detecting color of four subjects. Following subjects I placed in the front of sensor:
Here is the video:
Source codes
Updated source codes of this experiment are on the Github now. But because I am posting it over deadline I did not pushed them to the main branch. Instead, I created separate branch for them. It is available here: https://github.com/misaz/VEML3328-Experiments/tree/simple-color-detection-algorithm
Summary
At the end I am very satisfied. After white balancing, algorithm works very well even it is very trivial. I am generally satisfied with the VEML3328 sensor. It works out of box, is easy to use, important parameters are configurable, has high resolution and sensitivity can be adjusted in big range depending on application and environment.
This is most probably my last blog as part of this serie. If you have any question about VEML3328, then feel free to ask in comments below. Similarly, I welcome any feedback regarding my last-minute application and project as part of this competition and Exploration Station category. Thank you for reeding this blog post. Have a nice day.
Top Comments