Where can I find a code sample for the Color Sensor TCS3200-db

I'm doing a project and the only color sensors the school has are the TCS3200-DB. They have two LEDs. I have found alot of code online for those with the 4 LEDs but the ouput is not accurate, they're all over 255.

Can some please direct me as to where I can find a tutorial on the two LED color sensors or provide me with a code sample. its 6am in the morning and I started researching since 10pm. Please help someone.

Found a datasheet but the code provided is in basic.

There is a video with a guy using it, he also provided a link to code that also doesn't work, after adjustments too remove the text to spech.

I will also provide what I have currently, but nothing is outputed in the serial monitor.

Also his code uses the maximum function from the Average.h file but for some reason, an error occurs about not finding the Average.h library, which I downloaded and placed in the Arduino library.

*/
//**************************************** LIBRARIES ***************************************
#include <Average.h> // See http://playground.arduino.cc/Main/Average

//************************************ GLOBAL VARIABLES ************************************
// s0 - purple - 40
// s1 - gray - 37
// s2 - green - 41
// s3 - blue - 39
// led - brown - 36
// out - white - 38


int S0=40,S1=37,S2=41,S3=39, LED=36;
int OUT=38;

void setup() {
  TCS3200_Setup();
  delay(100);
}

void loop() {
  Serial.println(ReadColor());
}

//********************************* FUNCTIONS COLOR SENSOR *********************************
void TCS3200_Setup() {
  pinMode(S0,OUTPUT);
  pinMode(S1,OUTPUT);
  pinMode(S2,OUTPUT);
  pinMode(S3,OUTPUT);
  pinMode(LED,OUTPUT);
  pinMode(OUT,INPUT);
}

void TCS3200_On() {
  digitalWrite(LED,HIGH); // Switch LED on
  digitalWrite(S0,HIGH); //Output frequency scaling (100%)
  digitalWrite(S1,HIGH);
  delay(5);
}

void TCS3200_Off() {
  digitalWrite(LED,LOW); // Switch LED off
  digitalWrite(S0,LOW); //Power off sensor
  digitalWrite(S1,LOW);
}

void NoFilter() { //Select no filter
  digitalWrite(S2,HIGH);
  digitalWrite(S3,LOW);
  delay(5);
}

void RedFilter() { //Select red filter
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  delay(5);
}

void GreenFilter() { //Select green filter
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  delay(5);
}

void BlueFilter() { //Select blue filter
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  delay(5);
}

int ReadColor() { //0=white, 1=orange, 2=yellow, 3=red, 4=green, 5=blue, 6=object out of range
  float FrequencyClear,FrequencyRed,FrequencyGreen,FrequencyBlue;
  int PercentageRed,PercentageGreen,PercentageBlue;
  TCS3200_On();
  NoFilter();
  FrequencyClear=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  RedFilter();
  FrequencyRed=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  GreenFilter();
  FrequencyGreen=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  BlueFilter();
  FrequencyBlue=500.0/pulseIn(OUT,LOW,10000); // Frequency in kHz
  TCS3200_Off();
 
  //Output frequency blue, green, red percentage represents the ratio of the
  //respective color to the Clear channel absolute value:
  PercentageRed=int((FrequencyRed/FrequencyClear)*100.0);
  PercentageGreen=int((FrequencyGreen/FrequencyClear)*100.0);
  PercentageBlue=int((FrequencyBlue/FrequencyClear)*100.0);
 
  //Learned blue, green, red percentage values of different colors
  int SavedColorRed[] = {28,55,42,50,19,13};
  int SavedColorGreen[] = {30,25,36,22,45,26};
  int SavedColorBlue[] = {45,20,20,30,36,58};
  int GetColor[] = {1,2,3,4,5,6};
  int ColorArray[3];
  int i_color;
  int ClosestColor;
  int MaxDiff;
  int MinDiff=300;
  if(FrequencyClear<1.5)ClosestColor=6; // Object out of range
  else {
    for (i_color=0; i_color<6; i_color++) { //Find closest color
      ColorArray[0]=abs(SavedColorRed[i_color]-PercentageRed);
      ColorArray[1]=abs(SavedColorGreen[i_color]-PercentageGreen);
      ColorArray[2]=abs(SavedColorBlue[i_color]-PercentageBlue);
 //     MaxDiff=maximum(ColorArray,3);
      if (MaxDiff<MinDiff) {
        MinDiff=MaxDiff;
        ClosestColor=i_color;
       }
     }
   }
   return GetColor[ClosestColor]; 
}

Please help Someone, even if is by making the code simpler.

ShockWave08:
Can some please direct me as to where I can find a tutorial on the two LED color sensors or provide me with a code sample

Does this tutorial use the same sensor?

@ShockWave08

Other post/duplicate DELETED

Please READ THIS POST to help you get the best out of the forum.

Bob.

I'm sorry you'll.. It was a desperate attempt for a solution. Won't happen again!!

