Lesson 32 Color Detection

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

The Color Sensor is a complete color detector. It consists of a TAOS TCS3200 RGB sensor chip and 4 white LEDs. It can detect and measure a nearly limitless range of visible colors to a certain degree.

Components

– 1 * SunFounder Uno board

– 1 * USB data cable

– 1 * Color sensor

– 1 * 4-Pin anti-reverse cable

– 1 * 3-Pin anti-reverse cable

Experimental Principle

The color of an object we can see in fact is the chromatic light the object reflects in the white light (sunlight) after it absorbs the rest ones. The white color is a mixture of various visible colors, which means it includes each colored light like red (R), yellow (Y), green (G), blue (B), and purple (P). Based on the theory of three primary colors, any color is made by mixing the three primary colors (red, green, and blue) in a certain proportion.

Thus, knowing the proportion you can get the color of the tested object. For TCS3200, when a color filter is selected, it only allows a specific primary color to pass through and blocks the other two colors. With the light intensity value of the three primary colors, by analysis we can know the color of light reflected to TCS3200D.

The TCS3200D sensor has 4 types of filter: red filter, green filter, blue filter, and clear with no filter. The filter is selected based on the high/low of pin S2 and S3 on the module (see the table below):

S2S3Photo Diode Type
LLRed
LHBlue
HLClear (no filter)
HHGreen

The module contains a programmable converter which transforms colorlight to frequency. When the part of RGB colors in the light reflected by the object passes through the filter selected to the TAOS TCS3200RGB chip, the built-in oscillator outputs square waves. The frequency of the waves is directly proportional to the light intensity – the more intense the light is, the higher is the frequency. Also, the frequency of the OUT pin on the sensor module is proportional to that of the oscillator; the proportion depends on the high/low of pin S0 and S1, as shown in the table.

S0S1OUTPUT Frequency Scaling (f0)
LLPower Down
LH2%
HL20%
HH100%

The schematic diagram of the module:

Experimental Procedures

Step 1: Build the circuit

The wiring between the color sensor and SunFounder Uno board:

Color SensorSunFounder Uno
GNDGND
VCC5V
OUT2
S03
S14
S25
S36

Step 2: Program (Please refer to the example code in LEARN -> Get Tutorial on our website)

Note: Here you need to add a library. Refer to the description in Lesson 1 previously in the manual.

Step 3: Compile

Step 4: Upload the sketch to SunFounder Uno board

Now you may check the RGB value of the color on Serial Monitor. 

 Code

/*********************************************************
name:Color Detection
function: you may see the color RGB value in the serial monitor
connection:
color sensor     uno r3
S0                 3
S1                 4
S2                 5
S3                 6
OUT                2
GND               GND
VCC                5V
******************************************************/
//Email:support@sunfounder.com
//website:www.sunfounder.com#include <TimerOne.h>
#define S0 3 // Please notice the Pin’s define
#define S1 4
#define S2 5
#define S3 6
#define OUT 2int g_count = 0; // count the frequecy
int g_array[3]; // store the RGB value
int g_flag = 0; // filter of RGB queue
float g_SF[3]; // save the RGB Scale factor// Init TSC230 and setting Frequency.
void TSC_Init()
{
  //set the pins as OUTPUT
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  pinMode(OUT, INPUT);
  // OUTPUT Frequency Scaling 2%
  digitalWrite(S0, LOW);
  digitalWrite(S1, HIGH);
}// 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 output Red
      break;
    case 1:
      Serial.print(“->Frequency R=”);
      Serial.println(g_count);
      g_array[0] = g_count;
      TSC_WB(HIGH, HIGH); //Filter output Green
      break;
    case 2:
      Serial.print(“->Frequency G=”);
      Serial.println(g_count);
      g_array[1] = g_count;
      TSC_WB(LOW, HIGH); //Filter output Blue
      break;
    case 3:
      Serial.print(“->Frequency B=”);
      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);//1000000 microseconds(1s)
}
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]);
}
void loop()
{
  g_flag = 0;
  for(int i=0; i<3; i++)
  Serial.println(int(g_array[i] * g_SF[i]));
  delay(4000);
}