Project 11: Arduino Color Sensor (TCS230/TCS3200)
Main Ideas:
The TCS3200 color sensor can detect a wide range of colors based on
their wavelength. This sensor is can be used for color recognition/detection
projects such as color matching, color sorting.
The sensor has four different types of filter covered diodes. In the 8 x
8 array of photodiodes, 16 photodiodes have Red filters, 16 have Blue filters,
16 have Green filters and the rest 16 photodiodes are clear with no filters.
Each type can be activated using the S2, S3 selection inputs.
/*********
https://arduino4everyones.blogspot.com/ Color ranges
2 cm till 5 cm Main Ideas: The TCS3200 color sensor can detect a wide range of
colors based on their wavelength. This sensor is can be used for color
recognition/detection projects such as color matching, color sorting. The sensor has four different types of filter
covered diodes. In the 8 x 8 array of photodiodes, 16 photodiodes have Red filters, 16 have Blue
filters, 16 have Green filters and the rest 16 photodiodes are clear with no filters. Each type can be activated
using the S2, S3 selection inputs. *********/ // TCS230 or TCS3200 pins wiring to Arduino #define S0 2
// gray #define S1 3
//maron #define S2 4
//yellow #define S3 5 //green #define sensorOut 6 //orange // Capture frequencies read by the TCS230/TCS3200
Sensor int redFrequency = 0; int greenFrequency = 0; int blueFrequency = 0; //Define the colors //red color int redColor = 0; //green color int greenColor = 0; //blue color int blueColor = 0; void setup() { // Setting
the outputs pinMode(S0,
OUTPUT); pinMode(S1,
OUTPUT); pinMode(S2,
OUTPUT); pinMode(S3,
OUTPUT); // Setting
the sensorOut as an input
pinMode(sensorOut, INPUT); // Setting
frequency scaling to 20%
digitalWrite(S0, HIGH); digitalWrite(S1,
LOW); // Begins
serial communication
Serial.begin(9600); } void loop() { // Set RED
(R) filtered photodiodes to be read
digitalWrite(S2, LOW);
digitalWrite(S3, LOW); // Reading
the output frequency redFrequency
= pulseIn(sensorOut, LOW); // Remaping
the value of the RED (R) frequency from 0 to 255 // You must
replace with your own values. Here's an example: // this
range for 2-3 cm distance //redColor =
map(redFrequency, 137, 144, 255, 0); redColor =
redFrequency; // Printing
the RED (R) value
Serial.print(" R = ");
Serial.print(redColor); delay(100); // Setting
GREEN (G) filtered photodiodes to be read
digitalWrite(S2, HIGH);
digitalWrite(S3, HIGH); // Reading
the output frequency
greenFrequency = pulseIn(sensorOut, LOW); // Remaping
the value of the GREEN (G) frequency from 0 to 255 // You must
replace with your own values. Here's an example: // Printing
the GREEN (G) value // this
range for 2-3 cm distance //greenColor
= map(greenFrequency, 206, 212, 255,
0); greenColor =
greenFrequency; // Printing
the GREEN (G) value
Serial.print(" G = ");
Serial.print(greenColor); delay(100); // Setting
BLUE (B) filtered photodiodes to be read digitalWrite(S2,
LOW);
digitalWrite(S3, HIGH); // Reading
the output frequency
blueFrequency = pulseIn(sensorOut, LOW); // Remaping
the value of the BLUE (B) frequency from 0 to 255 // You must
replace with your own values. Here's an example: // this range
for 2-5 cm distance //blueColor
= map(blueFrequency, 96, 102, 255, 0); blueColor =
blueFrequency;
// Printing the BLUE (B) value
Serial.print(" B = ");
Serial.print(blueColor);
Serial.println(""); delay(100); // Checks
the current detected color and prints // a message
in the serial monitor if (redColor
< greenColor && redColor < blueColor) {
Serial.println("****** Red color detected *****"); } if
(greenColor < redColor && greenColor < blueColor) {
Serial.println("****** Green color detected ******"); } if
(blueColor < redColor && blueColor < greenColor) {
Serial.println("****** Blue color detected ******"); } delay(400);
Serial.println(""); |
No comments:
Post a Comment