All post of ElecFreaks

TSC230 Color Sensor Demo for Arduino

By September 7, 2011 39 Comments
TSC230

The TSC230/3200 is Programmable Color Light-to-Frequency Converter Module, which we have in stock about a week. It can be controlled in many ways, one of which is Arduino. We tried and tested it, then made a demo. Let’s see how it works with Arduino.

First of all, of course material preparation.  The ChipKit which you need one of  Arduino main board and some of F/F jumper wire(3 pin & 4 pin &5 pin) andTSC230 Color Snsor Module.  From now on, we provide two types of the TSC230 module with different layout. Here we use the white Color Sensor Module for the demo.

The module works at 5V, you can use Arduino for power supply directly. The White Color Sensor has shorted   LED to GND  by a short circuit Block Cap. The four white highlight LEDs in the surrounding are providing a source constant light.

The Demo pins to Arduino as below:

GND & LED – GND ,  OE – GND,  VCC – VCC, S0 – D6,  S1 – D5,  S2 – D4,  S3 – D3,  OUT – D2


#define S0     6
#define S1     5
#define S2     4
#define S3     3
#define OUT    2

Here OE connects to GND that means the module was always enable. The OUT(Signal pin) is output of a square wave (50% duty cycle) with frequency directly proportional to light intensity (irradiance).  Most Arduino boards have two external interrupts: numbers 0 (on digital pin 2) and 1 (on digital pin 3). So we connect OUT to D2 for counting the square wave frequency using external interrupt 0.

So you need an other timer library which is a collection of routines for configuring the 16 bit hardware timer called Timer1 on the ATmega168/328 .  Get the Timer1 Library.

The initialization function of the demo as below:


// Init TSC230 and setting Frequency.
void TSC_Init()
{
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);

  digitalWrite(S0, LOW);  // OUTPUT FREQUENCY SCALING 2%
  digitalWrite(S1, HIGH);
}

The S0, S1 is output frequency scaling selection inputs. S0 – L &  S1 – H is output frequency scaling to 2%.  The reason of setting the scale factor is  adapt to the different measurement range and improve the measurement ability with different MCU.  The S2, S3 control the filter of RGB.

Then is the important part of the demo code – Adjust the WB(white balance) .


// Select the filter color
void TSC_FilterColor(int Level01, int Level02)
{
  if(Level01 != 0)
    Level01 = HIGH;

  if(Level02 != 0)
    Level02 = HIGH;

  digitalWrite(S2, Level01);
  digitalWrite(S3, Level02);
}

void TSC_Count()
{
  g_count ++ ;
}

void TSC_Callback()
{
  switch(g_flag)
  {
    case 0:
         Serial.println("->WB Start");
         TSC_WB(LOW, LOW);              //Filter without Red
         break;
    case 1:
         Serial.print("->Frequency R=");
         Serial.println(g_count);
         g_array[0] = g_count;
         TSC_WB(HIGH, HIGH);            //Filter without Green
         break;
    case 2:
         Serial.print("->Frequency B=");
         Serial.println(g_count);
         g_array[1] = g_count;
         TSC_WB(LOW, HIGH);             //Filter without Blue
         break;

    case 3:
         Serial.print("->Frequency G=");
         Serial.println(g_count);
         Serial.println("->WB End");
         g_array[2] = g_count;
         TSC_WB(HIGH, LOW);             //Clear(no filter)
         break;
   default:
         g_count = 0;
         break;
  }
}

void TSC_WB(int Level0, int Level1)      //White Balance
{
  g_count = 0;
  g_flag ++;
  TSC_FilterColor(Level0, Level1);
  Timer1.setPeriod(1000000);             // set 1s period
}

void setup()
{
  TSC_Init();
  Serial.begin(9600);
  Timer1.initialize();             // defaulte is 1s
  Timer1.attachInterrupt(TSC_Callback);
  attachInterrupt(0, TSC_Count, RISING);

  delay(4000);

  for(int i=0; i<3; i++)
    Serial.println(g_array[i]);

  g_SF[0] = 255.0/ g_array[0];     //R Scale factor
  g_SF[1] = 255.0/ g_array[1] ;    //G Scale factor
  g_SF[2] = 255.0/ g_array[2] ;    //B Scale factor

  Serial.println(g_SF[0]);
  Serial.println(g_SF[1]);
  Serial.println(g_SF[2]);

}

WB is to tell the system what is white. In theory, composed of equal amounts of  RGB is white, but in fact RGB component values ​​are not equal.  For TSC230 module  the sensitivity of the three light is different,  which leading to RGB value different. So before you use it must be adjusted WB.

The method  of adjust the WB  is in a fixed time(here used 1S) statistics RGB component’s pulses, then calculate a scale factor, this scale factor can be transformed the pulses count to 255.  In the actual test, we calculate  the number of pulses at same fixed time and multiply by the scaling factor and then get the object’s  RGB values.

