Go Down

Topic: Color Sensor Errors (Read 1 time) previous topic - next topic

rascarnascar

Hello, I'm trying to use a parallax color sensor (specs at http://www.parallax.com/Portals/0/Downloads/docs/prod/acc/TCS3200_doc.pdf ) I'm checking the colors' frequencies using the Serial Monitor on my computer. However, I'm only getting a reading for the green color duration. Even then, I'm not getting any variance with different colors. I'm not exactly sure if it's a programming error or if the sensor is malfunctioning, but I would love some help. I think it may have something to do with the outputs(S2 and S3). Thanks for all your help.

Code: [Select]
[
/*optimal distance for calibration is 2.5 in*/
int Out = 13;                 //C Input "the Read"?
int S0= 8;                 //A Output
int S1 = 9;                //B Output
int S2= 10;                  //E Output
int S3 = 12;                 //F Output
int LED = 11;                 //D Output-turns on LEDS

unsigned long REDduration;
unsigned long GREENduration;
unsigned long BLUEduration;
unsigned long CLEARduration;


void setup () {
pinMode(LED,OUTPUT);
pinMode(Out,INPUT);
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);
digitalWrite(LED, HIGH);
Serial.begin(9600);
}

void loop () {
  digitalWrite(S0,HIGH);
  digitalWrite(S1,HIGH);
  color ();
  Serial.print("R");
  Serial.println(REDduration);
  Serial.print("G");
  Serial.println(GREENduration);
  Serial.print("B");
  Serial.println(BLUEduration);
  Serial.print("C");
  Serial.println(CLEARduration);
}

void color () {
digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
REDduration= pulseIn(Out,HIGH);
digitalWrite(S2,LOW);
digitalWrite(S3,HIGH);
BLUEduration= pulseIn(Out,HIGH);
digitalWrite(S2,HIGH);
digitalWrite(S3,LOW);
CLEARduration= pulseIn(Out,HIGH);
digitalWrite(S2,HIGH);
digitalWrite(S3,HIGH);
GREENduration= pulseIn(Out,HIGH);

}]

PaulS

There is already an LED with resistor connected to pin 13. That usually means it's not the best choice for an input pin. Try a different input pin.

rascarnascar

Thank you I did not think of that. I changed it to pin 7, but now it's only reading CLEARduration. Any thoughts?

Msquare

What values do you get? Do they vary when you vary what you point to? Do the vary if you have the illumination LED On/Off?

I have never used this sensor, so I am just guessing here. Firstly I would pull the S0 (using the names in the PDF you refer to) LOW so we divide the output frequency by 50. This may possibly give a slow enough frequency for the pulseIn() to return a more usefull value.
The example code in the PDF counts pulses, the Arduino measures pulsewidth. Assuming the same wave shape, then at higher frequencies the pulsewidth should be narrower.
I do not like using interrupts but this may be the right place to do it, to get a count of pulses rather than the pulswidth - at the possible 600Khz that is 2 microseconds which pulsewidth() may/may not catch.

rascarnascar

Thanks for the help. I worked it out now. I just disconnected the S0 and S1 inputs. I know this is probably not correct, but I'm getting consistent readings. If it starts malfunctioning again, I'll be sure and try your suggestions!

Go Up