/* 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
6int s0 8 //Module pins wiring
int s1 9
int 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