Colour sensor help

Hi its my first time posting. Im a first year electronic engineering student and doing our second lab project which is an RGB colour sensor. I have an Arduino mega R3 2560 and the colour sensor I am using is a TCS3200.
I have it set up correctly will the pin out diagram and was able to upload the code to the board.

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
int redfrequency = 0;
int greenfrequency = 0;
int bluefrequency = 0;
int blackfrequency = 0;
void setup() {
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);

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

Serial.begin(9600);
}
void loop() {
// Setting red filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
// Reading the output frequency
redfrequency = pulseIn(sensorOut, LOW);

delay(100);
// Setting Green filtered photodiodes to be read
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
// Reading the output frequency
greenfrequency = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor

delay(100);
// Setting Blue filtered photodiodes to be read
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
// Reading the output frequency
bluefrequency = pulseIn(sensorOut, LOW);
// Printing the value on the serial monitor

if (redfrequency>25 && redfrequency< 77) {Serial.println("RED COLOUR");} else if (bluefrequency>25 && bluefrequency< 77) {Serial.println("BLUE COLOUR");} else if (greenfrequency>25 && greenfrequency< 77)

{Serial.println("GREEN COLOUR");}

else
{Serial.println("NO COLOUR DETECTION");}
}

When I hold up blue and red colours the serial monitor can identify the colours correctly. When I try the colour green it shows up as blue. Im sure I have something wrong in the code but I only know some basic C+

Our goal for the lab is to identify the colours white,black,green,blue and red.
Any help would be appriciated.

Try printing all three frequency values.

int redfrequency = 0;
int greenfrequency = 0;
int bluefrequency = 0;
int blackfrequency = 0;

A few things here;

  1. Those values are all globals, but don't need to be.
  2. Because they're global, by the time you see them, they're already all zero, so no need for the "= 0"
  3. See what I did with code tags? You should do the same.

Thanks for the reply. I'm in college at the moment so wont get to have a look at the code will I get home.
I thought I was printing the frequency values?
Not sure what you mean by global. Sorry I'm a complete newb with all of this

macman20:
I thought I was printing the frequency values?

Not in the code you posted.

BTW, pulseIn returns "unsigned long", not "int"

Uncompiled, untested, could probably be factored better

const uint8_t Sn [] { 4, 5, 6, 7}
const uint8_t sensorOut = 8;

void setup() 
{
  for (auto &sn : Sn) { 
    pinMode(sn, OUTPUT);
  }
  pinMode(sensorOut, INPUT);  // Strange name for an input, but, hey.
 
  // Setting frequency-scaling to 20%
  digitalWrite(Sn[0], HIGH);
  digitalWrite(Sn[1], LOW);
 
  Serial.begin(9600);
}

void loop() 
{
  // Setting red filtered photodiodes to be read
  digitalWrite(Sn[2], LOW);
  digitalWrite(Sn[3], LOW);
  uint32_t redfrequency = pulseIn(sensorOut, LOW);
 
  delay(100);
  // Setting Green filtered photodiodes to be read
  digitalWrite(Sn[2],HIGH);
  digitalWrite(Sn[3],HIGH);
  uint32_t greenfrequency = pulseIn(sensorOut, LOW);
 
  delay(100);
  // Setting Blue filtered photodiodes to be read
  digitalWrite(Sn[2],LOW);
  digitalWrite(Sn[3],HIGH);
  uint32_t bluefrequency = pulseIn(sensorOut, LOW);

  Serial.print (F("Red: "));
  Serial.println (redfrequency);
  Serial.print (F("Green: "));
  Serial.println (greenfrequency);
  Serial.print (F("Blue: "));
  Serial.println (bluefrequency);

  if (redfrequency>25 && redfrequency< 77) {
    Serial.println("RED COLOUR");
  } else if (bluefrequency>25 && bluefrequency< 77) {
    Serial.println("BLUE COLOUR");
  } else if (greenfrequency>25 && greenfrequency< 77) {
    Serial.println("GREEN COLOUR");
  } else {
    Serial.println("NO COLOUR DETECTION");
  }
}

Hi tried again with the code you posted. Still detecting green as blue. Is there any way to add the colours white and black into the code ?

It may well be that the sensor can't tell the coloured things you have apart. Or the ranges you chose aren't working for those two.

What do the serial prints tell you for your green & blue samples?

For green I'm getting R=165, G=109, B=41
Blue R=147, G=90, B=29

There's your answer. Green shows up with a blue component of 41. 41 is between 25 and 77, so your code declares it blue. You probably need to tweak the test numbers in your if statements.

macman20:
For green I'm getting R=165, G=109, B=41
Blue R=147, G=90, B=29

Yes, expected.
And?

Great thank you. I will change the values in the if statements. Is there a way for it to detect black and white colours?

macman20:
Great thank you. I will change the values in the if statements. Is there a way for it to detect black and white colours?

I'd do some experiments and find out.

As a newbie too, I thought a little data gathering would help.
Ask yourself these questions:
What RGB values do I get when I hold up white card?
What RGB values do I get when I hold up black card?

Then tweak your decision making process (your 'if...then' statements to suit.

Thanks I will sit down and do some testing when I get a chance. Studying for an electronic priciples test in the morning :fearful: