Components and supplies
TCS3200/TCS230
Arduino UNO
RGB Diffused Common Cathode
Project description
Code
Color_sensor_TEST.ino
arduino
1st code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and displays it on the Serial monitor 3 * Refer to www.surtrtech.com for more details 4 */ 5 6#define s0 8 //Module pins wiring 7#define s1 9 8#define s2 10 9#define s3 11 10#define out 12 11 12int data=0; //This is where we're going to stock our values 13 14void setup() 15{ 16 pinMode(s0,OUTPUT); //pin modes 17 pinMode(s1,OUTPUT); 18 pinMode(s2,OUTPUT); 19 pinMode(s3,OUTPUT); 20 pinMode(out,INPUT); 21 22 Serial.begin(9600); //intialize the serial monitor baud rate 23 24 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 25 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 26 27} 28 29void loop() //Every 2s we select a photodiodes set and read its data 30{ 31 32 digitalWrite(s2,LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 33 digitalWrite(s3,LOW); 34 Serial.print("Red value= "); 35 GetData(); //Executing GetData function to get the value 36 37 digitalWrite(s2,LOW); 38 digitalWrite(s3,HIGH); 39 Serial.print("Blue value= "); 40 GetData(); 41 42 digitalWrite(s2,HIGH); 43 digitalWrite(s3,HIGH); 44 Serial.print("Green value= "); 45 GetData(); 46 47 Serial.println(); 48 49 delay(2000); 50} 51 52void GetData(){ 53 data=pulseIn(out,LOW); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again 54 Serial.print(data); //it's a time duration measured, which is related to frequency as the sensor gives a frequency depending on the color 55 Serial.print("\ "); //The higher the frequency the lower the duration 56 delay(20); 57} 58 59
Color_sensor_Recognition.ino
arduino
2nd code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and displays it on the Serial monitor 3 * and identify if possible the color 4 * Refer to www.surtrtech.com for more details 5 */ 6 7#define s0 8 //Module pins wiring 8#define s1 9 9#define s2 10 10#define s3 11 11#define out 12 12 13int Red=0, Blue=0, Green=0; //RGB values 14 15void setup() 16{ 17 pinMode(s0,OUTPUT); //pin modes 18 pinMode(s1,OUTPUT); 19 pinMode(s2,OUTPUT); 20 pinMode(s3,OUTPUT); 21 pinMode(out,INPUT); 22 23 Serial.begin(9600); //intialize the serial monitor baud rate 24 25 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 26 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 27 28} 29 30void loop(){ 31 32 GetColors(); //Execute the GetColors function to get the value of each RGB color 33 //Depending of the RGB values given by the sensor we can define the color and displays it on the monitor 34 35 if (Red <=15 && Green <=15 && Blue <=15) //If the values are low it's likely the white color (all the colors are present) 36 Serial.println("White"); 37 38 else if (Red<Blue && Red<=Green && Red<23) //if Red value is the lowest one and smaller thant 23 it's likely Red 39 Serial.println("Red"); 40 41 else if (Blue<Green && Blue<Red && Blue<20) //Same thing for Blue 42 Serial.println("Blue"); 43 44 else if (Green<Red && Green-Blue<= 8) //Green it was a little tricky, you can do it using the same method as above (the lowest), but here I used a reflective object 45 Serial.println("Green"); //which means the blue value is very low too, so I decided to check the difference between green and blue and see if it's acceptable 46 47 else 48 Serial.println("Unknown"); //if the color is not recognized, you can add as many as you want 49 50 51 delay(2000); //2s delay you can modify if you want 52 53 54 55} 56 57 58void GetColors() 59{ 60 digitalWrite(s2, LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 61 digitalWrite(s3, LOW); 62 Red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again, if you have trouble with this expression check the bottom of the code 63 delay(20); 64 digitalWrite(s3, HIGH); //Here we select the other color (set of photodiodes) and measure the other colors value using the same techinque 65 Blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 66 delay(20); 67 digitalWrite(s2, HIGH); 68 Green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 69 delay(20); 70} 71 72//Here's an example how to understand that expression above, 73//""digitalRead(out) == HIGH ? LOW : HIGH"" this whole expression is either HIGH or LOW as pulseIn function requires a HIGH/LOW value on the second argument 74 75//led_Status = led_Status == HIGH ? LOW : HIGH; 76// 77//if(led_Status == HIGH) 78//{ 79//led_Status =LOW; 80//} 81//else 82//{ 83//led_Status = HIGH; 84//} 85 86
Color_Sensor_RGB.ino
arduino
3rd code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and reproduce the color on the RGB LED 3 * Refer to www.surtrtech.com for more details 4 */ 5 6#define s0 8 //Module pins wiring 7#define s1 9 8#define s2 10 9#define s3 11 10#define out 12 11 12#define LED_R 3 //LED pins, don't forget "pwm" 13#define LED_G 5 14#define LED_B 6 15 16int Red=0, Blue=0, Green=0; 17 18void setup() 19{ 20 pinMode(LED_R,OUTPUT); //pin modes 21 pinMode(LED_G,OUTPUT); 22 pinMode(LED_B,OUTPUT); 23 24 pinMode(s0,OUTPUT); 25 pinMode(s1,OUTPUT); 26 pinMode(s2,OUTPUT); 27 pinMode(s3,OUTPUT); 28 pinMode(out,INPUT); 29 30 Serial.begin(9600); //intialize the serial monitor baud rate 31 32 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 33 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 34 35} 36 37void loop() 38{ 39 GetColors(); //Execute the GetColors function 40 41 analogWrite(LED_R,map(Red,15,60,255,0)); //analogWrite generates a PWM signal with 0-255 value (0 is 0V and 255 is 5V), LED_R is the pin where we are generating the signal, 15/60 are the min/max of the "Red" value, try measuring your own ones 42 //e.g: if the "Red" value given by the sensor is 15 -> it will generate a pwm signal with 255 value on the LED_R pin same for 60->0, because the lower the value given by the sensor the higher is that color 43 analogWrite(LED_G,map(Green,30,55,255,0)); 44 analogWrite(LED_B,map(Blue,13,45,255,0)); 45 46} 47 48void GetColors() 49{ 50 digitalWrite(s2, LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 51 digitalWrite(s3, LOW); 52 Red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again, if you have trouble with this expression check the bottom of the code 53 delay(20); 54 digitalWrite(s3, HIGH); //Here we select the other color (set of photodiodes) and measure the other colors value using the same techinque 55 Blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 56 delay(20); 57 digitalWrite(s2, HIGH); 58 Green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 59 delay(20); 60} 61 62//Here's an example how to understand that expression above, 63//""digitalRead(out) == HIGH ? LOW : HIGH"" this whole expression is either HIGH or LOW as pulseIn function requires a HIGH/LOW value on the second argument 64 65//led_Status = led_Status == HIGH ? LOW : HIGH; 66// 67//if(led_Status == HIGH) 68//{ 69//led_Status =LOW; 70//} 71//else 72//{ 73//led_Status = HIGH; 74//} 75 76
Color_sensor_Recognition.ino
arduino
2nd code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and displays it on the Serial monitor 3 * and identify if possible the color 4 * Refer to www.surtrtech.com for more details 5 */ 6 7#define s0 8 //Module pins wiring 8#define s1 9 9#define s2 10 10#define s3 11 11#define out 12 12 13int Red=0, Blue=0, Green=0; //RGB values 14 15void setup() 16{ 17 pinMode(s0,OUTPUT); //pin modes 18 pinMode(s1,OUTPUT); 19 pinMode(s2,OUTPUT); 20 pinMode(s3,OUTPUT); 21 pinMode(out,INPUT); 22 23 Serial.begin(9600); //intialize the serial monitor baud rate 24 25 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 26 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 27 28} 29 30void loop(){ 31 32 GetColors(); //Execute the GetColors function to get the value of each RGB color 33 //Depending of the RGB values given by the sensor we can define the color and displays it on the monitor 34 35 if (Red <=15 && Green <=15 && Blue <=15) //If the values are low it's likely the white color (all the colors are present) 36 Serial.println("White"); 37 38 else if (Red<Blue && Red<=Green && Red<23) //if Red value is the lowest one and smaller thant 23 it's likely Red 39 Serial.println("Red"); 40 41 else if (Blue<Green && Blue<Red && Blue<20) //Same thing for Blue 42 Serial.println("Blue"); 43 44 else if (Green<Red && Green-Blue<= 8) //Green it was a little tricky, you can do it using the same method as above (the lowest), but here I used a reflective object 45 Serial.println("Green"); //which means the blue value is very low too, so I decided to check the difference between green and blue and see if it's acceptable 46 47 else 48 Serial.println("Unknown"); //if the color is not recognized, you can add as many as you want 49 50 51 delay(2000); //2s delay you can modify if you want 52 53 54 55} 56 57 58void GetColors() 59{ 60 digitalWrite(s2, LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 61 digitalWrite(s3, LOW); 62 Red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again, if you have trouble with this expression check the bottom of the code 63 delay(20); 64 digitalWrite(s3, HIGH); //Here we select the other color (set of photodiodes) and measure the other colors value using the same techinque 65 Blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 66 delay(20); 67 digitalWrite(s2, HIGH); 68 Green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 69 delay(20); 70} 71 72//Here's an example how to understand that expression above, 73//""digitalRead(out) == HIGH ? LOW : HIGH"" this whole expression is either HIGH or LOW as pulseIn function requires a HIGH/LOW value on the second argument 74 75//led_Status = led_Status == HIGH ? LOW : HIGH; 76// 77//if(led_Status == HIGH) 78//{ 79//led_Status =LOW; 80//} 81//else 82//{ 83//led_Status = HIGH; 84//} 85 86
Color_sensor_TEST.ino
arduino
1st code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and displays it on the Serial monitor 3 * Refer to www.surtrtech.com for more details 4 */ 5 6#define s0 8 //Module pins wiring 7#define s1 9 8#define s2 10 9#define s3 11 10#define out 12 11 12int data=0; //This is where we're going to stock our values 13 14void setup() 15{ 16 pinMode(s0,OUTPUT); //pin modes 17 pinMode(s1,OUTPUT); 18 pinMode(s2,OUTPUT); 19 pinMode(s3,OUTPUT); 20 pinMode(out,INPUT); 21 22 Serial.begin(9600); //intialize the serial monitor baud rate 23 24 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 25 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 26 27} 28 29void loop() //Every 2s we select a photodiodes set and read its data 30{ 31 32 digitalWrite(s2,LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 33 digitalWrite(s3,LOW); 34 Serial.print("Red value= "); 35 GetData(); //Executing GetData function to get the value 36 37 digitalWrite(s2,LOW); 38 digitalWrite(s3,HIGH); 39 Serial.print("Blue value= "); 40 GetData(); 41 42 digitalWrite(s2,HIGH); 43 digitalWrite(s3,HIGH); 44 Serial.print("Green value= "); 45 GetData(); 46 47 Serial.println(); 48 49 delay(2000); 50} 51 52void GetData(){ 53 data=pulseIn(out,LOW); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again 54 Serial.print(data); //it's a time duration measured, which is related to frequency as the sensor gives a frequency depending on the color 55 Serial.print("\ "); //The higher the frequency the lower the duration 56 delay(20); 57} 58 59
Color_Sensor_RGB.ino
arduino
3rd code
1/* This code works with GY-31 TCS3200 TCS230 color sensor module 2 * It select a photodiode set and read its value (Red Set/Blue set/Green set) and reproduce the color on the RGB LED 3 * Refer to www.surtrtech.com for more details 4 */ 5 6#define s0 8 //Module pins wiring 7#define s1 9 8#define s2 10 9#define s3 11 10#define out 12 11 12#define LED_R 3 //LED pins, don't forget "pwm" 13#define LED_G 5 14#define LED_B 6 15 16int Red=0, Blue=0, Green=0; 17 18void setup() 19{ 20 pinMode(LED_R,OUTPUT); //pin modes 21 pinMode(LED_G,OUTPUT); 22 pinMode(LED_B,OUTPUT); 23 24 pinMode(s0,OUTPUT); 25 pinMode(s1,OUTPUT); 26 pinMode(s2,OUTPUT); 27 pinMode(s3,OUTPUT); 28 pinMode(out,INPUT); 29 30 Serial.begin(9600); //intialize the serial monitor baud rate 31 32 digitalWrite(s0,HIGH); //Putting S0/S1 on HIGH/HIGH levels means the output frequency scalling is at 100% (recommended) 33 digitalWrite(s1,HIGH); //LOW/LOW is off HIGH/LOW is 20% and LOW/HIGH is 2% 34 35} 36 37void loop() 38{ 39 GetColors(); //Execute the GetColors function 40 41 analogWrite(LED_R,map(Red,15,60,255,0)); //analogWrite generates a PWM signal with 0-255 value (0 is 0V and 255 is 5V), LED_R is the pin where we are generating the signal, 15/60 are the min/max of the "Red" value, try measuring your own ones 42 //e.g: if the "Red" value given by the sensor is 15 -> it will generate a pwm signal with 255 value on the LED_R pin same for 60->0, because the lower the value given by the sensor the higher is that color 43 analogWrite(LED_G,map(Green,30,55,255,0)); 44 analogWrite(LED_B,map(Blue,13,45,255,0)); 45 46} 47 48void GetColors() 49{ 50 digitalWrite(s2, LOW); //S2/S3 levels define which set of photodiodes we are using LOW/LOW is for RED LOW/HIGH is for Blue and HIGH/HIGH is for green 51 digitalWrite(s3, LOW); 52 Red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); //here we wait until "out" go LOW, we start measuring the duration and stops when "out" is HIGH again, if you have trouble with this expression check the bottom of the code 53 delay(20); 54 digitalWrite(s3, HIGH); //Here we select the other color (set of photodiodes) and measure the other colors value using the same techinque 55 Blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 56 delay(20); 57 digitalWrite(s2, HIGH); 58 Green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); 59 delay(20); 60} 61 62//Here's an example how to understand that expression above, 63//""digitalRead(out) == HIGH ? LOW : HIGH"" this whole expression is either HIGH or LOW as pulseIn function requires a HIGH/LOW value on the second argument 64 65//led_Status = led_Status == HIGH ? LOW : HIGH; 66// 67//if(led_Status == HIGH) 68//{ 69//led_Status =LOW; 70//} 71//else 72//{ 73//led_Status = HIGH; 74//} 75 76
Downloadable files
Wiring 2
Color replicate project
Wiring 2
Wiring 1
Direct test
Wiring 1
Wiring 1
Direct test
Wiring 1
Comments
Only logged in users can leave comments