Here we use the Timer1 library to set a callback function –   void TSC_Callback() , for getting the RGB value.  Pin D2(OUT)  count the trigger when the pin goes from low to high: attachInterrupt(0, TSC_Count, RISING); At last, save the RGB value to g_array[] and  calculate out the scale factor save to g_SF[].

Note: The first use or restart or change light and so on, please adjust the WB(White Balance) again.

OK,  Now we can test the light color from the demo code and print out the RGB value.


void loop()
{
   g_flag = 0;
   for(int i=0; i<3; i++)
    Serial.println(int(g_array[i] * g_SF[i]));
   delay(4000);

}

First, after we have did all of previous. The  result of the monitor as below:

As the figure, the mark 1 is the  RGB value at 1S period.   The RGB value is Red=1126 , Green=707 and Blue =774. Then the mark 2 is  the scale factor. Then use the scale factor for adjusting the RGB value to 255 as Mark 3.

Then we  measure objects in color, keep the objects on the module top 3s and the result from monitor as below:

As the figure, the Mark 4 is object’s RGB value.  Now you have got the RGB value it means you have got the object color.

Left is your own personal test and enjoy it.  This file is a sample code for your reference.Just a demo for TSC230 Programmable Color Light-to-Frequency Converter Module. Thank you very much.

Download the TCS230 Demo for Arduino

 

 

(here is setting 1S)
VN:F [1.9.13_1145]
Rating: 7.6/10 (10 votes cast)
VN:F [1.9.13_1145]
Rating: +2 (from 2 votes)
TSC230 Color Sensor Demo for Arduino, 7.6 out of 10 based on 10 ratings
robi

Author robi

ElecFreaks - Community for electronics enthusiasts, provide all you need.

More posts by robi

