Problem with Arduino Uno Color sensor TC3200

Hi guys,

i'm trying to make a simple program with arduino uno and sensor tcs3200. I want to catch the rgb value of a scannerized color, but the output doesn't seem an rgb code because the values are over 255 and they not match with the real color. I think that the values returned are frequencies of the colors. Here is my code:

    /*     Arduino Color Sensing Tutorial
     *      
     *  by Dejan Nedelkovski, www.HowToMechatronics.com
     *  
     */
     
    #define S0 4
    #define S1 5
    #define S2 6
    #define S3 7
    #define sensorOut 8
    int frequency = 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
      frequency = pulseIn(sensorOut, LOW);
      //Remaping the value of the frequency to the RGB Model of 0 to 255
      frequency = map(frequency, 25,72,255,0);
      // Printing the value on the serial monitor
      Serial.print("R= ");//printing name
      Serial.print(frequency);//printing RED color frequency
      Serial.print("  ");
      delay(100);
      // Setting Green filtered photodiodes to be read
      digitalWrite(S2,HIGH);
      digitalWrite(S3,HIGH);
      // Reading the output frequency
      frequency = pulseIn(sensorOut, LOW);
      //Remaping the value of the frequency to the RGB Model of 0 to 255
      frequency = map(frequency, 30,90,255,0);
      // Printing the value on the serial monitor
      Serial.print("G= ");//printing name
      Serial.print(frequency);//printing RED color frequency
      Serial.print("  ");
      delay(100);
      // Setting Blue filtered photodiodes to be read
      digitalWrite(S2,LOW);
      digitalWrite(S3,HIGH);
      // Reading the output frequency
      frequency = pulseIn(sensorOut, LOW);
      //Remaping the value of the frequency to the RGB Model of 0 to 255
      frequency = map(frequency, 25,70,255,0);
      // Printing the value on the serial monitor
      Serial.print("B= ");//printing name
      Serial.print(frequency);//printing RED color frequency
      Serial.println("  ");
      delay(100);
    }

Any suggestions?

  frequency = map(frequency, 25, 72, 255, 0);Where did the magic numbers such as 25 and 72 come from ?

Have you tried printing the value of frequency before mapping it ?

Are you sure that the values mapped to are the correct way round ?

Read this and the documentation that comes with the referenced library.

You should also read the datasheet for the device.

UKHeliBob:   frequency = map(frequency, 25, 72, 255, 0);Where did the magic numbers such as 25 and 72 come from ?

Have you tried printing the value of frequency before mapping it ?

Are you sure that the values mapped to are the correct way round ?

I followed the tutorial on this link http://howtomechatronics.com/tutorials/arduino/arduino-color-sensing-tutorial-tcs230-tcs3200-color-sensor/ and they say that this fuction "represent the detected colors with the RGB Model which has values from 0 to 255" and "The value of 70 will be mapped to 0, and the value of 25 to 255. The same procedure goes for the two other colors."

I scanned a grey color without the map fuction and i get this output:

R= 40 G= 43 B= 13 R= 40 G= 43 B= 14 R= 40 G= 43 B= 13 R= 40 G= 43 B= 14 R= 40 G= 42 B= 13

With the map fuction i get this:

R= 174 G= 200 B= 340 R= 174 G= 200 B= 317 R= 174 G= 200 B= 323 R= 174 G= 200 B= 323

this "rgb" values aren't grey.

marco_c: Read this and the documentation that comes with the referenced library.

You should also read the datasheet for the device.

I already read your blog but i have some perplexity:

i've put the sensor in a black plastic bell and i'm getting different values for the same white or black material, which value i should use to apply the formula to get the rgb value?

i need this sensor to detect specific color from varius leathers, i will get the exact color detected?

Sorry for the questions.

I think that Sensor Color TCS3200 of Arduino uno, is not efficiently for detect a specific color from various leathers. I suggest you to buy an another sensor possibly better.

i'm getting different values for the same white or black material, which value i should use to apply the formula to get the rgb value

Are you using my library or the other code you copied for this? Please test using my library and the example program(s) that come with the library. There is a lot of crappy code for this sensor written by people who don't understand how it works.

is not efficiently for detect a specific color from various leathers.

I disagree. There should be no problem detecting different leather colors with the sensor, PROVIDING that it is calibrated properly and you are not trying to detect shades of the same color (ie, light brown, medium brown or dark brown) as the discrimination is not sensitive enough.

Hi Marco thanks for your help!

