I'm having trouble using a TCS3200 Color sensor

Hey,
I have a project for school and im using a color sensor i need to detect 3 colors but only the last color in the code can be detected properly, the other two switch between unkown and the right color. the values of the colors are right i've changed them multiple times and it still doesn't work.this is my code.

// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
#define Roodout 9
#define Groenout 10
#define Blauwout 11

// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

//Detectie van de snoepkleur
int detectRood = 0;
int detectGroen = 0;
int detectBlauw = 0;

void setup() {

  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);

  // Setting frequency scaling to 20%
  digitalWrite(S0, HIGH);
  digitalWrite(S1, LOW);

  // Begins serial communication
  Serial.begin(9600);
}
void loop() {
  // Setting RED (R) filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, LOW);

  // Reading the output frequency
  redFrequency = pulseIn(sensorOut, LOW);

  // Setting GREEN (G) filtered photodiodes to be read
  digitalWrite(S2, HIGH);
  digitalWrite(S3, HIGH);

  // Reading the output frequency
  greenFrequency = pulseIn(sensorOut, LOW);

  // Setting BLUE (B) filtered photodiodes to be read
  digitalWrite(S2, LOW);
  digitalWrite(S3, HIGH);

  // Reading the output frequency
  blueFrequency = pulseIn(sensorOut, LOW);

  if ((93 <= redFrequency) && (108 >= redFrequency) && (79 <= greenFrequency) && (greenFrequency <=94 ) && (73 <= blueFrequency) && (blueFrequency <= 88)) {
    Serial.println("GROEN");
    detectGroen = 1;
    digitalWrite(Groenout, HIGH);
    delay(200);
  }
  if ((105 <= redFrequency) && (redFrequency <= 120) && (100 <= greenFrequency) && (greenFrequency <= 115) && (72 <= blueFrequency) && (blueFrequency <= 87)) {
    Serial.println("BLAUW");
    detectBlauw = 1;
    digitalWrite(Blauwout, HIGH);
    delay(200);
  }
  if ((63 <= redFrequency) && (redFrequency <= 77) && (107 <= greenFrequency) && (greenFrequency <= 122) && (89 <= blueFrequency) && (blueFrequency <= 104)) {
    Serial.println("ROOD");
    detectRood = 1;
    digitalWrite(Roodout, HIGH);
    delay(200);
    }
  else {
    Serial.println("ONBEKEND");
    detectRood = 0;
    detectGroen = 0;
    detectBlauw = 0;
    digitalWrite(Roodout, LOW);
    digitalWrite(Groenout, LOW);
    digitalWrite(Blauwout, LOW);
    delay(200);
  }
}

Can anyone please tell me what im doing wrong? thank you already

the values of the colors are right

How do you know? Print out the values of redFrequency, blueFrequency and greenFrequency when pointing the sensor at different objects, and compare those with the numbers in the "if" statements.

Keep in mind that reflected color depends VERY strongly on the illumination.

I moved your topic to an appropriate forum category @joppevanderaa.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

Thank you
i didn't really kow where to put it

1 Like

I've used a diffrent code to determine the values and they are correct.

Do you mind sharing that code? It can be useful for others that experience the same problem.

You can mark a topic as "solved" by clicking the solution button under the most useful reply. That would be yours if you post the new code :wink: That way others know that a solution was provided.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.