d How to Build a Color Sensor Circuit 


               


How to Build a Color Sensor Circuit


TCS3200 Color Sensor



In this project, we are going to build a color sensor circuit with an Arduino microcontroller.

A color sensor is a device that can detect and differentiate between certain primary colors.

The sensor we will use in this circuit is a TCS3200 color sensor.

This sensor can detect and differentiate between the colors white, blue, green, and red.

A color sensor is actually a very useful device. Now in the age where autonomous vehicles are being developed that can run on their own, driverless cars, there has to be a way to be able to detect colors at places such as red stop signs and of course traffic lights, green to go and red to stop.

Another device that a color sensor can be integrated to is robotics. The more capabilities a robot has, the smarter it is. If a robot can differentiate colors, it has much greater capability. Imagine if you have a robot that can tell the difference between a green towel and a red towel. If you know you have a green towel and another person has a red towel, the robot can sort it appropriately.

Other applications include strip reading, ambient light sensing and calibration, and color matching.

So there's no shortage of devices that a color sensor can be used in. Knowing how to use one is very useful, especially in this robotic, autonomous day in age.

In this circuit, we will make a device that can detect between white, bliue, green, and red.

Components

  • TCS3200 Color Sensor
  • Arduino


The TCS3200 functions as a color sensor. Its more specific title is a color light-to-frequency converter.

What this means is the sensor takes light and converts it into certain amount of current. This current is then fed into a frequency converter. The amount of current that is produced, which corresponds with the color of the light, produces a certain frequency. This output frequency then determines the color that was sensed. So basically the light is converted into a frequency. Each color has its own unique frequency. So this is how this sensor can differentiate between colors. Red has its unique frequency. Blue has its own. And so does every other color.

The sensor has 10 pins.

The pins of this chip are summarized in the table below.

Since 2 of the pins are redundant, VCC and GND, the table shows a description of 8 pins.



TCS3200 Color Sensor
Pins Function
VCC This is where you feed the positive voltage to power the chip.
GND This is where you connect ground to in order to complete the electrical pathway.
S0 With S1, these determine the scaling of the output frequency.
S1 With S0, these determine the scaling of the output frequency.
S2 With S3, these pins determine the color sensed by the sensor.
S3 With S2, these pins determine the color sensed by the sensor.
Output The Output pin gives the output frequency (of the color sensed)
LED If connected to 5V or to a digital pin, the LEDs on the chip board will light up.


So now you know the functions that each pin has. But to go a little more in depth, we give a few tables below on how exactly pins S0-S3 work.

Probably, the most important of the 2 pins are S2 and S3. These determine the color that has been sensed. Since these are 2 pins (S2 and S3) and each one can be HIGH or LOW, there is a total of 4 combinations possible.

The table below shows the combinations and the output resultant from these combinations.
S2 S3 Photodiode Type
L L Red
L H Blue
H L Clear (no filter)
H H Green


How the sensor works that it has color filters. When you choose a color filter, it can only allow one particular color to get through and prevents the others from getting through. Selecting the different variety of combinations for S2 and S3 picks certain filters. So if you select S1 as LOW and S2 as LOW, for instance, this makes a red color filter, where only red color can go through. So if the color goes through, then the sensor knows that red color has in fact been detected. So if no color goes through and these filters are applied, then that means that the color is not red. And this is how it works for all the other colors.

S0 and S1 are much less important for our purposes. But they can be useful for more advanced uses.

This is summarized in the table below.

S0 S1 Output frequency Scaling (f0)
L L Power down
L H 2%
H L 20%
H H 100%


Powering down the sensor is a power saving method. It holds the output at a high-impedance state. It's similar to the output enable pin. But powering down the sensor with this pin saves much more power than disabling the sensor with the output enable pin. So it's much power efficient. To do this, you would just make S0 and S1 both LOW.

The other combinations change the scaling of the output frequency.Scaling is accomplished by connecting the output signal of the converter to a series of frequency dividers. All divided outputs have a 50% duty cycles but we can change the frequency of the signals by either 100%, 20%, and 2%.

The frequency-scaling function is useful for optimizing the frequency output obtained from the TCS3200 sensor with other devices. The scaled-down outputs may be used where only a slower frequency counter is available, an example being a low-cost microcontroller, or where period measurement techniques are used. Again, this is for more advanced applications.


Color Sensor Circuit

The color sensor circuit we will build is shown below.



TCS3200 Color Sensor Circuit

This color sensor circuit has quite a few connections.

First we connect power. VCC connects to 5V on the arduino board and GND connects to GND on the arduino. This establishes power to the TCS3200 chip.

S0 and S1 go to pins 8 and 9, respectively. These can either put the chip in power-saving mode or adjust the output frequency scaling.

S2 and S3 go into pins 12 and 11, respectively. These pins determine which color the sensor senses.

The Output pin goes into pin 10. This pin contains the output frequency of the color sensed.

The LED pin goes into pin 13. This pin can turn on the LEDs on the TCS3200 chip board if the digital pin is turned HIGH.

Next all we need is the code.


Code

This is the code necessary for the TCS3200 chip to detect between the colors green, blue, white, and red.

//Connections to the Arduino Microcontroller
int S0 = 8;
int S1 = 9;
int S2 = 12;
int S3 = 11;
int taosOutPin = 10;
int LED = 13;

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

void loop() {
detectColor(taosOutPin);
Serial.print("\n\n\n");
delay(1000);
}

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

Serial.print("white ");
Serial.println(white);

Serial.print("red ");
Serial.println(red);

Serial.print("blue ");
Serial.println(blue);

Serial.print("green ");
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 if LEDstate is 0, LED will be off. 1 and the LED will be on. taosOutPin is the ouput pin of the TCS3200. */
float colorRead(int taosOutPin, int color, boolean LEDstate){

//turn on sensor and use highest frequency/sensitivity setting

taosMode(1);

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

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

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

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

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

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

// 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, 80000);

//if the pulseIn times out, it returns 0 and that throws off numbers. just cap it at 80k if it happens
if(readPulse < .1){
readPulse = 80000;
}

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

// return the pulse value back to whatever called for it...
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
void taosMode(int mode){

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

//this will put in 1:1, highest sensitivity
}else if(mode == 1){
digitalWrite(S0, HIGH);
digitalWrite(S1, HIGH);
// Serial.println("Frequency Scaled at 100%");

//this will scale down the frequency down 20%
}else if(mode == 2){
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
//Serial.println("Frequency Scaled Down to 20%");

//this will scale down the frequency down to 2%
}else if(mode == 3){
digitalWrite(S0, LOW);
digitalWrite(S1, HIGH);
//Serial.println("Frequency Scaled Down to 2%");
}
return;
}

void TCS3200setup(){

pinMode(LED,OUTPUT);

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

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

//communication freq (sensitivity) selection
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
return;
}


This code will basically output to the Serial Monitor what color has been detected.

Once you know the color, you literally can make anything happen.

If the color is green, the circuit can do one thing. If the color is red, the circuit can do another thing. If the color is blue, the circuit can yet to do something else. And the same for clear. All of this can be configured using if statements.

But this circuit, once again, demonstrates how a color sensor physically is connected and how it works connected to a microcontroller with corresponding code.


Related Resources

How to Build an RGB Full Color LED Module Circuit

HTML Comment Box is loading comments...