Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

Virtuabotix TCS3200 Color sensor pinout and coding guide

Color Sensor Pin-out and Getting Started Guide

Software

A live color viewer is available from Color Sensor Windows Application Please let me know if you find any bugs.

Guides

“Simplest” Possible Code (Short answer: Use the library for best results)

A full library is in progress, but the sketch below this is able to read at a 10 bit resolution for each color.

Projects

Working Arduino Sketch

This code uses high resolution mode to pull 10 bit ARGB data for the color sensed. This data can be automatically read from the Color Sensor Application. MAKE SURE TO SET YOUR BAUD RATE TO 115200

/*
* Color Sensor ARGB Reader
*
V1S0C by Joseph Dattilo (Virtuabotix LLC) 4 FEB 2013
-Corrected a problem with an un-used pin declaration
V1S0B by Joseph Dattilo (Virtuabotix LLC) 23 DEC 2012
*/

int S0 = 10; //You can simply connect this pin to ground in this example
int S1 = 9; //You can simply connect this pin to 5V in this example

int taosOutPin = 2; //Pin 2
int S2 = 7; //Pin 7
int S3 =8; //Pin 8

void setup() {
TCS3200setup();
Serial.begin(115200);
delay(100);
}

void loop() {
// primary loop takes color readings from all four channels and displays the raw values once per second.

detectColor(taosOutPin);
//Serial.print("nnn");
delay(200);

}

int detectColor(int taosOutPin){

int white = colorRead(taosOutPin,0,1);
int red = colorRead(taosOutPin,1,1);
int blue = colorRead(taosOutPin,2,1);
int green = colorRead(taosOutPin,3,1);

//Serial.print("white ");
Serial.print(white);
//Serial.print("red ");
Serial.print(",");
Serial.print(red);

//Serial.print("blue ");
Serial.print(",");
Serial.print(blue);
//Serial.print("green ");
Serial.print(",");
Serial.println(green);

}

/*
This section will return the pulseIn reading of the selected color.
It will turn on the sensor at the start taosMode(1), and it will power off the sensor at the end taosMode(0)
color codes: 0=white, 1=red, 2=blue, 3=green
taosOutPin is the ouput pin of the TCS3200.
*/

int colorRead(int taosOutPin, int color, boolean LEDstate){

//turn on sensor and use highest frequency/sensitivity setting
taosMode(3);

//setting for a delay to let the sensor sit for a moment before taking a reading.
int sensorDelay = 5;

//set the S2 and S3 pins to select the color to be sensed
if(color == 0){//white
digitalWrite(S3, LOW); //S3
digitalWrite(S2, HIGH); //S2
// Serial.print(" w");
}

else if(color == 1){//red
digitalWrite(S3, LOW); //S3
digitalWrite(S2, LOW); //S2
// Serial.print(" r");
}

else if(color == 2){//blue
digitalWrite(S3, HIGH); //S3
digitalWrite(S2, LOW); //S2
// Serial.print(" b");
}

else if(color == 3){//green
digitalWrite(S3, HIGH); //S3
digitalWrite(S2, HIGH); //S2
// Serial.print(" g");
}

// create a var where the pulse reading from sensor will go
unsigned int readPulse;

// this portion of the code is still in development, but is not needed atm
// turn LEDs on or off, as directed by the LEDstate var
//if(LEDstate == 0){
// digitalWrite(LED, LOW);
//}
//if(LEDstate == 1){
// digitalWrite(LED, HIGH);
//}

// wait a bit for LEDs to actually turn on, as directed by sensorDelay var
delay(sensorDelay);

// now take a measurement from the sensor, timing a low pulse on the sensor's "out" pin
readPulse = pulseIn(taosOutPin, LOW);

//turn off color sensor and LEDs to save power
//taosMode(0);

// return the pulse value back to whatever called for it...

if(readPulse > 102400 || readPulse == 0) readPulse=102400;
readPulse=readPulse/100;
readPulse = 1024 - readPulse;
readPulse = readPulse/4 - 1;
return readPulse;

}

// Operation modes area, controlled by hi/lo settings on S0 and S1 pins.
//setting mode to zero will put taos into low power mode. taosMode(0);

