0% found this document useful (0 votes)
103 views3 pages

Arduino Program Code TCS230

This Arduino program code is used to detect the color of an object using a TCS3200 color sensor. It initializes the sensor pins and LED pins as outputs. In the main loop, it calls the detectColor function, which reads the color values from the sensor, maps them to RGB values, and outputs the brightness levels to the corresponding LED pins. It uses the colorRead function to get the raw color readings from the sensor by setting the sensor mode and reading the output pulse width.

Uploaded by

Wahyu Dian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views3 pages

Arduino Program Code TCS230

This Arduino program code is used to detect the color of an object using a TCS3200 color sensor. It initializes the sensor pins and LED pins as outputs. In the main loop, it calls the detectColor function, which reads the color values from the sensor, maps them to RGB values, and outputs the brightness levels to the corresponding LED pins. It uses the colorRead function to get the raw color readings from the sensor by setting the sensor mode and reading the output pulse width.

Uploaded by

Wahyu Dian
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Arduino program code TCS230

1
2
3 // www.nyebarilmu.com
4
5 //Wiring PIN Sensor TCS3200 / TCS230
6 int s0=3;int s1=4;int s2=5;int s3=6;int out=7;
7
8 // sebagai indikator Warna Object telah dideteksi
9 const int LED=13;
10
11 void TCS3200(){
12 pinMode(LED,OUTPUT);
13 pinMode(s2,OUTPUT);pinMode(s3,OUTPUT);pinMode(s0,OUTPUT);pinMode(s1,OUTPUT);
14 return;
15 }
16
17 void setup(){
18 //Prosedur pemanggilan fungsi sensor TCS3200
19 TCS3200();
20
21 //Setting output untuk LED RGB
22 pinMode (10, OUTPUT); //Warna merah
23 pinMode (11, OUTPUT); //warna hijau
24 pinMode (12, OUTPUT); //warna biru
25
26 Serial.begin(9600);
27 delay(200);
28 }
29
30 void loop(){
31 Serial.print(detectColor(out));
32 delay(200);
33 }
34
35 unsigned int detectColor(int taosOutPin){
36 double isPresentTolerance=1;
37 //range nilai tolerance yaitu 1 - 10
38 //Fungsi tolerance yaitu penghindar noise pembacaan sensor
39 double isPresent=colorRead(taosOutPin,0,0)/colorRead(taosOutPin,0,1);
40
41 Serial.print("isPresent:");Serial.println(isPresent,2);
42
43 Serial.print("isPresentTolerance curently set to:");Serial.println(isPresentTolerance,2);
44
45 if(isPresent<isPresentTolerance){
46 Serial.println("nothing is in front of sensor");
47 return 0;
48 }
49
50 double red,blue,green;
51 double white = colorRead(taosOutPin,0,1);
52 unsigned int maxColor = white;
53 unsigned int red2, blue2, green2;
54
55 //Mode pendeteksian warna merah
56 red = white/colorRead(taosOutPin,1,1);red2=red*255/maxColor;
57
58 //Mode pendeteksian warna hijau
59 green = white/colorRead(taosOutPin,3,1);green2=green*255/maxColor;
60
61 //Mode pendeteksian warna biru
62 blue = white/colorRead(taosOutPin,2,1);blue2=blue*255/maxColor;
63
64 Serial.print("red :");Serial.println(red2);
65
66 if(red2>20){analogWrite (9, red2);} else {analogWrite (9, 0);}
67
68 Serial.print("green :");Serial.println(green2);
69 if(green2>20){analogWrite (10, green2);}else {analogWrite (10, 0);}
70
71 Serial.print("blue :");Serial.println(blue2);
72 if(blue2>20){analogWrite (11, blue2);}else {analogWrite (11, 0);}
73 }
74
75 unsigned int colorRead(unsigned int taosOutPin,unsigned int color, boolean LEDstate)
76 {
77 pinMode(taosOutPin, INPUT);
78 taosMode(1);
79 int sensorDelay=1;
80
81 if(color==0){digitalWrite(s3, LOW);digitalWrite(s2, HIGH);}
82 else if(color==1){digitalWrite(s3, LOW);digitalWrite(s2, LOW);}
83 else if(color==2){digitalWrite(s3, HIGH);digitalWrite(s2, LOW);}
84 if(color==3){digitalWrite(s3, HIGH);digitalWrite(s2, HIGH);}
85
86 unsigned int readPulse;
87 if(LEDstate==0){digitalWrite(LED, LOW);}
88 if(LEDstate==1){digitalWrite(LED, HIGH);}
89
90 delay(sensorDelay);
91
92 readPulse=pulseIn(taosOutPin, LOW, 25000)/2;
93 if( readPulse<.1){readPulse = 25000;}
94
95 taosMode(0);
96 return readPulse;
97 }
98
99 void taosMode(int mode){
100 if(mode==0){digitalWrite(LED,LOW);digitalWrite(s0,LOW);digitalWrite(s1,LOW);}
101 else if(mode==1){digitalWrite(s0,HIGH);digitalWrite(s1,HIGH);}
102 else if(mode==2){digitalWrite(s0,HIGH);digitalWrite(s1,LOW);}
103 else if(mode==3){digitalWrite(s0,LOW);digitalWrite(s1,HIGH);}
104 return;
105 }
106

You might also like