Join the discussion 39 Comments

  • Excellent post. I was checking constantly this blog and I’m impressed!
    Very useful info specially the last part 🙂 I care for such info much.
    I was seeking this certain information for a very long time.
    Thank you and good luck.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Hi, constantly i used to check website posts here in the early
    hours in the daylight, as i enjoy to gain knowledge of more and more.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Very rapidly this web page will be famous among all blogging
    users, due to it’s good content

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • QQ says:

    Simply want to say your article is as astonishing. The clarity on your publish is simply great
    and that i can think you are a professional on this subject.
    Fine together with your permission let me to clutch your RSS feed to stay
    up to date with forthcoming post. Thanks 1,000,000 and please continue the rewarding work.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Why users still use to read news papers when in this
    technological globe the whole thing is presented on net?

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • ZU says:

    Hi, the whole thing is going sound here and ofcourse every one is
    sharing data, that’s genuinely good, keep up writing.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • apachi says:

    Thanks for some other great post. The place else may just anybody get that type
    of info in such an ideal means of writing? I have a presentation next week, and I am on the search for
    such info.

    [Reply]

    clark Reply:

    We are so glad to hear you say so. If you need some help, you can send an email to louise@elecfreaks.com .

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • After looking into a few of the articles on your web site, I seriously like
    your way of blogging. I saved it to my bookmark website list and will be checking back
    in the near future. Please visit my website as well and tell me how you feel.

    [Reply]

    clark Reply:

    Thanks for your comment. If you need our help, you can send an email to louise@elecfreaks.com . Thanks again!

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • If some one wants expert view on the topic of blogging after that i propose him/her to pay a visit this website, Keep
    up the pleasant job.

    [Reply]

    clark Reply:

    Thanks very much for your comment. We will work harder to serve you more better contents!

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Hi to every one, it’s genuinely a good for me to go to see this web site, it includes
    important Information.

    [Reply]

    clark Reply:

    Thanks very much for your comments. If you need help, you can send an email to louise@elecfreaks.com .

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • No matter if some one searches for his vital thing, thus he/she desires to be available that in detail,
    therefore that thing is maintained over here.

    [Reply]

    clark Reply:

    Thanks for your comment! We will work harder to present you more good contents!

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Nice post. I learn something new and challenging on websites I stumbleupon everyday.
    It’s always useful to read content from other authors and use a little
    something from their websites.

    [Reply]

    clark Reply:

    Thanks for your comment! We will work harder to present you more good contents!

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Simply wish to say your article is as surprising. The clearness to your post
    is just cool and i can assume you are knowledgeable on this subject.
    Well along with your permission allow me to clutch your
    feed to stay updated with coming near near post. Thank you 1,000,000 and please continue the gratifying
    work.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Danny Kamenský says:

    Hi, i have a problem, serial port shows me

    0

    0

    0

    ->WB Start

    ->Frequency R=0

    ->Frequency G=0

    ->Frequency B=0

    ->WB End

    Is it problem that i am using a little bit different sensor TSC230?
    http://tx.lt/379-large_default/color-sensor-tsc230.jpg

    Any idea where the problem is? thank you,

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • dmnc_net says:

    Hi, am I missing some `g_count` `g_flag` and `g_array` declarations? Example code in the link is totally different from this code.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Bryan says:

    Do you mind if I quote a couple of your articles as long as I provide credit and
    sources back to your site? My blog is in the exact same niche
    as yours and my users would truly benefit from a lot of
    the information you present here. Please let me know if this ok with you.
    Appreciate it!

    Here is my webpage: google.ca (Bryan)

    [Reply]

    Kathy Yang Reply:

    Hi Bryan,

    Thanks for your interest. Will contact you via email.

    [Reply]

    VN:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VN:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Stephany says:

    But, the techniques for glass making are still almost the same as when Murano
    artisans invented them through the last ten centuries.
    This season, large, dangling earrings are the most
    popular earring styles. When police confiscate goods from their store, the only option they
    have in getting their money back for the item is to attach
    themselves to the suit against person who stole the item and sold it to them.

    Here is my web blog :: fashion jewelry (Stephany)

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • baldrick says:

    Has anyone actually tried to run this code or the download demo? Didn’t think so.. I did and none of it actually works.
    Thanks anyway for some explanations – perhaps just taken from elsewhere but with only partial understanding.
    I will get this to work myself.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • weight loss says:

    Resale of the improved property could also need certain
    periods to lapse. Inventory finance would require no real estate security and neither will it count
    against your credit line. If you can negotiate the lowest
    price and get zero percent financing on it, than you possess a deal that is
    worth your pursuing.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Michael says:

    Pretty useful, I wish I could get more line of depth as when trying to grab the RGB value from over 20 feet away the data is way off.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: -1 (from 1 vote)
  • http://japan-fishing.net/index.php/利用者:Reedbland says:

    Your mode of explaining the whole thing in this paragraph is in fact
    fastidious, every one be able to effortlessly be aware of it, Thanks
    a lot.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • huddlebuy says:

    I’m really impressed with your writing skills and also with the layout on your weblog. Is this a paid theme or did you modify it yourself? Anyway keep up the excellent quality writing, it is rare to see a nice blog like this one today.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • astigmatism toric contact lenses says:

    Nice blog right here! Also your website lots up fast! What
    host are you the use of? Can I get your associate hyperlink in your host?
    I desire my website loaded up as fast as
    yours lol

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • http://vocamus.net says:

    It’s a shame you don’t have a donate button!
    I’d certainly donate to this brilliant blog! I guess for now i’ll settle for book-marking and adding your RSS feed to my Google
    account. I look forward to brand new updates and will share
    this blog with my Facebook group. Talk soon!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Oscar says:

    Mea. Kingdom: Animalia. The funny thing about working with i – Work.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • hair plugs says:

    I read this paragraph fully about the comparison of hottest and previous
    technologies, it’s amazing article.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • led matrix Driver ic says:

    The exact characteristics can be determined in later steps if not known.
    Sure, the companies running those social media sites feel
    they have control ‘ much the same way a race driver feels he has control of a car barreling down a track at over 200 mph. You can also use the travel charger included in the kit while travelling and enjoy music in your car with the help of USB car charger.

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Jim Jose says:

    thanks a lot for the code but can u tell how i can turn off the leds when not needed?
    also the display is having a 1 sec delay rt?
    im using the color sensor to follow a rc race track, so basically i need to get the values at a faster rate , can u tell me how?

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Jim Jose says:

    thanks a lot for the code but can u tell how i can turn off the leds when not needed?
    also the display is having a 1 sec delay rt?
    im using the color sensor to follow a rc race track, so should detect white color and yellow color lines and move away from it as required, but if there is a 1s delay for this color sensing alone the rc is not efficient enought, coz it has to control the servo and check the value from US distance senso,

    basically i need to get the values at a faster rate , can u tell me how?

    [Reply]

    Reyyan Reply:

    I also need a fast response time (in microseconds). help me!

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Adriana says:

    I have the same question to Denorro Stallworth

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.13_1145]
    Rating: 0 (from 0 votes)
  • Denorro Stallworth says:

    What is the math you are using to go from the frequency to get the rgb value? I see you have a frequency of 1126 for red, then some value of 0.23, then all of suddent you have 255 or the r value of rgb. So what equation did you use to get the 255 for r?

    For instance you have:
    Frequency r = 1126,g=774,b=707
    .23 , .33 , .36

    Then all of sudden you have: 255, 255, 254 (how did you get these #’s)

    [Reply]

    VA:F [1.9.13_1145]
    Rating: 5.0/5 (1 vote cast)
    VA:F [1.9.13_1145]
    Rating: +2 (from 2 votes)

Leave a Reply