Color detection is the process of identifying and distinguishing colors within an image, video, scene, or object. Many embedded and robotic applications require this feature, as it’s useful for sorting, selection, test strip reading, path determination, and more.
Two standard sensors used for color detection include TCS230 or TCS3200. TCS3200 recognizes various colors based on their wavelength and is easy to interface with any microcontroller using GPIO. TCS230 is slightly more sensitive, detecting a narrower range of colors with higher precision.
In this project, we’ll interface the TCS230/TCS3200 sensor (either can be used) with Arduino Mega, calibrate the sensor, and derive the RGB values for specific colors.
- Arduino UNO/Arduino Mega x1
- TCS230/TCS3200 color sensor x1
- Connecting/DuPont wires
The color-recognition sensors
TCS230 and TCS3200 sensors detect and measure the intensity of light across different wavelengths to determine the color of an object. They can detect various colors using an array of photodiodes with different filters.
TCS230/TCS3200 contains four white LEDs that light up the object in front of it.
Typically, color-recognition sensors have four channels: red, green, blue, and clear (no filter). The TCS3200 has an array of photodiodes with four filters. In total, it has 16 photodiodes with red, green, and blue filters that are sensitive to red, green, and blue wavelengths; and it has 16 photodiodes without filters.
These sensors control the integration time during which they collect light. This allows them flexibility in adapting to different lighting conditions for greater color-recognition accuracy. TCS230/TCS3200 provide analog output signals proportional to each color channel’s light intensity. They also have a frequency-to-voltage converter that converts the color information into a voltage signal, which can be easily measured.
Additionally, TCS230/TCS3200 uses a modulation and filtering technique to eliminate ambient light interference, further improving the accuracy of color detection. By selectively choosing the photodiode filter readings, the strength of colors can be identified.
These sensors operate at a voltage of 2.7~5.5V DC and have the below pin diagram.
Description of the TCS230/TCS3200 pin configuration:
Pins S2 and S3 are used to read the light intensity of a particular color. The photodiodes are connected in parallel, so setting S2 and S3 to LOW and HIGH in different combinations allows the photodiodes to be matched to the corresponding colors.
The colors are selected as per the following table.
Pins S0 and Si scale the output frequency to 100%, 20%, or 2% of the preset parameters. Scaling the output optimizes the sensor readings for different microcontrollers or frequency counters.
When using Arduino, 20% is typical. Scaling using pins S0 and S1 is done according to the following table.
For this project, we’ll connect the TCS230/TCS3200 color sensor with the Arduino Mega as follows:
- Connect TCS230/TCS3200’s S0, S1, S2, S3, and ouput pins of with Arduino’s GPIO13, GPIO12, GPIO11, GPIO10, and GPIO9, respectively.
- Next connect TCS230/TCS3200’s VCC and GND pins with Arduino’s 5Vout and ground pins, respectively.
How it works
Arduino reads the light intensity of each color using the TCS230/TCS3200 sensor by choosing the red, green, and blue channels one after the other. The “read” values are mapped to their appropriate calibrated values for an RBG color output. Note that the RGB color value is only obtained after calibration.
The sensor is affected by ambient light. So, the color-detection calibration is only possible after the ambient light calibration. These RGB values do not match the true RGB color values, but they’re a decent estimate and valuable for sorting or selecting objects.
The TCS230/TCS3200 sensor must be calibrated in the same light in which it will be used. Print the red (#FF0000), green (#00FF00), and blue (#0000FF) colors on a sheet, along with a color for testing as shown in the image below.
First, put the sensor on the “true red” color and note the readings for the red, green, and blue channels. Repeat for the “true green” and “true blue” colors.
When the sensor is on the true red color, the frequency output of the red channel will be at its lowest. When it’s on true green or true blue, the frequency output will be at its highest for either color. The lowest frequency is mapped at 255 and the highest recorded frequency is mapped at 0.
After obtaining each color’s minimum and maximum values, map them in the sketch using the map() function. This is already done in the sketch above. But you’ll need to do so based on the lighting in your environment. This way, the sensor will be calibrated for the ambient light conditions in your project’s operating conditions.
The sketch begins by defining the pin assignments for the TCS230/TCS3200 color sensor. Next, the variables are declared for storing the RGB values. In the setup() function, the pins connected to S0, S1, S2, and S3 are set as digital outputs, and the pin connected to TCS230/TCS3200 is set as a digital input. The S0 pin is set to HIGH, and the S1 is set to LOW — selecting for 20% scaling, which is most suitable for Arduino. The baud rate for the serial communication is set to 9600 bps.
In the loop() function, Arduino first selects the sensor’s red channel by setting the S2 and S3 pins to LOW. It reads the frequency of the red color, mapping to its calibrated values using the map function. The value for the red filter is printed to the serial port. Arduino then selects the sensor’s green channel by setting the S2 and S3 pins to HIGH. It reads the frequency of the green color, mapping to its calibrated values using the map function. The value for the green filter is printed to the serial port.
Lastly, Arduino selects the sensor’s blue channel by setting the S2 pin to LOW and the S3 pin to HIGH. It reads the frequency of the blue color, mapping to its calibrated values using the map function. The value for the blue filter is printed to the serial port.
This provides an approximate RGB color value, which is useful in selecting or sorting objects.
You may also like:
Filed Under: Arduino, Electronic Projects, Video