Arduino(5): Color Sensor TCS3200 (GY-31)

I saw a conveyor belt at the Auto Show, and there was a robot arm next to it. When the small squares passed through, they were clipped by the robot arm and placed in the same color. I think there should be a sensor that detects color. The TCS3200 color sensor is used to determine the three primary colors of red, green and blue (RGB). Let's take a look at how to use filters. The color of the object is measured.

[TCS3200 Detection Principle]

The TCS3200 color sensor is a complete color detector that includes a TAOS TCS3200 RGB sensor chip and four white LEDs. The TCS3200 can detect and measure virtually unlimited range of visible colors. Applications include test strip reading, color classification, ambient light sensing and calibration, and color matching. 
According to the three primary color theory, various colors are formed by mixing three different primary colors (red, green, and blue). The TCS3200 uses this principle to identify the color of an object. Through an array of photodetectors, each has a red, green or blue filter, or no filter (transparent). 
When sensing the color of an object, it is necessary to take three colors through the filter to detect, analyze the light projected onto the TCS3200 sensor to determine the color, and then obtain three values ​​to know which color. Take S2 ​​and S3 as low and high potentials. For example, only blue light is allowed to pass. At this time, the value of blue light is received, and so on. Finally, the values ​​of the three colors are collected, and the color of the object is composed. .

[TCS3200 Features]

  • Wafer: TCS3200
  • Power supply: 2.7V to 5.5V
  • With LED lights
  • Anti-light interference, white LED, can control bright or off, can detect the color of non-illuminating objects
  • Best detection distance: 1 cm

Pin description

Pin name
description
GND )
Power ground
OE )
Input, enable output frequency (low potential)
OUT )
Output frequency
S0 S1 1,2 )
Input, select output frequency scaling
S2 S3 7,8 )
Select input photodiode type
VDD )
Voltage supply
When the combination of HIGH and LOW of S0 and S1 affects the output frequency. For Arduino, a 20% frequency scaling is usually used. Therefore, set the S0 pin high and the S1 pin low.

S0
S1
Output frequency step
LOW
LOW
No power
LOW
HIGH
2%
HIGH
LOW
20%
HIGH
HIGH
100%

The color to be read by the sensor can be achieved by controlling the pins S2 and S3. The table below shows the color table filtered when S2 and S3 are set to high and low potentials.

S2
S3
Filter color
LOW
LOW
Red  
LOW
HIGH
Blue 
HIGH
LOW
Do not filter  ( clear )
HIGH
HIGH
Green 

[material]

• Arduino Leonardo x 1 
• GY-31 TCS3200 color sensor module 
• Cable x 7

[wiring]

Arduino
GY-31 TCS3200
5V (VCC)
VCC
GND
GND
Pin 4
S0
Pin 5
S1
Pin 6
S2
Pin 7
S3
Pin 8
OUT





[Code]

Because the sensitivity of each sensor is different, the first program first reads the three color values ​​of red, blue and green to find the maximum and minimum sensing range, and then the second program determines which range the detected value falls to determine the object. s color.

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

void setup() {
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

  pinMode(sensorOut, INPUT);

  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);

  Serial.begin(9600);
}
void loop() {
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);

  redFrequency = pulseIn(sensorOut, LOW);

  Serial.print("R = ");
  Serial.print(redFrequency);
  delay(100);

  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);

  greenFrequency = pulseIn(sensorOut, LOW);

  Serial.print(" G = ");
  Serial.print(greenFrequency);
  delay(100);

  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);

  blueFrequency = pulseIn(sensorOut, LOW);

  Serial.print(" B = ");
  Serial.println(blueFrequency);
  delay(100);
}

The following is the program two, directly listed object color:

#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8

int redFrequency = 0;
int greenFrequency = 0;
int blueFrequency = 0;

int redColor = 0;
int greenColor = 0;
int blueColor = 0;

void setup() {

  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
  pinMode(S3, OUTPUT);

  pinMode(sensorOut, INPUT);
  
  digitalWrite(S0,HIGH);
  digitalWrite(S1,LOW);
  
  Serial.begin(9600);
}

void loop() {
  digitalWrite(S2,LOW);
  digitalWrite(S3,LOW);
  
  redFrequency = pulseIn(sensorOut, LOW);
  
  // redColor = map(redFrequency, min, max, 255,0);
  redColor = map(redFrequency, 332, 1280, 255,0);
  
  Serial.print("R = ");
  Serial.print(redColor);
  delay(100);
  
  digitalWrite(S2,HIGH);
  digitalWrite(S3,HIGH);
  
  greenFrequency = pulseIn(sensorOut, LOW);

  // greenColor = map(greenFrequency, min, max, 255, 0);
  greenColor = map(greenFrequency, 850, 1498, 255, 0);
  
  Serial.print(" G = ");
  Serial.print(greenColor);
  delay(100);
 
  digitalWrite(S2,LOW);
  digitalWrite(S3,HIGH);

  blueFrequency = pulseIn(sensorOut, LOW);

  blueColor = map(blueFrequency, 178, 459, 255, 0);
  
  Serial.print(" B = ");
  Serial.print(blueColor);
  delay(100);

 
  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!");
  }
}

[Results]

[Reference]

• Randomnerdtutorials:Guide for TCS230/TCS3200 Color Sensor with Arduino
• How to mechatronics:Arduino Color Sensing Tutorial – TCS230 TCS3200 Color Sensor

Post a Comment

Previous Post Next Post