What is the best color sensor?

Has anyone been able to get a color sensor to work? What sensor did you use and could you share your code? Thanks.

I have tried both the TCS3414 and the TCS34725 and all the code samples I could find on the internet. I have tweaked the gains and tried modifying the code but I can't get any decent readings from either sensor. I just want my robot lawnmower to distinguish between green grass and brown earth. The sensing distance would be variable (2 to 5 inches) and the sensor should work with varying illumination brightness. Maybe I need optics?

My latest trial code is:

#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.println(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", (clear/clear));
   sendPlotData("2-red", (red/clear));
   sendPlotData("3-green", (green/clear));
   sendPlotData("4-blue", (blue/clear));
   
  

}
//*********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("}");
}

warren631:
Has anyone been able to get a color sensor to work? What sensor did you use and could you share your code? Thanks.

I have done nothing other than test it but I have one of these and used sample sketch to read it. Adafruit products are usually very well supported and they probably have demo code and tutorials on how to use the sensor you have (assuming it came from them).

// *** Connect OUT to pin 2 (Interrupt 0)

/*  S0 & S1 = Frequency scaling
     L , L = Power down
     L , H = 2%
     H , L = 20%
     H , H = 100%    */
const byte S0=3,S1=4;

/*  S2 & S3 = Photodiode type
     L , L = Red
     L , H = Blue
     H , L = Clear (no filter)
     H , H = Green   */
const int S2=5,S3=6;

const int sampleDelay = 100;

volatile unsigned int Counter = 0;

void setup() {
    Serial.begin(9600);
    // All control pins to output
    pinMode(S0,OUTPUT);
    pinMode(S1,OUTPUT); 
    pinMode(S2,OUTPUT);
    pinMode(S3,OUTPUT);
//    // Set scaling to 2%
//    digitalWrite(S0,LOW);
//    digitalWrite(S1,HIGH);
    // Set scaling to 20%
    digitalWrite(S0,HIGH);
    digitalWrite(S1,LOW);
    // Attach handler
    attachInterrupt(0, pin2InterruptHandler, CHANGE);
}

void pin2InterruptHandler()
{
    Counter++;
}

void loop() {

    unsigned int Red = 0;
    unsigned int Green = 0;
    unsigned int Blue = 0;
    unsigned int Clear = 0;
    
    // Red filter
    digitalWrite(S2,LOW);
    digitalWrite(S3,LOW);
    noInterrupts();
    Counter = 0;
    interrupts();
    delay(sampleDelay);
    Red = Counter;
    // Green filter
    digitalWrite(S2,HIGH);
    digitalWrite(S3,HIGH);
    noInterrupts();
    Counter = 0;
    interrupts();
    delay(sampleDelay);
    Green = Counter;
    // Blue filter
    digitalWrite(S2,LOW);
    digitalWrite(S3,HIGH);
    noInterrupts();
    Counter = 0;
    interrupts();
    delay(sampleDelay);
    Blue = Counter;
    // Clear filter
    digitalWrite(S2,HIGH);
    digitalWrite(S3,LOW);
    noInterrupts();
    Counter = 0;
    interrupts();
    delay(sampleDelay);
    Clear = Counter;
    
    Serial.print("Red = ");
    Serial.print(Red);
    Serial.print(", Green = ");
    Serial.print(Green);
    Serial.print(", Blue = ");
    Serial.print(Blue);
    Serial.print(", Clear = ");
    Serial.println(Clear);
}

I have had good success with the tcs230 and have written a library with example code, including a document on how to calibrate. Link in my signature block - code repository.

Thanks guys, but the sensing distance for these devices has to be a constant 10mm and constant illumination for reliable color readings. I'm looking for: "The sensing distance will be variable (2 to 5 inches) and the sensor should distinguish colors with varying illumination brightness." - like your own eyes.