I figured out how to run correctly your program "calibrate". I have some issues in catching colors, colors in output seem to have less brightness than they should have. Is it a sensor limit? Or could it be a code problem or a wrong pin connection? When i run the catching program, have the sensor to be in the same position of the calibration? Is there a right distance between the sensor and the material i want to scan?

Here some photos of my plastic bell |281x500

|281x500

Thanks again for your help.

Your hood should keep the same distance for both calibration and measurement. If the inside of the plastic is shiny then this may affect the color reading. It is better if it is matt so that it absorbs any bouncing light from the LEDs.

Not sure what you mean by brightness?

marco_c:
Your hood should keep the same distance for both calibration and measurement. If the inside of the plastic is shiny then this ma affect the color reading. It is better if it is matt so that it absorbs any bouncing light from the LEDs.

Not sure what you mean by brightness?

By brightness i mean luminosity; in fact the caught colors are darker than they should be.

Thank you, now i will try to make a matt hood.

So the luminosity is only relevant if you are looking for the exact color. If you want to discriminate between a number of different samples, then it it the code for the sample that counts rather than the exact match for the rgb value. Also an rgb value seen on a screen will look brighter than the same color on a real object.

marco_c: So the luminosity is only relevant if you are looking for the exact color. If you want to discriminate between a number of different samples, then it it the code for the sample that counts rather than the exact match for the rub value. Also an rgb value seen on a screen will look brighter than the same color on a real object.

The problem is that i need the exact color of the leather, because i've tried to catch different color like 3 shades of blue and it catch only color very near to the rgb value of 0,0,0. I think that the led doesn't lights enought. Inside the plastic bell i've put a black foam rubber.

|281x500

marco_c: Are you using my library or the other code you copied for this? Please test using my library and the example program(s) that come with the library. There is a lot of crappy code for this sensor written by people who don't understand how it works. I disagree. There should be no problem detecting different leather colors with the sensor, PROVIDING that it is calibrated properly and you are not trying to detect shades of the same color (ie, light brown, mediium bnrown or dark brown) as the discrimination is not sensitive enough.

Are you suggesting that the TCS3200 is not suited for detecting shades of colours? If so, then I need to rethink my initial plan for water quality testing with this sensor.

Without calibration I would say there will be little success. With calibration it will entirely depend on how the sensor is set up compared to the sample being measured. It is easier to detect ‘more’ or ‘less’ brown between samples, for example, than to reliably get the exact RGB value for each sample. A lot will depend on what level of discrimination is required.

I have done pretty decent calibration I believe, and I'm able to detect shades of red with pretty good precision. For some reason, which I've tried to figure out all day, when measuring a green surface - frequencies with the blue filter on are higher than green.

Do you have some sort of intuition what this could be due to? I can detect shades of blue as well.

From memory, in the data sheet there is an overlap between the sensors colour detection. Have you looked to see if this could be the problem? Also I would check any stray light or problems with shrouding.

Hi Marco,

My shrouding is pretty decent I would say. Have a smal round piece of white paper around the photo arrays (blocking direct light from the LED's), and I put everything in a box pointing into the room so to prevent direct light from the window. |500x375

I've looked thouroughly in the datasheet, and especially studied this illustration:

|487x500

Now please bare with me here, there are many new facets in this I haven't seen before: this illustration indicates to me, that they have tested the responsivity of the photo diodes from around 350 nm to 1100 nm by shining that light directly on to the photo diodes with a peak value at 715 nm. It's interesting to see how blue and red are very responsive into the IR range. I don't think I have IR in my room, but I could be wrong. This would obviously have a huge impact, no?

In other news, I noticed that the onboard LED's of the breakout I bought might have a slight blue tint https://www.dfrobot.com/product-540.html so I wrote to dfrobot to get the specific partnumber for the LED's, but they obviously have no clue since they just told me "The onboard LEDs are normal RGB LEDs" end of story. So I tried turning off the onbard LED's and using my phones rather strong and more white-ish light. This actually gave me a more sane value when measuring a green object. Now green is more pronounced than blue, but not super much though. |500x375

There's probably some clever way to calibrate for the onboard LED's lack of total whiteness, and if you have a hint or two for me - I would be the happiest man around. Or maybe it's just a plain matter of subtracting some of the blue channel of the final data?

Best Søren

I would use a black shroud, not white. Any reflections off the white will contribute to the color reading. I suppose in the final setup, if you know you have too much blue you just compensate, as you say. I don’t really have much else for you other than what has already been written.

Hi Marco I have a problem with calibration I always get RGB [0,0,0]

I think the problem is that Im not connectd all the pins as they need to be can you please help me with how to connect each pin?

Thanks

You have provided no information on how you have things wired.

Also, it is bad form to hijack someone else’s thread.