Virtuabotix TCS3200 Color sensor pinout and coding guide
Posted on January 16, 2014 by Joseph Dattilo, in guides.
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;
}
Open source
Everything for this sensor is on GitHub as
arduino-tcs3200-color-sensor:
the ARGB reader sketch reproduced above, the Color Sensor Windows
Application referenced earlier (a C# viewer that paints the live color from
the sketch’s serial output), and the laser-cut lens-plate design files. Code is
Apache-2.0; the hardware files are CC BY 4.0. There was never a formal
.h/.cpp class for this sensor — the reader sketch is the whole Arduino
side.
Restored from the josephdattilo.com archive — original publish date preserved. Lightly cleaned up for the modern site; the words are period-correct.
I'm Joseph Dattilo — engineer-founder in Lansing, Michigan, author of the FleetHarbor suite, and founder of Date Palm Media. More about me · More writing · Get in touch