Guide for TCS230/TCS3200 Color Sensor with Arduino

This post shows you how to detect colors with the Arduino using the TCS230/TCS3200 color sensor.

The TCS3200 color sensor can detect a wide variety of colors based on their wavelength. This sensor is specially useful for color recognition projects such as color matching, color sorting, test strip reading and much more.

Introduction

The TCS3200 color sensor – shown in the figure below –  uses a TAOS TCS3200 RGB sensor chip to detect color. It also contains four white LEDs that light up the object in front of it.


Specifications

Here’s the sensor specifications:

  • Power: 2.7V to 5.5V
  • Size: 28.4 x 28.4mm (1.12 x 1.12″)
  • Interface: digital TTL
  • High-resolution conversion of light intensity to frequency
  • Programmable color and full-scale output frequency
  • Communicates directly to microcontroller

Where to buy?

You can check the TCS3200 or a TCS230 color sensor on Maker Advisor and find the best price.

How does the TCS3200 sensor work?

The TCS3200 has an array of photodiodes with 4 different filters. A photodiode is simply a semiconductor device that converts light into current. The sensor has:

  • 16 photodiodes with red filter – sensitive to red wavelength
  • 16 photodiodes with green filter – sensitive to green wavelength
  • 16 photodiodes with blue filter – sensitive to blue wavelength
  • 16 photodiodes without filter

If you take a closer look at the TCS3200 chip you can see the different filters.

By selectively choosing the photodiode filter’s readings, you’re able to detect the intensity of the different colors. The sensor has a current-to-frequency converter that converts the photodiodes’ readings into a square wave with a frequency that is proportional to the light intensity of the chosen color. This frequency is then, read by the Arduino – this is shown in the figure below.

Pinout

Here’s the sensor pinout:

Pin Name I/O Description
GND (4) Power supply ground
OE (3) I Enable for output frequency (active low)
OUT (6) O Output frequency 
S0, S1(1,2) I Output frequency scaling selection inputs
S2, S3(7,8) I Photodiode type selection inputs
VDD(5) Voltage supply

Filter selection

To select the color read by the photodiode, you use the control pins S2 and S3. As the photodiodes are connected in parallel, setting the S2 and S3 LOW and HIGH in different combinations allows you to select different photodidodes. Take a look at the table below:

Photodiode type S2 S3
Red LOW LOW
Blue LOW HIGH
No filter (clear) HIGH LOW
Green HIGH HIGH

Frequency scaling

Pins S0 and S1 are used for scaling the output frequency. It can be scaled to the following preset values: 100%, 20% or 2%. Scaling the output frequency is useful to optimize the sensor readings for various frequency counters or microcontrollers. Take a look at the table below:

Output frequency scaling S0 S1
Power down L L
2% L H
20% H L
100% H H

For the Arduino, it is common to use a frequency scaling of 20%. So, you set the S0 pin to HIGH and the S1 pin to LOW.

Color Sensing with Arduino and TCSP3200

In this example you’re going to detect colors with the Arduino and the TCSP3200 color sensor.  This sensor is not very accurate, but works fine for detecting colors in simple projects.

Parts required

Here’s the parts required for this project:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic

Wiring the TCSP3200 sensor to your Arduino is pretty straightforward. Simply follow the next schematic diagram.

Here’s the connections between the TCSP3200 and the Arduino:

  • S0: digital pin 4
  • S1: digital pin 5
  • VCC: 5V
  • S3: digital pin 6
  • S4: digital pin 7
  • OUT: digital pin 8

Code

You need two sketches for this project:

  1. Reading and displaying the output frequency on the serial monitor. In this part you need to write down the frequency values when you place different colors in front of the sensor.
  2. Distinguish between different colors. In this section you’ll insert the frequency values picked previously on your code, so that your sensor can distinguish between different colors. We’ll detect red, green and blue colors.

1. Reading the output frequency

Upload the following code to your Arduino board.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

