Go Down

Topic: TCS3200 Sensor - possible saturation, or coding error (producing low values) (Read 57 times) previous topic - next topic

lennon-pledge

Originally when I started to fiddle with the TCS3200 sensor, the numbers of the RGB frequencies were incomprehensible but had great variability when I introduced new colors to analyze.  The slightest difference in shade of yellow, for example, triggered a good response from the sensor.  It wasn't the actual RGB value, but a definite change in frequency was noticed.

Now, running the same setup, the frequency values are very low with minimal variability when introducing different colors (red to blue (a big jump), green to orange, etc.). 

I used two different Arduino Mega 2560's just to be sure there was nothing wrong with the development board.  However, I switched the original TCS3200 I was using to a new one straight out of the bag, and the same poor results showed...which struck me as odd (if the original sensor had been saturated, the second would have worked fine.)

Here's the code...there could be something wrong in there for sure, but to my knowledge, it doesn't seem to be a problem.  I'm using the result of the "pulseIn()" function as a "float" value, not an "unsigned int".  I haven't noticed any difference in terms of actual value.

Code: [Select]
int LED = 2, S0 = 7, S1 = 6, S2 = 5, S3 = 4, OUT = 3;


void setup() {
  // put your setup code here, to run once:
  TCS3200_Setup();
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  generateRGBValues();
  delay(250);
}

void generateRGBValues() {
  float freqClear, freqRed, freqGreen, freqBlue;
  TCS3200_On();
  freqClear = NoFilter();
  freqRed = RedFilter();
  freqGreen = GreenFilter();
  freqBlue = BlueFilter();
  TCS3200_Off();
  float distance = sqrt(pow(freqRed,2.0) + pow(freqGreen,2.0) + pow(freqBlue,2.0));
  Serial.print("r:");
  Serial.print(freqRed);
  Serial.print(" g:");
  Serial.print(freqGreen);
  Serial.print(" b:");
  Serial.print(freqBlue);
  Serial.print("   Dist:");
  Serial.println(distance);
}

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);
}

float NoFilter() {
  digitalWrite(S2,HIGH); // select no filter
  digitalWrite(S3,LOW);
  //delay(5);
  return float(pulseIn(OUT,LOW, 10000));
}

float RedFilter() {
  digitalWrite(S2,LOW); // select red filter
  digitalWrite(S3,LOW);
  //delay(5);
  return float(pulseIn(OUT,LOW, 10000));
}

float GreenFilter() {
  digitalWrite(S2,HIGH); // select green filter
  digitalWrite(S3,HIGH);
  //delay(5);
  return float(pulseIn(OUT,LOW, 10000));
}

float BlueFilter() {
  digitalWrite(S2,LOW); // select blue filter
  digitalWrite(S3,HIGH);
  //delay(5);
  return float(pulseIn(OUT,LOW, 10000));
}




Here is some output...somewhere in the data there's a transition from analyzing red to analyzing blue.  In terms of frequency, it's like nothing happened.  Please take note, I'm not printing out actual RGB values based on the 0-255 scale....these are the frequencies.  Since I can't get good readings anymore, I have not bothered converting my output to the RGB scale...that is not the issue.

Code: [Select]
r:6.00 g:13.00 b:11.00   Dist:18.06
r:9.00 g:16.00 b:15.00   Dist:23.71
r:8.00 g:15.00 b:15.00   Dist:22.67
r:8.00 g:15.00 b:15.00   Dist:22.67
r:9.00 g:16.00 b:16.00   Dist:24.35
r:10.00 g:19.00 b:17.00   Dist:27.39
r:8.00 g:20.00 b:16.00   Dist:26.83
r:4.00 g:12.00 b:9.00   Dist:15.52
r:4.00 g:12.00 b:9.00   Dist:15.52
r:4.00 g:15.00 b:11.00   Dist:19.03
r:4.00 g:13.00 b:10.00   Dist:16.88
r:4.00 g:14.00 b:10.00   Dist:17.66
r:4.00 g:13.00 b:10.00   Dist:16.88
r:5.00 g:14.00 b:11.00   Dist:18.49
r:4.00 g:14.00 b:10.00   Dist:17.66
r:5.00 g:13.00 b:10.00   Dist:17.15
r:4.00 g:13.00 b:10.00   Dist:16.88
r:5.00 g:13.00 b:10.00   Dist:17.15
r:4.00 g:11.00 b:9.00   Dist:14.76
r:4.00 g:12.00 b:9.00   Dist:15.52
r:8.00 g:17.00 b:15.00   Dist:24.04
r:6.00 g:12.00 b:11.00   Dist:17.35
r:7.00 g:12.00 b:11.00   Dist:17.72
r:9.00 g:10.00 b:8.00   Dist:15.65
r:9.00 g:9.00 b:6.00   Dist:14.07
r:11.00 g:9.00 b:6.00   Dist:15.43
r:11.00 g:9.00 b:6.00   Dist:15.43
r:11.00 g:9.00 b:5.00   Dist:15.07
r:10.00 g:8.00 b:5.00   Dist:13.75
r:11.00 g:8.00 b:6.00   Dist:14.87
r:12.00 g:9.00 b:6.00   Dist:16.16
r:11.00 g:8.00 b:5.00   Dist:14.49
r:11.00 g:9.00 b:6.00   Dist:15.43

Go Up