DESCRIPTION
Your electronics can now see in dazzling color with this lovely color light sensor.
We found the best color sensor on the market, the TCS34725, which has RGB and Clear light sensing elements.
An IR blocking filter, integrated on-chip and localized to the color sensing photodiodes, minimizes the IR spectral component of the incoming light and allows color measurements to be made accurately.
The filter means you’ll get much truer color than most sensors, since humans don’t see IR.
The sensor also has an incredible 3,800,000:1 dynamic range with adjustable integration time and gain so it is suited for use behind darkened glass.
We add supporting circuitry as well, such as a 3.3V regulator so you can power the breakout with 3-5VDC safely and level shifting for the I2C pins so they can be used with 3.3V or 5V logic. Finally, we specified a nice neutral 4150°K temperature LED with a MOSFET driver onboard to illuminate what you’re trying to sense.
The LED can be easily turned on or off by any logic level output.
Connect to any microcontroller with I2C and our example code will quickly get you going with 4 channel readings.
We include some example code to detect light lux and temperature that we snagged from the eval board software.
A detailed tutorial is here, check out our Arduino library and follow our tutorial to install.
Wire up the sensor by connecting VDD to 3-5VDC, Ground to common ground, SCL to I2C Clock and SDA to I2C Data on your Arduino.
Restart the IDE and select the example sketch and start putting all your favorite fruit next to the sensor element!
Weight: 3.23g
Dimensions: 20.44mm / 0.8″ x 20.28mm / 0.79″
This board/chip uses I2C 7-bit address 0x29.
Example
To read the TCS34725 sensor we will use the library developed by Adafruit, available at this link.
The library includes several examples of use that should be reviewed
Next we are going to see some examples of use of the TCS34725 sensor.
Read RGB values
In this first, very simple example, we read the sensor values and display them via serial port for visualization.
#include <Wire.h>
#include "Adafruit_TCS34725.h"
/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
void setup(void) {
Serial.begin(9600);
if (!tcs.begin())
{
Serial.println("Error al iniciar TCS34725");
while (1) delay(1000);
}
}
void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;
tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);
Serial.print("Temperatura color: "); Serial.print(colorTemp, DEC); Serial.println(" K");
Serial.print("Lux : "); Serial.println(lux, DEC);
Serial.print("Rojo: "); Serial.println(r, DEC);
Serial.print("Verde: "); Serial.println(g, DEC);
Serial.print("Azul: "); Serial.println(b, DEC);
Serial.print("Clear: "); Serial.println(c, DEC);
Serial.println(" ");
delay(1000);
}
Sort colors
In the second example, we are going to read the RGB values and try to determine the color we are reading.
To make it easier we convert the RGB values to HSV with the ColorConverter library.
To determine the color we use the Hue of the measured color.
For a fine adjustment it will be necessary to calibrate the sensor by testing colors and adjusting the values of the conditionals.
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include "RGBConverterLib.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X);
void setup()
{
Serial.begin(9600);
if (!tcs.begin())
{
Serial.println("Error al iniciar TCS34725");
while (1) delay(1000);
}
}
void loop()
{
uint16_t clear, red, green, blue;
tcs.setInterrupt(false);
delay(60); // Cuesta 50ms capturar el color
tcs.getRawData(&red, &green, &blue, &clear);
tcs.setInterrupt(true);
// Hacer rgb medición relativa
uint32_t sum = clear;
float r, g, b;
r = red; r /= sum;
g = green; g /= sum;
b = blue; b /= sum;
// Escalar rgb a bytes
r *= 256; g *= 256; b *= 256;
// Convertir a hue, saturation, value
double hue, saturation, value;
RGBConverterLib::RgbToHsv(static_cast<uint8_t>(r), static_cast<uint8_t>(g), static_cast<uint8_t>(b), hue, saturation, value);
// Mostrar nombre de color
printColorName(hue * 360);
delay(1000);
}
void printColorName(double hue)
{
if (hue < 15)
{
Serial.println("Rojo");
}
else if (hue < 45)
{
Serial.println("Naranja");
}
else if (hue < 90)
{
Serial.println("Amarillo");
}
else if (hue < 150)
{
Serial.println("Verde");
}
else if (hue < 210)
{
Serial.println("Cyan");
}
else if (hue < 270)
{
Serial.println("Azul");
}
else if (hue < 330)
{
Serial.println("Magenta");
}
else
{
Serial.println("Rojo");
}
}