TCS3414 color sensor not working?

Hi guys,
I want to sense the color green (e.g. so a LED lights when the sensor is shown something green).
I am using the following code and it seems to work (spits out numbers which change when I cover the sensor with my hand) but the red, green, blue and clear numbers printed to the serial monitor all seem to change by the same percentages whatever color I show the sensor. I have tried to tweak the gain settings, etc. but no luck so far. I was expecting the green output to increase in relationship with the other color numbers when the sensor is shown a green object such as a leaf.

I level shifted bidirectionally all the inputs and outputs as suggested elsewhere. It seems to be working but just not differentiating between different colors. Bad sensor or am I missing something? Should I use a green filter?

The code is too large to post here so I put a copy in dropbox:

Google "TCS3414 spec" for the sensor specification.

I was not getting any success with the TCS3414 color sensor - see above - so I have been experimenting with the Adafruit TCS34725 without any better success. I have been using the following code and monitoring the color outputs with MegunoLink’s plotter. It seems with all these color sensors the biggest variable to change outputs is the object distance from sensor. Distance (or brightness) changes not only the color outputs but also the ratios between color outputs to such an extent that trying to get a definite difference between say green and brown (grass and earth) would be practically impossible. Can anyone suggest what else I should try? Thanks for any suggestions.

PS: I removed the delays from the Adafruit_TCS34725.cpp to give me a much faster response - but no better results.

#include <Wire.h>
#include "Adafruit_TCS34725.h"

// Pick analog outputs, for the UNO these three work well
// use ~560  ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground

// set to false if using a common cathode LED
#define commonAnode true

// our RGB -> eye-recognized gamma color
byte gammatable[256];


Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_16X);

void setup() {
  Serial.begin(9600);
  Serial.println("Color View Test!");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
  
  // use these three pins to drive an LED
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  
  // thanks PhilB for this gamma table!
  // it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;
      
    if (commonAnode) {
      gammatable[i] = 255 - x;
    } else {
      gammatable[i] = x;      
    }
    //Serial.println(gammatable[i]);
  }
}


void loop() {
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(60);  // takes 50ms to read 
  
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED
  
  //Serial.print("C:\t"); Serial.print(clear);
  //Serial.print("\tR:\t"); Serial.print(red);
  //Serial.print("\tG:\t"); Serial.print(green);
  //Serial.print("\tB:\t"); Serial.print(blue);

  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
  //Serial.print("\t");
  //Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
  //Serial.println();

  //Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );
float total = r + g + b;
float redPart = r/total;
float greenPart = g/total;
float bluePart = b/total;

  analogWrite(redpin, gammatable[(int)r]);
  analogWrite(greenpin, gammatable[(int)g]);
  analogWrite(bluepin, gammatable[(int)b]);
   //sendPlotData("1-clr", (c));
   sendPlotData("2-red", redPart);
   sendPlotData("3-green", greenPart);
   sendPlotData("4-blue", bluePart);
   
  

}
//*********format data for MegunoLink *****************************************************
void sendPlotData(String seriesName, float data) {
  Serial.print("{");  
  Serial.print(seriesName);
  Serial.print(",T,");
  Serial.print(data);
  Serial.println("}");
 
//  Serial1.print("{");  
//  Serial1.print(seriesName);
//  Serial1.print(",T,");
//  Serial1.print(data);
//  Serial1.println("}");
}

Well, i agree. Im running into the same behaviour. It doesnt want to pickup a significantly green value from a green object. Im testing with TCS230 or 3000 im not even sure, but i tried 101 examples for both, just in case.

All i can think off is that anyone thusfar has been statisfied with bad results, hence, publishing bad libraries.
Its said this particular sensor i have is used to read RGB pixel of Displays to see if the colors are OK.

If thats true, this sensor is just being mistreaded by any of the libraries so far. But im hesistate to conclude this. I mean, i rather wish to adopt my sensor is broken. But Perhaps i just need to start from Scratch and write the code myself?!

That said, i hardly understand the Data Sheet for this one, and i usuealy have no issues with datasheets....

As per the other post, if you are looking for TCS230 library that works with an explanation of how to calibrate the sensor, look at my code libraries, linked below.

Yeah, i did, but its not yielding the results im looking for. Maybe its todo with my sensor. I now need to learn to write my own so i understand, and see if i can take it from there. And then compare to yours to see what you do differently.

I may be able to shed some light :wink: on this issue. The RGB method of describing color is an imatation of what atually happens when a human perceives color. There is a lot of "processing" that goes on between the place where light enters the eye and a person says, "oh yes, that color is orange". In fact, if two colors are seen by a human it would be perceived as some color between those two. So, when your rgb color sensor sees what looks like green to you, the sensors might actually be responding to some shade of blue and some shade of yellow at the same time. In that case there would be output from all three sensors. In fact, there are an infinite number of colors and color combinations that a human would perceive as green, or many other colors.

The RGB method can be used to measure and reproduce colors quite well, but the process only works well in one direction. You cannot measure RGB values and accurately predict what a human would call the color.

You might have better luck looking for one of the wavelengths that chlorophyll reflects. Presumably dirt wouldn't have much chlorophyll.

Wild guess: when 16 bits are transferred via I2C, the order of the high and low bytes may not match the code.