Earn Maker Points on Every Order! Learn More!

TCS3200 Color Sensor Tutorial | Arduino | TCS230

Back to Blog
TCS3200 Color Sensor Tutorial | Arduino | TCS230 - TCS3200 Color Sensor Tutorial | Arduino | TCS230 - TCS3200 Color Sensor Tutorial | Arduino | TCS230 - TCS3200 Color Sensor Tutorial | Arduino | TCS230

TCS3200 Color Sensor Tutorial | Arduino | TCS230

In this tutorial, we will see how to use TCS3200/TCS230 color sensor with Arduino. This color sensor is used in wide range of applications like conveyors, food processing units, paint mixing applications, Vending machines and many more. So let’s see how it works and how to use it with Arduino.

At the center of the sensor, you can able to see the TCS3200 Chip. It consists of 8×8 arrays of photodiodes (Total 64 photodiodes). There is a current to frequency converter that converts the sensed current from photodiode into a square wave frequency. This frequency determines the color of the object in front of TCS3200.

There are 4 White LEDs around the TCS3200 chip is to apply a plain white color on the object and the reflected amount of color is detected by photodiodes.

Block Diagram

TCS3200 Color sensor block

As you can see the block diagram of TCS3200 color sensor, there are 64 diodes of each colors Red, Blue, Clear and Green. These can be configured by S2 and S3 Pin of this module to read a specific color. In the current to frequency converter, the output from sensor is converted into frequency. Here, the S0 and S1 pins are used to scale the frequency according to different microcontroller speeds. The table is given below to configure these modes,

Frequency Scaling Configuration

S0S1Output Frequency Scaling
LLPower Down
LH2%
HL20%
HH100%

 

Type of Photodiode (RGB)

S2S3Photodiode Type
LLRed
LHBlue
HLClear (no filter)
HHGreen

 

We will read all the values by setting the S2 and S3 Pins one by one and store in the variable. By using this variable you can print it on the serial monitor or perform some actions like turning a servo for color sorting machines.

Circuit Diagram:

In our example, we will use an RGB LED to lit up the color which is detected by the Color Sensor. This code will detect only the three primary colors RED, GREEN and BLUE. If you want to detect a specific color you can extend it by the color values.

Note: Here, we are using Common Anode RGB LED so I have connected 5V at the Common terminal. If your RGB LED is common Cathode you should connect it to Ground.

RGB LED Pinout:

RGB-LED-Pinout-factoryforward

Tcs3200 Color sensor Arduino

Code:

/*

// TCS230 & 3200 color recognition sensor

// Sensor connection pins to Arduino are shown in comments

Color Sensor      Arduino

-----------      --------

 VCC               5V

 GND               GND

 s0                8

 s1                9

 s2                12

 s3                11

 OUT               10

 OE                GND

*/

const int s0 = 8; 

const int s1 = 9; 

const int s2 = 12; 

const int s3 = 11; 

const int out = 10;


// LED pins connected to Arduino

int redLed = 2; 

int greenLed = 3; 

int blueLed = 4;

// Variables 

int red = 0; 

int green = 0; 

int blue = 0; 

void setup()  

{ 

  Serial.begin(9600);

  pinMode(s0, OUTPUT); 

  pinMode(s1, OUTPUT); 

  pinMode(s2, OUTPUT); 

  pinMode(s3, OUTPUT); 

  pinMode(out, INPUT); 

  pinMode(redLed, OUTPUT); 

  pinMode(greenLed, OUTPUT); 

  pinMode(blueLed, OUTPUT);


  //Setting frequncy to 100%

  digitalWrite(s0, HIGH); 

  digitalWrite(s1, HIGH); 

} 


void loop()

{ 

  color();

  Serial.print("R Intensity:"); 

  Serial.print(red, DEC); 

  Serial.print(" G Intensity: "); 

  Serial.print(green, DEC); 

  Serial.print(" B Intensity : "); 

  Serial.print(blue, DEC); 

  //Serial.println(); 


/* This is for common Anode LEDs, Change HIGH and LOW to opposite for Common Cathode*/

  if (red < blue && red < green && red < 20)

  { 

   Serial.println(" - (Red Color)"); 

   digitalWrite(redLed, LOW); // Turn RED LED ON

   digitalWrite(greenLed, HIGH); 

   digitalWrite(blueLed, HIGH); 

  } 



  else if (blue < red && blue < green)  

  { 

   Serial.println(" - (Blue Color)"); 

   digitalWrite(redLed, HIGH); 

   digitalWrite(greenLed, HIGH); 

   digitalWrite(blueLed, LOW); // Turn BLUE LED ON 

  } 



  else if (green < red && green < blue) 

  { 

   Serial.println(" - (Green Color)"); 

   digitalWrite(redLed, HIGH); 

   digitalWrite(greenLed, LOW); // Turn GREEN LED ON

   digitalWrite(blueLed, HIGH); 

  } 

  else{

  Serial.println(); 

  }

  delay(300);  

  digitalWrite(redLed, HIGH); 

  digitalWrite(greenLed, HIGH); 

  digitalWrite(blueLed, HIGH); 

 } 


/* Read and Store values in red, green and blue variables */   

void color() 

{  

  //s2 and s3 to LOW for reading RED Values

  digitalWrite(s2, LOW); 

  digitalWrite(s3, LOW); 

  red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);



  //s3 to HIGH (Already s2 in LOW) for BLUE values 

  digitalWrite(s3, HIGH); 

  blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH);



  //s2 to HIGH (Already s3 HIGH) for GREEN

  digitalWrite(s2, HIGH); 

  green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 

}