void setup() {
  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);
  
  // Setting frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
   // Begins serial communication 
  Serial.begin(9600);
}
void loop() {
  // Setting RED (R) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  // Reading the output frequency
  redFrequency = pulseIn(sensorOut, LOW);
  
   // Printing the RED (R) value
  Serial.print("R = ");
  Serial.print(redFrequency);
  delay(100);
  
  // Setting GREEN (G) filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  greenFrequency = pulseIn(sensorOut, LOW);
  
  // Printing the GREEN (G) value  
  Serial.print(" G = ");
  Serial.print(greenFrequency);
  delay(100);
 
  // Setting BLUE (B) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  blueFrequency = pulseIn(sensorOut, LOW);
  
  // Printing the BLUE (B) value 
  Serial.print(" B = ");
  Serial.println(blueFrequency);
  delay(100);
}

View raw code

Open the serial monitor at a baud rate of 9600.

Place a blue object in front of the sensor at different distances. You should save two measurements: when the object is placed far from the sensor and when the object is close to it.

Check the values displayed on the serial monitor. The blue frequency (B) should be the lowest compared to the red (R) and green (G) frequency readings – see figure below.

When we place the blue object in front of the sensor, the blue frequency (B) values oscillate between 59 and 223 (see highlighted values).

Note: you can’t use these frequency values (59 and 223) in your code, you should measure the colors for your specific object with your own color sensor. Then, save your upper and bottom frequency limits for the blue color, because you’ll need them later.

Repeat this process with a green and red objects and write down the upper and bottom frequency limits for each color.

2. Distinguish between different colors

This next sketch maps the frequency values to RGB values (that are between 0 and 255).

In the previous step when we have maximum blue we obtained a frequency of 59 and when we have blue at a higher distance we obtained 223.

So, 59 in frequency corresponds to 255 (in RGB) and 223 in frequency to 0 (in RGB). We’ll do this with the Arduino map() function. In the map() function you need to replace XX parameters with your own values.

/*********
  Rui Santos
  Complete project details at https://randomnerdtutorials.com  
*********/

// TCS230 or TCS3200 pins wiring to Arduino
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

// Stores frequency read by the photodiodes
int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

// Stores the red. green and blue colors
int redColor = 0;
int greenColor = 0;
int blueColor = 0;

void setup() {
  // Setting the outputs
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);
  
  // Setting the sensorOut as an input
  pinMode(sensorOut, INPUT);
  
  // Setting frequency scaling to 20%
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  // Begins serial communication
  Serial.begin(9600);
}

void loop() {
  // Setting RED (R) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  // Reading the output frequency
  redFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the RED (R) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // redColor = map(redFrequency, 70, 120, 255,0);
  redColor = map(redFrequency, XX, XX, 255,0);
  
  // Printing the RED (R) value
  Serial.print("R = ");
  Serial.print(redColor);
  delay(100);
  
  // Setting GREEN (G) filtered photodiodes to be read
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  greenFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the GREEN (G) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // greenColor = map(greenFrequency, 100, 199, 255, 0);
  greenColor = map(greenFrequency, XX, XX, 255, 0);
  
  // Printing the GREEN (G) value  
  Serial.print(" G = ");
  Serial.print(greenColor);
  delay(100);
 
  // Setting BLUE (B) filtered photodiodes to be read
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);
  
  // Reading the output frequency
  blueFrequency = pulseIn(sensorOut, LOW);
  // Remaping the value of the BLUE (B) frequency from 0 to 255
  // You must replace with your own values. Here's an example: 
  // blueColor = map(blueFrequency, 38, 84, 255, 0);
  blueColor = map(blueFrequency, XX, XX, 255, 0);
  
  // Printing the BLUE (B) value 
  Serial.print(" B = ");
  Serial.print(blueColor);
  delay(100);

  // Checks the current detected color and prints
  // a message in the serial monitor
  if(redColor > greenColor && redColor > blueColor){
      Serial.println(" - RED detected!");
  }
  if(greenColor > redColor && greenColor > blueColor){
    Serial.println(" - GREEN detected!");
  }
  if(blueColor > redColor && blueColor > greenColor){
    Serial.println(" - BLUE detected!");
  }
}

View raw code

To distinguish between different colors we have three conditions:

  • When the R is the maximum value (in RGB parameters) we know we have a red object
  • When G is the maximum value, we know we have a green object
  • When B is the maximum value, we know we have a blue object

Now, place something in front of the sensor. It should print in your serial monitor the color detected: red, green or blue.

Tip: your sensor can also detect other colors with more if statements.

Wrapping up

