Inconsistent Sensor TCS230 reading

Greetings, I'm making my first ever Arduino project. I connect my 3x TCS230 in my Arduino Mega 2560. The problem is I'm trying to get the perfect value of each color but every time pass by it keep changing the values, i already cover the sensor that only focus in the color to minimize light ambient. When i start recording the value in the morning but the value changed in afternoon.

Here's the code:

/*
  RGB Color Sensor Demonstration
  rgb-color-sensor-demo.ino
  Read RGB values from Color Sensor
  Must use calibration values from Color Sensor Calibration Sketch

  DroneBot Workshop 2020
  https://dronebotworkshop.com
*/

// Define color sensor pins

#define S1 4
#define S0 5
#define S3 6
#define S2 7
#define sensorOut 8

// Calibration Values
// Get these from Calibration Sketch

int redMin =170; // Red minimum value
int redMax =609; // Red maximum value
int greenMin =238; // Green minimum value
int greenMax =705; // Green maximum value
int blueMin =244; // Blue minimum value
int blueMax =530; // Blue maximum value

// Variables for Color Pulse Width Measurements

int redPW = 0;
int greenPW = 0;
int bluePW = 0;

// Variables for final Color values

int redValue;
int greenValue;
int blueValue;

void setup() {

  // Set S0 - S3 as outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Set Sensor output as input
  pinMode(sensorOut, INPUT);
  
  // Set Frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Setup Serial Monitor
  Serial.begin(9600);
}

void loop() {
  
  // Read Red value
  redPW = getRedPW();
  // Map to value from 0-255
  redValue = map(redPW, redMin,redMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Read Green value
  greenPW = getGreenPW();
  // Map to value from 0-255
  greenValue = map(greenPW, greenMin,greenMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Read Blue value
  bluePW = getBluePW();
  // Map to value from 0-255
  blueValue = map(bluePW, blueMin,blueMax,255,0);
  // Delay to stabilize sensor
  delay(200);
  
  // Print output to Serial Monitor

  Serial.print("Red = ");
  Serial.print(redValue);
  Serial.print(" - Green = ");
  Serial.print(greenValue);
  Serial.print(" - Blue = ");
  Serial.println(blueValue);

  if((redValue > 270) && (greenValue > 225) && (blueValue > 170))
    {
      Serial.println("YELLOW");
      delay(200);
    }  

  else if((redValue > 280 && redValue < 295) && (greenValue > 164 || greenValue < 183) && (blueValue > 180 && blueValue < 210))
    {
        Serial.println("Orange");
        delay(200);
    } 


  else if((redValue > 260 && redValue < 286) && (greenValue > 88 && greenValue < 155 ) && (blueValue > 140 && blueValue < 215))
    {
      Serial.println("Red");
      delay(200);
    }

  else if((redValue > 109 && redValue < 172) && (greenValue > 28 && greenValue < 132) && (blueValue > 123 && blueValue < 230 ))
    {
      Serial.println("Violet");
      delay(200);
    }

  else if((redValue > 76 && redValue < 113) && (greenValue > 79 && greenValue < 141) && (blueValue > 150 && blueValue < 199))
    {
      Serial.println("Blue");
      delay(200);
    }

  else if((redValue > 18 && redValue < 55) && (greenValue > 65 && greenValue < 99) && (blueValue > 48 && blueValue < 89))
    {
      Serial.println("Green");
      delay(200);
    }

  else if((redValue > 240 && redValue < 258) && (greenValue > 131 && greenValue < 162) && (blueValue > 161 && blueValue < 199))
    {
      Serial.println("Maroon");
      delay(200);
    }

  else if((redValue > 9 && redValue < 30) && (greenValue > 16 && greenValue < 37) && (blueValue > 17 && blueValue < 49))
    {
      Serial.println("Black");
      delay(200);
    }


  else
    {
      Serial.println("ERROR");  
      delay(200);    
    }
  
}


// Function to read Red Pulse Widths
int getRedPW() {

  // Set sensor to read Red only
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Green Pulse Widths
int getGreenPW() {

  // Set sensor to read Green only
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

// Function to read Blue Pulse Widths
int getBluePW() {

  // Set sensor to read Blue only
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  // Define integer to represent Pulse Width
  int PW;
  // Read the output Pulse Width
  PW = pulseIn(sensorOut, LOW);
  // Return the value
  return PW;

}

Thank you!

You may want to read this before you proceed:-
how to get the best out of this forum
It tells you how to ask a question and what information to supply.

In this case please supply a schematic, hand drawn is fine Fritzing is not. Also please do not post code as an image, use the correct code tabs, as explained in the above link.

The phrase "power frequency" is not the right term for what you get from these sensors.

This link might help you
Using the TECS230 sensor

1 Like

Topics merged as requested. Best wishes for success with your project @garex331.

Regards,
pert

1 Like

What makes you think it should be the same?

Yes, i would like to make each sensor has same frequency reading as you can see the image their frequency reads not the same and it is divided.

I think you are missing the point here. The frequency you get back from the sensor is a representation of the colour the sensor detects. They can't send exactly the same unless all the sensors receive an equal colour component from source of reflected light. That will only happen when there is some sort of special tint of white colour.

The fact that the readings are not stable is due to changes in ambient light as explained in the link I sent you.

1 Like

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