Make sure the correct Board and COM Port is selected then upload the code. Open serial monitor, now place any Red/Green/Blue colored object in front of Color sensor. It will show the color value in decimal and also the color of the object. Similarly, you can notice the RGB LED will light according to the Object color.

Share this post

Comments (6)

  • Uriel

    Que tal amigo buen día disculpa me puedes dar un ejemplo de cómo detectar cualquier color soy nuevo en eso y quiero Acer una clasificadora de frijol por color

    May 5, 2019 at 11:19 PM
    • Sharath

      Even I tried to detect any color and map on the RGB LED. But it won’t works as expected. But if you want more color you can read the RGB Values and put the else if conditions. It works perfectly with Display in Serial Monitor or LCDs but not in RGB LED as it needs a perfect voltage (it needs more calibration).

      May 6, 2019 at 3:07 PM
  • Uriel

    Tengo los valores de cada color que e detectado en el monitor pero en el código no sé cómo incluirlos
    // Variables

    int red = 0;

    int green = 0;

    int blue = 0;

    Aki es donde pondré los valores que me arroja el monitor serie

    May 7, 2019 at 8:59 AM
    • Sharath

      In these else if, you need to exacly mention the range of RGB Values:

      else if (blue < red && blue < green) { Serial.println(" - (Blue Color)"); digitalWrite(redLed, HIGH); digitalWrite(greenLed, HIGH); digitalWrite(blueLed, LOW); // Turn BLUE LED ON }

      Example:

      else if (200>red && red<255 && 050 && 80>blue && blue< 100)
      red is 200 to 255

      green is 0 to 50

      blue is 80 to 100

      May 7, 2019 at 11:42 AM
  • Uriel

    Ok muchas gracias sharath por su atención y amabilidad 👍

    May 9, 2019 at 5:40 AM
  • Uriel

    Que tal amigo otra vz molestandolo ocurre que entiendo muy poco de estos códigos cómo podría prender un LED dependiendo el color en este código uno por cada color
    //

    // Cableado de TCS3200 a Arduino

    //

    #define S0 8

    #define S1 9

    #define S2 12

    #define S3 11

    #define salidaSensor 10

     

    // Para guardar las frecuencias de los fotodiodos

    int frecuenciaRojo = 0;

    int frecuenciaVerde = 0;

    int frecuenciaAzul = 0;

    int colorRojo;

    int colorVerde;

    int colorAzul;

     

    void setup() {

      // Definiendo las Salidas

      pinMode(S0, OUTPUT);

      pinMode(S1, OUTPUT);

      pinMode(S2, OUTPUT);

      pinMode(S3, OUTPUT);

      

      // Definiendo salidaSensor como entrada

      pinMode(salidaSensor, INPUT);

      

      // Definiendo la escala de frecuencia a 20%

      digitalWrite(S0,HIGH);

      digitalWrite(S1,LOW);

      

       // Iniciar la comunicacion serie

      Serial.begin(9600);

    }

     

    void loop() {

      // Definiendo la lectura de los fotodiodos con filtro rojo

      digitalWrite(S2,LOW);

      digitalWrite(S3,LOW);

      

      // Leyendo la frecuencia de salida del sensor

      frecuenciaRojo = pulseIn(salidaSensor, LOW);

     

      // Mapeando el valor de la frecuencia del ROJO (RED = R) de 0 a 255

      // Usted debe colocar los valores que registro. Este es un ejemplo:

      // colorRojo = map(frecuenciaRojo, 70, 120, 255,0);

      colorRojo = map(frecuenciaRojo, xx, xx, 255,0);

      

      // Mostrando por serie el valor para el rojo (R = Red)

      Serial.print(“R = “);

      Serial.print(colorRojo);

      delay(100);

      

      // Definiendo la lectura de los fotodiodos con filtro verde

      digitalWrite(S2,HIGH);

      digitalWrite(S3,HIGH);

      

      // Leyendo la frecuencia de salida del sensor

      frecuenciaVerde = pulseIn(salidaSensor, LOW);

     

      // Mapeando el valor de la frecuencia del VERDE (GREEN = G) de 0 a 255

      // Usted debe colocar los valores que registro. Este es un ejemplo:

      // colorVerde = map(frecuenciaVerde, 100, 199, 255,0);

      colorVerde = map(frecuenciaVerde, xx, xx, 255,0);

     

      // Mostrando por serie el valor para el verde (G = Green)

      Serial.print(“G = “);

      Serial.print(colorVerde);

      delay(100);

      // Definiendo la lectura de los fotodiodos con filtro azul

      digitalWrite(S2,LOW);

      digitalWrite(S3,HIGH);

      

      // Leyendo la frecuencia de salida del sensor

      frecuenciaAzul = pulseIn(salidaSensor, LOW);

     

      // Mapeando el valor de la frecuencia del AZUL (AZUL = B) de 0 a 255

      // Usted debe colocar los valores que registro. Este es un ejemplo:

      // colorAzul = map(frecuenciaAzul, 38, 84, 255, 0);

      colorAzul = map(frecuenciaAzul, xx, xx, 255, 0);

      

      // Mostrando por serie el valor para el azul (B = Blue)

      Serial.print(“B = “);

      Serial.print(colorAzul);

      delay(100);

     

      // Comprobar cual es el color detectado y mostrarlo

      // con un mensaje en el monitor serie

      if(colorRojo > colorVerde && colorRojo > colorAzul){

          Serial.println(” – Detectado ROJO”);

      }

      if(colorVerde > colorRojo && colorVerde > colorAzul){

        Serial.println(” – Detectado VERDE”);

      }

      if(colorAzul > colorRojo && colorAzul > colorVerde){

        Serial.println(” – Detectado AZUL”);

      }

    }

    May 11, 2019 at 8:26 PM

Leave a Reply

Back to Blog