Color Box
Hello, my name is Gary Sidhu and welcome to this tutorial, today we will find out how to read colours using Arduino and sensors such as the one I'm using the TCS 3200. The idea is that it will be to detecting an object colour, and display it on an LCD and on an RGB led.
Supplies
- TCS 3200 Color Sensor
- RGB led - Bread Board - Arduino - 4 pin Liquid crystal LCD - box (optional)
- male wire
- female wires
- jumper wires
- construction paper for the end
Step 1: Preperation
LCD SCREEN PREPARATION
For this component first off you need to connect the LCD Screen to ground and power ( Either on the Arduino or the breadboard with jumper wires ) Then you need to connect the ports of LCD to Analog Pins A4 and A5 ( Green wires in the first picture for LCD)
COLOUR SENSOR PREPARATION
for this component, I put the S1 , S2, S3, S4 into to the analog pins by the same number and I put the ground and power in the breadboard and the sensor out into pin 8
RGB PREPARATION
put a 4 pin female pin to make and connect the male ends to the board
Step:3 the Code
here is the code, if you want to add more colours you will need to add the colour values so the colour sensor cam read it and project it to the RGB
//Arduino pins:
#define S0 2 #define S1 3 #define S2 4 #define S3 5 #define sensorOut 6 #define redLED 10 #define greenLED 11 #define blueLED 12 #include #include // TCS230 pins connected to Arduino LiquidCrystal_I2C lcd(0x27,16,2); const int s0 = 2; const int s1 = 3; const int s2 = 4; const int s3 = 5; const int out = 6;
// Variables int red = 0; int green = 0; int blue = 0; //Output from the sensor: int redFrequency = 0; int greenFrequency = 0; int blueFrequency = 0;
//Formatted color values: int redColor = 0; int greenColor = 0; int blueColor = 0;
//Values used for calibration+ int redMin; int redMax; int greenMin; int greenMax; int blueMin; int blueMax;
int color = 0;
void setup() { //Declarations: pinMode(S0, OUTPUT); pinMode(S1, OUTPUT); pinMode(S2, OUTPUT); pinMode(S3, OUTPUT); pinMode(redLED, OUTPUT); pinMode(greenLED, OUTPUT); pinMode(blueLED, OUTPUT); pinMode(13, OUTPUT); pinMode(sensorOut, INPUT); // Set frequency scaling to 20%: digitalWrite(S0, HIGH); digitalWrite(S1, LOW); Serial.begin(9600);//begin serial communication 120 calibrate();//calibrate sensor (look at serial monitor) }
void loop() { { color(); Serial.print("R ="); Serial.print(red, DEC); Serial.print(" G = "); Serial.print(green, DEC); Serial.print(" B = "); Serial.print(blue, DEC); Serial.print("\t");
if (red < blue && red < green && red < 25) { if (green - blue >= 10 && green - blue <= 25 && green - ( 2 * red ) >= 8 ) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Color Detection"); lcd.setCursor(0, 1); lcd.print("Color : "); lcd.print("Red"); Serial.println(" - (Red Color)"); }
else if (green - red <= 10 && green - red >= -3 && blue >= green) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Color Detection"); lcd.setCursor(0, 1); lcd.print("Color : "); lcd.print("Yellow"); Serial.println(" - (Yellow Color)"); } else if (blue - red >= 3 && blue - red <= 10 && green - ( 2 * red ) <= 5) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Color Detection"); lcd.setCursor(0, 1); lcd.print("Color : "); lcd.print("Pink"); Serial.println(" - (Pink Color)"); } else if (green - blue >= -5 && green - blue <= 5 && green - ( 2 * red ) <= 5 ) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Color Detection"); lcd.setCursor(0, 1); lcd.print("Color : "); lcd.print("Orange"); Serial.println(" - (Orange Color)"); readColor();//read sensor decideColor();//format color values printColor();//print values delay (1000); }
void decideColor() {//format color values //Limit possible values: redColor = constrain(redColor, 0, 255); greenColor = constrain(greenColor, 0, 255); blueColor = constrain(blueColor, 0, 255);
//find brightest color: int maxVal = max(redColor, blueColor); maxVal = max(maxVal, greenColor); //map new values redColor = map(redColor, 0, maxVal, 0, 255); greenColor = map(greenColor, 0, maxVal, 0, 255); blueColor = map(blueColor, 0, maxVal, 0, 255); redColor = constrain(redColor, 0, 255); greenColor = constrain(greenColor, 0, 255); blueColor = constrain(blueColor, 0, 255);
//light led analogWrite(redLED, 255-redColor); analogWrite(greenLED,255- greenColor); analogWrite(blueLED, 255- blueColor); //decide which color is present (you may need to change some values here): if (redColor > 250 && greenColor > 250 && blueColor > 250) { color = 1;//white } else if (redColor < 25 && greenColor < 25 && blueColor < 25) { color = 2;//black } else if (redColor > 200 && greenColor > 200 && blueColor < 100) { color = 4;//yellow } else if (redColor > 200 && greenColor > 25 /*&& blueColor < 100*/) { color = 3;//orange } else if (redColor > 200 && greenColor < 100 && blueColor > 200) { color = 5;//purple } else if (redColor > 250 && greenColor < 200 && blueColor < 200) { color = 6;//red } else if (redColor < 200 && greenColor > 250 && blueColor < 200) { color = 7;//green } else if (redColor < 200 /*&& greenColor < 200*/ && blueColor > 250) { color = 8;//blue } else { color = 0;//unknown } } void color() { digitalWrite(s2, LOW); digitalWrite(s3, LOW); //count OUT, pRed, RED red = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); digitalWrite(s3, HIGH); //count OUT, pBLUE, BLUE blue = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); digitalWrite(s2, HIGH); //count OUT, pGreen, GREEN green = pulseIn(out, digitalRead(out) == HIGH ? LOW : HIGH); } void calibrate() { Serial.println("Calibrating..."); Serial.println("White");//aim sensor at something white //set calibration vaues:
digitalWrite(13, HIGH); delay(2000); digitalWrite(S2, LOW); digitalWrite(S3, LOW); redMin = pulseIn(sensorOut, LOW); delay(100); digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); greenMin = pulseIn(sensorOut, LOW); delay(100); digitalWrite(S2, LOW); digitalWrite(S3, HIGH); blueMin = pulseIn(sensorOut, LOW); delay(100); Serial.println("next...");//aim sensor at something black digitalWrite(13, LOW); delay(2000); Serial.println("Black");
//set calibration values: digitalWrite(13, HIGH); delay(2000); digitalWrite(S2, LOW); digitalWrite(S3, LOW); redMax = pulseIn(sensorOut, LOW); delay(100); digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); greenMax = pulseIn(sensorOut, LOW); delay(100); digitalWrite(S2, LOW); digitalWrite(S3, HIGH); blueMax = pulseIn(sensorOut, LOW); delay(100); Serial.println("Done calibrating."); digitalWrite(13, LOW); }
void printColor() {//print data Serial.print("R = "); Serial.print(redColor); Serial.print(" G = "); Serial.print(greenColor); Serial.print(" B = "); Serial.print(blueColor); Serial.print(" Color: "); switch (color) { case 1: Serial.println("WHITE"); break; case 2: Serial.println("BLACK"); break; case 3: Serial.println("ORANGE"); break; case 4: Serial.println("YELLOW"); break; case 5: Serial.println("PURPLE"); break; case 6: Serial.println("RED"); break; case 7: Serial.println("GREEN"); break; case 8: Serial.println("BLUE"); break; default: Serial.println("unknown"); break; } }
void readColor() {//get data from sensor //red: digitalWrite(S2, LOW); digitalWrite(S3, LOW); redFrequency = pulseIn(sensorOut, LOW); redColor = map(redFrequency, redMin, redMax, 255, 0); delay(100);
//green: digitalWrite(S2, HIGH); digitalWrite(S3, HIGH); greenFrequency = pulseIn(sensorOut, LOW); greenColor = map(greenFrequency, greenMin, greenMax, 255, 0); delay(100);
//blue: digitalWrite(S2, LOW); digitalWrite(S3, HIGH); blueFrequency = pulseIn(sensorOut, LOW); blueColor = map(blueFrequency, blueMin, blueMax, 255, 0); delay(100); }
The Start
when making this code keep in mind that you need to include these libraries because without them your code will not work
Wire.h
LiquidCrystal_I2c.h
to change the pins go to the define and change the number to any pin you want
Step:3 Icing on the Top!
this is optional if you want this project to look good grab a shoebox and follow these steps
1. tape up the lid
2.cut one side open
3.cut the top the same size as the LCD
4.cut a cube for the sensor
5. cut an X for the led
NOW ENJOY YOUR VERY OWN COLOR DETECTOR