Color sensor TSC230 2/4 led lights off? And negative values

Hi

I’m supposed to make a project with the TSC320 color sensor ( data sheet: https://goo.gl/tfZQ2B ) which I bought online.

Issue is, the sensor have 4 light leds on it, but only 2 get ON while the other 2 are always OFF no matter how I connect the sensor.

As each led has its legs surfacing the other side of the “board?” I touched the off leds legs with the GND and VCC cables inverted and got ON. Don’t know if the leds got inverted or I need to connect the sensor in another way.

I have already followed 4 tutorials and the frequencies seems normal until I map them to

map(variable, x, x, 255,0);

which results in negative or over 255 values. Don’t know if has something to do with the off leds.

It’s a nightmare by the time as I need this working for the end of the week, as this is going to be my exam/project =/

The 4 tutorials I had followed are:

  1. Arduino Color Sensing Tutorial - TCS230 TCS3200 Color Sensor - HowToMechatronics
  2. http://engmousaalkaabi.blogspot.mx/2015/12/color-sensor-using-arduino-and-tcs230.html
  3. http://www.circuitstoday.com/arduino-and-color-rgb-sensor
  4. Arduino Color Sensor TCS230 TCS3200 | Random Nerd Tutorials

Hope you know something about this sensor. Thanks.

Hi, Can you post your complete code please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also look at this link to see how to use map.

https://www.arduino.cc/reference/en/language/functions/math/map/

Tom... :)

TomGeorge: Hi, Can you post your complete code please?

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Also look at this link to see how to use map.

https://www.arduino.cc/reference/en/language/functions/math/map/

Tom... :)

Of course =)

This is the code from the example I like the most because is more clear to understand for me:

int s0_pin = 4;
int s1_pin = 5;
int s2_pin = 7;
int s3_pin = 6;
int out_pin = 8;

void setup() {
  Serial.begin(9600);
  
  pinMode(s0_pin, OUTPUT);
  pinMode(s1_pin, OUTPUT);
  pinMode(s2_pin, OUTPUT);
  pinMode(s3_pin, OUTPUT);
  
  pinMode(out_pin, INPUT);
  
  digitalWrite(s0_pin,HIGH);
  digitalWrite(s1_pin,LOW);
}


void loop() {
  digitalWrite(s2_pin,LOW);
  digitalWrite(s3_pin,LOW);
  int red_color = pulseIn(out_pin, LOW);
  red_color = map(red_color, 25,72,255,0);
  delay(50);
  
  digitalWrite(s2_pin,HIGH);
  digitalWrite(s3_pin,HIGH);
  int green_color = pulseIn(out_pin, LOW);
  green_color = map(green_color, 30,90,255,0);
  delay(50);
  
  digitalWrite(s2_pin,LOW);
  digitalWrite(s3_pin,HIGH);
  int blue_color = pulseIn(out_pin, LOW);
  blue_color = map(blue_color, 25,70,255,0);
  delay(50);
  
  Serial.print("RED: ");
  Serial.print(red_color);
  Serial.print("  ");
  
  Serial.print("GREEN: ");
  Serial.print(green_color);
  Serial.print("  ");

  Serial.print("BLUE: ");
  Serial.print(blue_color);
  Serial.println("  ");
  
  delay(1000);
}

This is the TSC230 color sensor I'm using, the left leds are always OFF but light on if I touch their legs inverted with the VCC and GND cables.

My connection goes as follows:

sensor - arduino
S0 = 4
S1 = 5
S2 = 7
S3 = 6
OUT = 8
GND = GND
VCC = 5V

And the circuit:

|447x500

I'm reading the map info you shared right now, thanks so much =)

The white LEDs are intended for illuminating the object, so it seems two of them were wired backwards.

which results in negative or over 255 values. Don't know if has something to do with the off leds.

Nope. What do you suppose the map() function is doing, and why is it there? Where do "magic numbers" like 25 and 72 come from?

You may find this blog entry on TSC230 calibration helpful.

I touched the off leds legs with the GND and VCC cables inverted and got ON.

While that yielded useful information, poking around with Vcc and GND is generally a very bad idea. Without a current limiting resistor, you will often destroy components doing this.

Hi

I had already read that post but didn't really understand that equation on the practical case =( But i understand the map() function.. While don't know why it prints negative and over 255 values.

I will definitively don't poke around anymore, thanks =s

But i understand the map() function.. While don't know why it prints negative and over 255 values.

Your understanding of the map function is short of the full truth.

Again, please explain what you are trying to accomplish with map() , and where the "magic numbers" come from.

Hint: if you study the linked blog post long enough to understand it, then be assured that map() will accomplish the same thing, if used properly.

Hi, I think you need to realise that map does not set constraints. for example

val = map( input , 25 , 100, 0, 1000)

if; input = 25, val = 0 if input = 0, val = -333 if input = 75, val = 666 if input = 110, val = 1133

So even though you put positive values in, map will give you negative values and outside the values you specify in map.

You need to know more about the variable you are mapping.

Tom... :)

As I understand map() makes a conversion between values as a "three rule"

The color frequency values goes from 300s to 500s so I use map to convert those values in a range of 0 to 255..

TomGeorge pointed out map() does not constraint values, which I didn't knew.. So i'm lost..

This is the third time I touch an Arduino and I'm supposed to do a color recognition program.

  1. get the frequency value from sensor
  2. convert to RGB value
  3. convert to hex value (if possible)
  4. print value

I have no idea what to do.. Every tutorial I found results in the same thing, and none explains how to really calibrate or convert the frequency values. Add the leds that doesn't light up, and I'm feeling frustrated..

Thanks for your help, is helping me =)

and none explains how to really calibrate or convert the frequency values.

What do you NOT understand in this blog entry on TCS230 calibration.

Doesn't the black card/white card approach to calibration make sense?

Hi,

The color frequency values goes from 300s to 500s so I use map to convert those values in a range of 0 to 255..

so why

 red_color = map(red_color, 25,72,255,0);

instead of

 red_color = map(red_color, 300,500,0,255);

Also look up.

https://www.arduino.cc/reference/en/language/functions/math/constrain/

Tom... :)

TomGeorge: Hi,

so why

 red_color = map(red_color, 25,72,255,0);

instead of

 red_color = map(red_color, 300,500,0,255);

...

Tom... :)

That was what I was missing, THANKS! It seems so simple now I feel embarrased ._." The values now range from 0-255 as expected with some peaks lows -4 highs 258 but that is reasonable and i can constrain it with code.

I changed the TSC230 color sensor and this one has all leds light up, that helps to the value results too.

At the moment, I have printed the RGB and HEX values, and I'm going to implement a push button, so the recognition starts at that moment for one time instead of constantly be reading and printing values.

I didn't understand the equation yet.. But I keep reading it and trying.

Thanks for your help and patience to everyone that has commented =)

That was what I was missing, THANKS!

Which is exactly why I asked you to explain that line of code, in reply #2.