In this post you’ve learned how to detect colors with the TCSP3200 color sensor.

You can easily build a color sorting machine by simply adding a servo motor.

Do you have project ideas related to color sorting?

Let us know by posting a comment below.

If you like this post, you’ll probably like these posts:

Thanks for reading,

Rui and Sara



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

62 thoughts on “Guide for TCS230/TCS3200 Color Sensor with Arduino”

    • Hi Damian.
      You need to add a condition that checks colors, and does something when that condition is met.
      For example, if you to trigger the relay when the sensor detects blue, you need to check what are the R, G and B parameters ranges for the blue color. Then, add a condition to control the relay on or off.
      We have a tutorial on how to use the relay module with the Arduino.
      https://randomnerdtutorials.com/guide-for-relay-module-with-arduino/
      Hope this is useful.

      Reply
  1. Hey, I’ve tried this tutorial. Doing exactly how you say it so. The problem is now that the sensor gives a high blue value when reading a green object. Do you know of any way to help me with this?

    Reply
    • Hi Duncan.
      This sensor is a little tricky to use.
      The values obtained may vary depending on the light intensity on the object and how far the sensor is from the object.
      Try reading the color values in different orientations and with different light intensities.
      Regards,
      Sara

      Reply
  2. Hi, thanks for this tutorias, has been very usefull.
    I have a question, i like if someone can helpme before buy a sensor. is possible with it detect other colors? example yellow, brown, purple, orange etc. just making a interpotalion between RGB values, or shoud i use some other sensor?

    Reply
    • Hi Rob.
      The sensor can detect any color.It outputs different results depending on the color you put in front of it.
      You then, have to assign the output values to the given color. So, in answer to your question: yes, it can detect other colors.
      However, I should warn you that the sensor is a bit tricky to use, as the results vary depending on the light intensity and the distance to the object.

      Reply
  3. Could you post the code for adding extra colours to this? For example I’m trying to detect yellow but cannot get it to work, I’m a bit of a beginner to coding but I can get the RGB sensing working, just not working with RGB and yellow.

    Reply
    • Hi.
      You need to use the first code to get values with yellow. Try several times in order to have significant data for that color.
      Then, you need to see the relationship between the R, G, and B values when you measure the yellow color, and add that logic to the second code.
      We haven’t tested checking for yellow.
      Take a look at the code of this project that checks for yellow, and see if you can sort it out: howtomechatronics.com/projects/arduino-color-sorter-project/
      I hope this helps.

      Reply
    • Hi. Yes, it should probably work.
      If you are programming it in a high level language, you may be able to adapt this code to make it work for you.

      Reply
      • Actually what’s the output of tcs3200? How can I find out the detected color from the frequency output? How to measure the frequency output? I think it is in the form of square pulses. Then how will I obtain the frequency?

        Reply
  4. Second code give this error. First code compiles well btw. Help?

    C:\Users\aa\Documents\Arduino\color3\color3.ino: In function ‘void loop()’:

    color3:51: error: ‘XX’ was not declared in this scope

    redColor = map(redFrequency, XX, XX, 255,0);

    ^

    exit status 1
    ‘XX’ was not declared in this scope

    Reply
    • Hi.
      You need to replace the XX with your own values. That’s why the code is getting error.
      I hope this helps 🙂

      Reply
  5. Thank you, I have made this experiment. May I copypaste your code and part of this webpage and write it on my USB for archiving my project?

    Reply
    • Hi.
      You can use the content of this project if it is for personal uses only.
      You cannot distribute this content online.
      You can use part of the content as long as you give credit to our blog.
      Thanks 🙂

      Reply
  6. is it possible to calculate the percentage of nitrate, potassium and phosphorous in soil by bringing a color change in those compounds using some chemicals

    Reply
    • Hi Vasco.
      The temperature sensor can read any color.
      Each color results in different output readings in the red, green, and blue frequency.
      You’ll be able to identify any color if you properly calibrate the sensor for each specific color accordingly to the red, green, and blue frequencies.
      Regards,
      Sara 🙂

      Reply
  7. Hello,
    this is great work! Before I go and buy this sensor, may I ask two questions…
    1) Can you tell how long (time) the sensor has to have/”see” the object in front of itself in order to recognize the colour resp. to return values in the expected range (that has been calibrated in the first step)?
    I would like to use it but it has to be very fast (0.2 sec or maybe even a bit less)
    2) If it should be too slow do you know / could you recommend a certain type of sensor that is able to detect very fast 8and that works with arduino)?
    Thanks a lot!

    Reply
    • Hi.
      You don’t need to wait to get the measurements.
      It gives instantaneous readings but they fluctuate a bit.
      However, it needs some calibration and you need to have it in a controlled environment, because, for example the same “yellow” will give you different results depending on the light on its surroundings.
      Regards,
      Sara 🙂

      Reply
    • Hi.
      I don’t think this sensor is appropriate for that.
      But you can try it and see what you get.
      Regards,
      Sara

      Reply
    • Hi.
      You need to replace XX with your own values.
      Please read the full tutorial to understand how to get those values.
      Regards,
      Sara

      Reply
  8. I have a monitor displaying a 2cmx2cm box. This box change color.
    I need to build a circuit that I can attach to the screen of that monitor so that the circuit can alert me when the box change color.

    Do you think these sensors on this article can do that job? I was told that since the source of the color is a computer monitor, not all sensors would work.

    (just in case you are wondering.. the detection has to be done with a external circuit. I cannot install a software the machine to alert me when the box change color)

    Thanks in advance

    Reply
    • Hi.
      In my opinion, this sensor is very tricky to get it working in a reasonable way.
      So, I would recommend another sensor.
      Regards,
      Sara

      Reply
  9. I made a project to read the colors a green red blue
    But this sensor gives different values ​​on the same color, although the external environment remains constant
    Despite my efforts, I was unable to calibrate this sensor
    Thank you

    Reply
  10. Hi! I have a project in mind which may include this sensor, but before I buy it, I’d like to ask you a question. Can a video be triggered based on the color the sensor detects?
    Thanks!!

    Reply
  11. Long ago I bought an OSEPP-COLOR-01 board. It uses the Taos TCS3200 chip.

    The board was breadboard unfriendly with 8 connection pins on the top side. I re-soldered header pins on the bottom, with a slight angle so the module could plug into a standard size breadboard on the two outermost rows. I put flat wiring on my STEMTera board so the module would lay flat over that wiring on the breadboard.

    The OSEPP had no collar around the sensor so I added a cardboard strip with a small hole cut out for the sensor. This strip acts as an insulator so I can lay a tiny metal rechargeable flashlight on it, to detect the change from charging (red) to charged (green). It fits nicely between the 4 illumination LEDs.

    In using your code the values measured by the pulseIn command were still fairly jumpy. This was solved by two things: 1. Taking 50 measurements for each color and dividing by 50 (using longs for the totals) and 2. putting a small cardboard box over the whole thing when measuring.

    Now the values returned seem very consistent, at any scaling. Values per color vary by approximately 2-4. I would send pictures of the STEMTera but do not know how to include them here.

    thank you for the code to get me going.

    Reply
    • Great.
      Thanks for sharing your feedback.
      To share a picture, you can upload it to imgur, for example and then share a link to the picture.
      Regards,
      Sara

      Reply
      • Thanks Sara for telling me about imgur. With my code modifications to Rui’s code I was able to consistently tell the flashlight charged up in about 90 minutes.

        I found the damaged flashlight out on a walk but have now resurrected it. A HOBO data logger determines that it has a very bright white LED lasting about 5 hours. An “eyeball” test showed it gave useful light, walking around my darkened house and not stepping on my new kitten.

        Sacked out kitten
        https://i.imgur.com/oH243UO.jpg

        Reply
  12. I have been looking around and so far this is the only tutorial I could find that clearly lays out the calibration and mapping process. Thanks so much!

    Reply
  13. Hello, may I ask the distance and beam angle of the color sensor? We need an answer for our research study.. thank you

    Reply
  14. hello can i ask about the colour shorting you told if i need out as different colour for different output pin then what should i do

    Reply
  15. The Ratio of colors is more suited for detecting colour.
    So It doesn’t matter what the absolute brightness of the colour is:

    Blue / (Blue + Red + Green) = 0.1

    This is probably what the ‘White’ reading is for:

    Blue / White = 0.1

    i.f ” If Blue ratio < 0.2 then object is blue”

    Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.