Wednesday, June 30, 2021

Project 13: Arduino Color Sensor TCS3200 & LCD

Project 13:  Arduino Color Sensor TCS3200 & LCD






/*********
  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.

*********/
#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>

LiquidCrystal_PCF8574 lcd(0x27);  // set the LCD address to 0x27 for a 16 chars and 2 line display

// 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);

  int LCDError;

  Serial.println("LCD...");

  // http://arduino4everyones.blogspot.com
  Wire.begin();
  Wire.beginTransmission(0x27); //Define LCD Address
  LCDError = Wire.endTransmission();
  Serial.print("LCD Error Found: ");
  Serial.print(LCDError);

  if (LCDError == 0) {
    Serial.println("No Error: LCD found.");

  } else {
    Serial.println("LCD is Missing ,  check connection.");
  }

  // initialize the lcd
  lcd.begin(16, 2);

  lcd.setBacklight(255);
  lcd.home();
  lcd.clear();
  lcd.print("LCD Found");
  delay(1500);
  lcd.clear();
  lcd.print("Welcome to ");
  lcd.setCursor(0, 1);
  lcd.print("Arduino4Everyones");
  delay(2000);
  lcd.clear();
  lcd.print("Color Detection");
  lcd.setCursor(0, 1);
  lcd.print("Started... ");
  delay(3000);
}

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 *****");
    lcd.clear();
    lcd.print("Red Color");
    lcd.setCursor(0, 1);
    lcd.print("Detected !");
  }
  if (greenColor < redColor && greenColor < blueColor) {
    Serial.println("****** Green color detected ******");
    lcd.clear();
    lcd.print("Green Color");
    lcd.setCursor(0, 1);
    lcd.print("Detected !");
  }
  if (blueColor < redColor && blueColor < greenColor) {
    Serial.println("****** Blue color detected ******");
    lcd.clear();
    lcd.print("Blue Color ");
    lcd.setCursor(0, 1);
    lcd.print("Detected !");
  }
  delay(400);
  Serial.println("");
}

No comments:

Post a Comment