void taosMode(int mode){

if(mode == 0){
//power OFF mode- LED off and both channels "low"
//digitalWrite(LED, LOW);
digitalWrite(S0, LOW); //S0
digitalWrite(S1, LOW); //S1
// Serial.println("mOFFm");

}else if(mode == 1){
//this will put in 1:1, highest sensitivity
digitalWrite(S0, HIGH); //S0
digitalWrite(S1, HIGH); //S1
// Serial.println("m1:1m");

}else if(mode == 2){
//this will put in 1:5
digitalWrite(S0, HIGH); //S0
digitalWrite(S1, LOW); //S1
//Serial.println("m1:5m");

}else if(mode == 3){
//this will put in 1:50
digitalWrite(S0, LOW); //S0
digitalWrite(S1, HIGH); //S1
//Serial.println("m1:50m");
}

return;

}

void TCS3200setup(){

//initialize pins
//pinMode(LED,OUTPUT); //LED pinD

//color mode selection
pinMode(S2,OUTPUT); //S2 pinE
pinMode(S3,OUTPUT); //s3 pinF

//color response pin (only actual input from taos)
pinMode(taosOutPin, INPUT); //taosOutPin pinC

//communication freq (sensitivity) selection
pinMode(S0,OUTPUT); //S0 pinB
pinMode(S1,OUTPUT); //S1 pinA

return;

}

9 Responses to “Virtuabotix TCS3200 Color sensor pinout and coding guide”

  1. vijeth March 8, 2014 at 6:19 pm #

    Sir, Can this sensor be interfaced to 8051? If yes, please suggest how

    • Joseph Dattilo April 28, 2014 at 9:24 pm #

      I am not exactly sure which 8051 you are referring to, but regardless of micro-controller type it should work with this sensor, as long as it had an ADC (Analog to Digital converter) or you are able to connect an external ADC to it. The sensor works by outputting 3 Analog values (Red Green and Blue respectively) that represent the amount of each color that is detected by the sensors.

      If you can let me know the Manufacturer or send a link to the Datasheet I may have better luck finding you more details about what you would have to do with that particular controller.

  2. odeduxi March 7, 2015 at 9:14 am #

    Hi,
    I am running the code on Leonardo but it seems that the only thing I get is the below result, any idea how to fix?
    In addition, the code does not mention what do to with the E0 pin, where should it be connected to?

    This is the result I get: (I changed the display a bit so it will be more comfortable to read)

    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255
    red 254 ; green 254 ; blue 254 ; white 255

    • Joseph Dattilo March 12, 2015 at 7:40 pm #

      As noted int S0 = 10; //You can simply connect this pin to ground in this example

      Try connecting the S0 pin to ground, and let me know if the output changes.

  3. John August 20, 2015 at 5:57 pm #

    Hello Joe,

    I would like to get started using the TCS3200 Color sensor. I have installed the Arduino IDE and want to know how to install the need code for the Color sensor. Can you direct me to the process and how to do this?

    Regards,
    John

    • Joseph Dattilo September 21, 2015 at 5:21 pm #

      I believe we already talked about your project together, but let me know if you still needed help on this one. You should be able to simply run the code on this post without issue.

  4. Abby May November 5, 2016 at 4:43 pm #

    Hello Sir!

    The code you presented seems to be helpful in our school project. I’m just wondering where the value 102400 in the Readpulse portion of the code came from? Also, do I need to calibrate the sensor module before using it? If I do need to calibrate it, What specific calibration code will I use? 🙂

    Thank you Sir!

    • Joseph Dattilo February 22, 2017 at 8:39 pm #

      102400 is a number close to the actual limit of output from the sensor itself that then makes it easier to map returned values to actual colors. I chose 102400 because it was easy to map to 1024, and 256.

      As for calibration it really depends on your application, I would calibrate for the object you want to detect, so basically show your device the object, and then program in a trigger based on the output you get back over the serial port.

      You could do a more complex program that “learned” during operation and stored values in eeprom, or you could even hook up a neural network chip, but that is far beyond the scope of most school projects.

Trackbacks/Pingbacks

  1. Physical Computing Week 3 – Digital and Analog Inputs and Outputs | Bring Your Own Lasers - September 24, 2015

    […] a lesson on toptechboy.com using a similar color sensor by Virtuabotix, as well as their own coding guide. I came up with a rough plan of what I would do with this sensor: for a rather “simple” […]

Leave a Reply