-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUno_ColorSensor.ino
More file actions
228 lines (180 loc) · 7.18 KB
/
Uno_ColorSensor.ino
File metadata and controls
228 lines (180 loc) · 7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/***************************************************
Observe and report an object's visible color spectrum via a TCS3200 light-to-frequency converter
Materials Schedule
Arduino Uno - 1 each
https://store.arduino.cc/usa/arduino-uno-rev3
DF Robot Gravity I/O Shield - 1 each
https://www.dfrobot.com/product-1009.html
DF Robot Color Sensor - 1 each
https://www.dfrobot.com/product-540.html
A-B Cable - 1 each
https://www.staples.com/usb+ab+cable/directory_usb+ab+cable
Wire Bundles - 7 female/female wires
https://www.adafruit.com/product/266
Assembly
1.Connect aurduino and shield
2.Connect color sensor to I/O shield digital pins 4, 5, 6, 7, 8 and VCC and GND with (7) female/female wires
(D) Digital D4 Green S0
(D) Digital D5 Black S1
(D) Digital D6 Red S2
(D) Digital D7 White S3
(D) Digital D8 Yellow OUT
(+) Voltage VCC Red VCC
(-) Ground GND Black GND
3.Connect A-B cable to arduino and laptop and upload sketch
4.Open serial monitor to view sensor readings at 9600 baud
Sensor specifications (Texas Advanced Optoelectronic Solutions TCS3200)
Max output frequency = 600 kHz (1 kHz = 0.001 MHz, 600 kHz = 0.6 MHz)
Max data acquisition rate = 1 datapoint/microsecond (1,000 microseconds = 1 millisecond)
Sensor power settings
Clear: S2 High S3 Low
Red: S2 Low S3 Low
Green: S2 High S3 High
Blue: S2 Low S3 High
RGB Model
Clear: Red 255 Green 255 Blue 255 Clear 0 Hex FFFFFF https://convertingcolors.com/rgb-color-255_255_255.html
Red: Red 255 Green 0 Blue 0 Clear 0 Hex FF0000 https://convertingcolors.com/rgb-color-255_0_0.html
Green: Red 0 Green 255 Blue 0 Clear 0 Hex 00FF00 https://convertingcolors.com/rgb-color-0_255_0.html
Blue: Red 0 Green 0 Blue 255 Clear 0 Hex 0000FF https://convertingcolors.com/rgb-color-0_0_255.html
Web safe color palette
Hexadecimal #00 #33 #66 #99 #CC #FF
Decimal 0 51 102 153 204 255
Color wavelengths (nanometers = nm)
Red: 640 nm
Green: 524 nm
Blue: 470 nm
Engineering References
Dejan Nedelkovski
https://howtomechatronics.com/tutorials/arduino/arduino-color-sensing-tutorial-tcs230-tcs3200-color-sensor/
Paul McWhorter (Lessons 13, 14a, 14b, 15, 31, and 32)
http://www.toptechboy.com/arduino-lessons/
DF Robot
https://www.dfrobot.com/wiki/index.php/TCS3200_Color_Sensor_(SKU:SEN0101)
Wiki - Color Difference
https://en.wikipedia.org/wiki/Color_difference
Wiki - RGB Model
https://en.wikipedia.org/wiki/RGB_color_model
ATS
https://www.youtube.com/channel/UCDuWq2wFqeVII1KC7grySRg
https://github.com/AnchorageBot
September 2019
**************************************************/
//=== Libraries =====================================
#include "Math.h" // https://www.arduino.cc/en/math/h
#include "string.h" // https://www.arduino.cc/en/Reference/StringLibrary
//=== Global constants and variables ==================
// Arduino language reference (functions, variables, & structure)
// https://www.arduino.cc/reference/en/
// TCS3200 light->frequency converter to i/o shield wiring assignments
#define S0 4
#define S1 5
#define S2 6
#define S3 7
#define sensorOut 8
// Sensor frequency range = 0 to 600 kHz, pulse width up to 102,400 microseconds
// byte = range of 0 to 255
// int = 16 bit = 2 byte value = range of -32,768 to 32,767
// unsigned int = 32 bit = 4 bytes = 4,294,967,295
unsigned int redFreq = 0; //
unsigned int greenFreq = 0; //
unsigned int blueFreq = 0; // for Blue photodiodes
unsigned int sensorArray[] = {redFreq, greenFreq, blueFreq};
// Mapping sets for Frequency to RGB Model
// Command Syntax - map(value, fromLow, fromHigh, toLow, toHigh)
// value = redFreq, greenFreq, or blueFreq
int fromLowFreq = 0;
int fromHighFreq = 1000; // try 1000 for bright environment, 10000 for shady environment
int toLowRGB = 0;
int toHighRGB = 255; // max RGB value
// Mapping data: RGBvalue <-> ColorDesignation
int RGBspectrumValues[7][3] = {{0, 0, 0}, {255, 0, 0}, {255, 255, 0}, {0, 255, 5}, {128, 128, 128}, {0, 5, 255}, {255, 255, 255}};
String SpectrumColorDesignation[7] = {"black", "red", "yellow", "green", "grey", "blue", "white"};
int RGBgreenValues[4][3] = {{50, 205, 50}, {0, 255, 0}, {34, 139, 34}, {0, 128, 0}};
String GreenColorDesignation[4] = {"limeGreen", "lime", "forestGreen", "green"};
int RGByellowValues[4][3] = {{250, 250, 210}, {255, 225, 224}, {255, 255, 0}, {255, 215, 0}};
String YellowColorDesignation[4] = {"goldenrodYellow", "light yellow", "yellow", "gold"};
int RGBblueValues[4][3] = {{132, 112, 255}, {0, 0, 205}, {65, 105, 255}, {0, 0, 205}};
String BlueColorDesignation[4] = {"light blue", "medium blue", "royal blue", "gold"};
// Chronometer delay in milliseconds
int delayTimer1 = 1000;
int delayTimer2 = 500;
//=== Setup code, runs once =========================
void setup()
{
// TCS3200 light->frequency converter
pinMode(S0, OUTPUT);
pinMode(S1, OUTPUT);
pinMode(S2, OUTPUT);
pinMode(S3, OUTPUT);
pinMode(sensorOut, INPUT);
// Set frequency-scaling to 20%
digitalWrite(S0, HIGH);
digitalWrite(S1, LOW);
// Open serial monitor to view sensor readings at 9600 baud
Serial.begin(9600);
}
//=== Main code, loops endlessly ====================
void loop()
{
sensorReading();
amplify();
printSensorData();
}
//=== Breakout of functions used in void loop ===========================
void sensorReading()
{
// Turn on Red filtered photodiodes
digitalWrite(S2, LOW);
digitalWrite(S3, LOW);
// Read sensor output frequency
redFreq = pulseIn(sensorOut, LOW); // width of pulse in microseconds
// Map the RGB Model to the Frequency
redFreq = map(redFreq, fromLowFreq, fromHighFreq, toLowRGB, toHighRGB);
// Turn on Green filtered photodiodes
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH);
// Read sensor output frequency
greenFreq = pulseIn(sensorOut, LOW); // width of pulse in microseconds
// Map the RGB Model to the Frequency
greenFreq = map(greenFreq, fromLowFreq, fromHighFreq, toLowRGB, toHighRGB);
// Turn on Blue filtered photodiodes
digitalWrite(S2, LOW);
digitalWrite(S3, HIGH);
// Read sensor output frequency
blueFreq = pulseIn(sensorOut, LOW); // width of pulse in microseconds
// Map the RGB Model to the Frequency
blueFreq = map(blueFreq, fromLowFreq, fromHighFreq, toLowRGB, toHighRGB);
}
void amplify()
{
if (redFreq > greenFreq && redFreq > blueFreq)
{
Serial.println("Sensor sees more Red");
delay(delayTimer1);
}
else if (greenFreq > redFreq && greenFreq > blueFreq)
{
Serial.println("Sensor sees more Green");
delay(delayTimer1);
}
else if (blueFreq > redFreq && blueFreq > greenFreq)
{
Serial.println("Sensor sees more Blue");
delay(delayTimer1);
}
}
void printSensorData()
{
// Print sensor readings
Serial.print("R = ");
Serial.print(redFreq);
Serial.print(" ");
Serial.print("G = ");
Serial.print(greenFreq);
Serial.print(" ");
Serial.print("B = ");
Serial.print(sensorArray[2]);
Serial.print(" ");
Serial.print("\n\n");
delay(delayTimer2);
}