Also read!
Home / Tools / Arduino / Color sensor with TCS3200D lens for Arduino

Color sensor with TCS3200D lens for Arduino

This small sensor Lens TCS3200D is able to perceive colors and send them to the Arduino. Although not very reliable, It seemed very cool. It also has a version without lens.

570feb7116d85_IMG_20160414_160510

Has 4 LEDs mounted on board.

570feb730cecc_IMG_20160414_160516

The connectors are: S0, S1, S2, S3, GND, VCC, OUT and LED.

570feb751d7d3_IMG_20160414_160524

There's a cover to protect the lens.

570feb771e264_IMG_20160414_160536

See how was the link with the Arduino.

570feb653fa95_IMG_20160414_160107

I used the 3.3 V voltage of the Arduino to connect the leds. It was very good brightness.

570feb66707a2_IMG_20160414_160139

In this picture I am trying to read the blue of my stapler.

570feb67a23fd_IMG_20160414_160153

The results for blue are not very good, but I can see that the Blue level is slightly larger.

570feb6879495_IMG_20160414_160203

Speaking of which, I will leave the program here for those who want to use:

int s2Pin = 7;
int s3Pin = 8;
int outPin = 4;

void setup() {
  pinMode(s2Pin, OUTPUT);
  pinMode(s3Pin, OUTPUT);
  pinMode(outPin, INPUT);
  Begin serial.(9600);
}

int readOutputPulse(int pin) {
  int count = 0;
  While ((digitalRead(PIN) == 1) & (count < 32640)) {
    count   ;
  }
  If (count == 32640) return count   2;
  count = 0;  While ((digitalRead(PIN) == 0) & (count < 32640)) {
    count   ;
  }
  return count;
}

int readRed() {
  int pulseWidth = 0;
  int colorStrength = 0;
  digitalWrite(s2Pin, LOW); 
  digitalWrite(s3Pin, LOW);
  delay(100);
  pulsewidth = readOutputPulse(outPin);
  colorStrength = (pulsewidth/128);
  colorStrength = (255 - colorStrength);
  return colorStrength;
}

int readGreen() {
  int pulseWidth = 0;
  int colorStrength = 0;
  digitalWrite(s2Pin, HIGH); 
  digitalWrite(s3Pin, HIGH);
  delay(100);
  pulsewidth = readOutputPulse(outPin);
  colorStrength = (pulsewidth/128);
  colorStrength = (255 - colorStrength);
  return colorStrength;
}

int readBlue() {
  int pulseWidth = 0;
  int colorStrength = 0;
  digitalWrite(s2Pin, LOW); 
  digitalWrite(s3Pin, HIGH);
  delay(100);
  pulsewidth = readOutputPulse(outPin);
  colorStrength = (pulsewidth/128);
  colorStrength = (256 - colorStrength);
  return colorStrength;
}

void loop() {
  Serial .print("R:");
  Out.println serial(readRed());
  Serial .print("G:");
  Out.println serial(readGreen());
  Serial .print("(B):");
  Out.println serial(readBlue());
  Out.println serial("");
  delay(500);
}

Now I'm reading a white screen on the computer.

570feb6f21b80_IMG_20160414_160356

A red screen. Check that the sensor is more sensitive to that color than any other.

570feb6a1b811_IMG_20160414_160226

A green screen:

570feb6d3fb12_IMG_20160414_160318

And finally blue.

570feb6bbce1c_IMG_20160414_160245

Although more sensitive to red than blue or green, the sensor works. Perhaps some calibration is necessary to work better. I leave below link for versions with lens (EBay) and without lens (DX):

With lenses – http://www.ebay.com/sch/i.html?_nkw=tcs3200d+lens&_sacat=0

Without lens – http://www.dx.com/p/gy-31-tcs230-tcs3200-color-sensor-recognition-module-blue-black-216,448

About Adriano

Professor, Engineer and buyer of products crazy China.

One comment

  1. Hello Adriano,

    Thanks for you helpful article.
    About the code. It seems to be missing some characters?

    While ((digitalRead(PIN) == 1) & (count < 32640)) {
    count ;

    Should this be "count++;"?

    And what about this piece: If (count == 32640) return count 2;
    What's between "count" and "2"?

Leave a Reply

Your email address will not be published. Required fields are marked *

*