@Riva, Yes it does use the same sensor. Thanks alot..

I'll let you know if, I was successful.

The code is running with no errors but for some reason the character encoding is incorrect.

Z4@⸮⸮⸮⸮⸮E⸮⸮؈⸮⸮⸮2Ơs⸮⸮⸮L9⸮⸮m⸮Þ⸮*⸮⸮N*Ⱦ⸮⸮⸮⸮R{⸮⸮觊⸮N(Y⸮⸮⸮⸮⸮⸮⸮⸮RL9⸮

Anyone ever encountered this output.

How can i fix?

I'm gonna switch encoding to see if that works. The fix encoding and reload has no effects on the output.

What baud rate (serial speed) do you have set on the Arduino serial monitor? Default for it is 9600 and this example is using 115200. Either edit the code to change Serial.begin(115200); to Serial.begin(9600); or increase the Serial monitor speed to match.

@Riva.. Its finally working.. Thanks sooo much bruv

@Riva

I've been trying to get other colors, which has proven most difficult cause I can't find a tutorial on the internet for this specific sensor that helps.

Would you have any idea how to get more colors, like brown, yellow and purple? I jus need an example

I 'd really appreciate any help, sir.

Or anyone that can help please

Forgive me if I am wrong but does not each primary colour give off a number up to 255 ?
If that is the case then a mix of colours would be a variation and addition of two or more colours.

So a midpoint red and a midpoint blue would both be around 128 each.
Then it would make sense that if red=128 and blue =128 then color equals magenta.

How you approach that may be a table / data array that can be referenced by the Arduino.

You may also have to allow for a plus / minus offset to allow for slight variations to the magenta.

Not done this myself but it seems logical.

Bob.

@Ballscrewbob.. Thanks much.. it makes sense, its similar to what i read.

But what i'm trynaa figure out is how to adjust the code to do that, the comparisons are made on a percentage..

That's what i really want to see. Atleast one example of a color with this implementation..

Again I am not speaking from experience here but how about you print off a contact sheet of secondary and tertiary colours.

Then see what the sensor combination says for any given colour.
The examples for the sensor are more than capable of doing that simple task.

From those you should be able to build a simple table from which to work.

This tutorial may be a good starting point.

Bob.

I got this to work.. thanks for all your help @ballscrewbob

int S0 = 40; //pinB
int S1 = 37; //pinA
int S2 = 41; //pinE
int S3 = 39; //pinF
int out = 38; //pinC
int LED = 36; //pinD


void setup()
{
  TCS3200setup();
  Serial.begin(9600);
  Serial.print("\n\n\nready\n\n\n");
  delay(100);
}

void loop()
{
  Serial.print(detectColor(out));
  Serial.print("\n\n\n");
  delay(1000);
}

int detectColor(int taosOutPin)
{
  //isPresentTolerance will need to be something small if used in high light environment, large if used in dark environment.
  //the color detection will work either way, but the larger isPresentTolerance is, the closer the object will need to be in front of sensor
  
  double isPresentTolerance = 5;
  double isPresent = colorRead(taosOutPin,0,0)/colorRead(taosOutPin,0,1);

  //number gets large when something is in front of sensor. 
  Serial.print("isPresent:");
  Serial.println(isPresent,2);
  Serial.print("isPresentTolerance currently set to:");
  Serial.println(isPresentTolerance,2);

  if(isPresent < isPresentTolerance)
  {
    Serial.println("nothing is in front of sensor");
    
    return 0;
  }

  double red,blue,green;
  double white = colorRead(taosOutPin,0,1);
  

  red = white / colorRead(taosOutPin,1,1);
  blue = white / colorRead(taosOutPin,2,1);
  green = white / colorRead(taosOutPin,3,1);

  
  Serial.print("red: ");
  Serial.println(red);
  Serial.print("blue: ");
  Serial.println(blue);
  Serial.print("green: ");
  Serial.println(green);

  if(red > 0.70 and red < 0.80  and blue > 0.10 and blue < 0.20 and green > 0.10 and green < 0.20)
  {
    Serial.println("red is in front");
    return 1;
  }
  
  if(red > 0.20 and red < 0.25 and blue > 0.55 and blue < 0.58 and green > 0.28 and green < 0.31)
  {
    Serial.println("blue is in front");
    return 2;
  }
  
  if(red > 0.20 and red < 0.30  and blue > 0.30 and blue < 0.40 and green > 0.40 and green < 0.50)
  {
    Serial.println("green is in front");
    return 3;
  }

  if(red > 0.45 and red < 0.55  and blue > 0.20 and blue < 0.30 and green > 0.30 and green < 0.40)
  {
    Serial.println("brown is in front");
    return 4;
  }

  if(red > 0.50 and red < 0.60  and blue > 0.10 and blue < 0.20 and green > 0.35 and green < 0.45)
  {
    Serial.println("yellow is in front");
    return 5;
  }

  if(red > 0.30 and red < 0.40  and blue > 0.30 and blue < 0.40 and green > 0.30 and green < 0.40)
  {
    Serial.println("black is in front");
    return 6;
  }

  if(red > 0.40 and red < 0.45  and blue > 0.30 and blue < 0.40 and green > 0.30 and green < 0.40)
  {
    Serial.println("white is in front");
    return 7;
  }

  if(red > 0.24 and red < 0.28  and blue > 0.52 and blue < 0.59 and green > 0.27 and green < 0.32)
  {
    Serial.println("purple is in front");
    return 8;
  }

   if(red > 0.78 and red < 0.85  and blue > 0.17 and blue < 0.20 and green > 0.10 and green < 0.14)
  {
    Serial.println("pink is in front");
    return 9;
  }

   if(red > 0.70 and red < 0.75  and blue > 0.15 and blue < 0.18 and green > 0.20 and green < 0.25)
  {
    Serial.println("orange is in front");
    return 10;
  }

  Serial.println("printed nothing");

}


/*
This method will return the pulseIn reading of the selected color.

Since frequency is proportional to light intensity of the selected color filter, 
the smaller pulseIn is, the more light there is of the selected color filter.
  
It will turn on the sensor at the start taosMode(1), and it will power off the sensor at the end taosMode(0)

color: 0=white, 1=red, 2=blue, 3=green

if LEDstate is 0, LED will be off. 1 and the LED will be on.

taosOutPin is the ouput of the TCS3200. If you have multiple TCS3200, all wires can be combined except the out pin
*/

double colorRead(int taosOutPin,int color,boolean LEDstate)
{
  //make sure that the pin is set to input
  pinMode(taosOutPin,INPUT);
  
  //turn on sensor with highest frequency setting
  taosMode(1);
  
  //delay to let the sensor sit before taking a reading. Should be very small with this sensor
  int sensorDelay = 10;
  
  //set the pins to select the color  
  if(color == 0){
    //white
    digitalWrite(S3,LOW);//S3
    digitalWrite(S2,HIGH);  //S2
    // Serial.print(" w");
  }

  else if(color == 1)
  {
    //red
    digitalWrite(S3,LOW); //S3
    digitalWrite(S2,LOW); //S2
    // Serial.print(" r");
  }

  else if(color == 2)
  {
    //blue
    digitalWrite(S3,HIGH);  //S3
    digitalWrite(S2,LOW); //S2 
    // Serial.print(" b");
  }
      
  else if(color == 3)
  {
    //green
    digitalWrite(S3,HIGH);  //S3
    digitalWrite(S2,HIGH);  //S2 
    // Serial.print(" g");
  }

  double readPulse;

  if(LEDstate == 0)
  {
    digitalWrite(LED,LOW);
  }
  
  if(LEDstate == 1)
  {
    digitalWrite(LED,HIGH);
  }

  delay(sensorDelay);
  
  readPulse = pulseIn(taosOutPin,LOW,80000);
  
  //if the pulseIn times out, it returns 0 and that throws off numbers. just cap it at 80k if it happens
  if(readPulse < .1)
  {
    readPulse = 80000;
  }
  
  //turn off color sensor and white LED to save power 
  taosMode(0);

  return readPulse;
}

//setting mode to zero will put taos into low power mode. taosMode(0);
void taosMode(int mode)
{
  if(mode == 0)
  {
    //power OFF
    digitalWrite(LED,LOW);
    digitalWrite(S0,LOW); //S0
    digitalWrite(S1,LOW); //S1
    
    // Serial.println("mOFFm");
  }
  
  else if(mode == 1)
  {
    //this will put in 1:1
    digitalWrite(S0,HIGH);  //S0
    digitalWrite(S1,HIGH);  //S1

    // Serial.println("m1:1m");
  }
  
  else if(mode == 2)
  {
    //this will put in 1:5
    digitalWrite(S0,HIGH); //S0
    digitalWrite(S1,LOW);  //S1

    //Serial.println("m1:5m");
  }
  
  else if(mode == 3)
  {
    //this will put in 1:50
    digitalWrite(S0,LOW); //S0
    digitalWrite(S1,HIGH);  //S1
     
    //Serial.println("m1:50m");
  }
  
  return;
}

void TCS3200setup()
{
  //initialize pins
  pinMode(LED,OUTPUT);  //LED pinD
  
  //color mode selection
  pinMode(S2,OUTPUT); //S2 pinE
  
  pinMode(S3,OUTPUT); //s3 pinF
  
  //color response pin (only actual input from taos)
  //pinMode(out, INPUT); //out pinC
  //communication freq output divider
  pinMode(S0,OUTPUT); //S0 pinB
  pinMode(S1,OUTPUT); //S1 pinA 
  
  return;
}

Sorry for the late reply (just back from India holiday) & thanks for ballscrewbob doing the work :slight_smile:

I didn't do that much but so glad you got something working.

Thanks for posting the code too it was pretty close to how I imagined it in my head LOL

Bob.