JACK
Published

TCS3200 Color Sensor Arduino and LCD display circuit

In this article we will learn how to Interface Arduino with TCS3200 color sensor and LCD display

ExpertFull instructions provided3 hours1,607
TCS3200 Color Sensor Arduino and LCD display circuit

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
9V battery (generic)
9V battery (generic)
×1
Resistor variable 10k
×1
Standard LCD - 16x2 White on Blue
Adafruit Standard LCD - 16x2 White on Blue
×1
Grove - Light&Gesture&Color&Proximity Sensor (TMG39931)
Seeed Studio Grove - Light&Gesture&Color&Proximity Sensor (TMG39931)
×1
connecting wires
×1

Software apps and online services

Arduino IDE
Arduino IDE
Fritizin

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)
Solder Wire, Lead Free
Solder Wire, Lead Free
PCB Holder, Soldering Iron
PCB Holder, Soldering Iron

Story

Read more

Schematics

TCS3200 Color Sensor Arduino and LCD display circuit

Code

TCS3200 Color Sensor Arduino and LCD display circuit

C#
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // Replace 0x27 with your LCD address

#define outPin 8
#define s0 9
#define s1 10
#define s2 11
#define s3 12

boolean DEBUG = true;

// Variables
int red, grn, blu;
String color = "";
long startTiming = 0;
long elapsedTime = 0;

void setup() {
  Serial.begin(9600);

  Wire.begin();  // Initialize the I2C communication
  lcd.init();    // Initialize the LCD
  lcd.backlight(); // Turn on the backlight

  pinMode(s0, OUTPUT);
  pinMode(s1, OUTPUT);
  pinMode(s2, OUTPUT);
  pinMode(s3, OUTPUT);
  pinMode(outPin, INPUT); //out from sensor becomes input to Arduino

  // Setting frequency scaling to 100%
  digitalWrite(s0, HIGH);
  digitalWrite(s1, HIGH);

  lcd.clear();
  lcd.setCursor(3, 0);
  lcd.print("Welcome To");
  lcd.setCursor(1, 1);
  lcd.print("Color Detector");
  delay(2000);
  lcd.clear();
  startTiming = millis();
}

void loop() {
  getColor();

  if (DEBUG) printData();
  elapsedTime = millis() - startTiming;
  if (elapsedTime > 1000) {
    showDataLCD();
    startTiming = millis();
  }
}

/* read RGB components */
void readRGB() {
  red = 0, grn = 0, blu = 0;

  int n = 10;
  for (int i = 0; i < n; ++i) {
    // read red component
    digitalWrite(s2, LOW);
    digitalWrite(s3, LOW);
    red = red + pulseIn(outPin, LOW);

    // read green component
    digitalWrite(s2, HIGH);
    digitalWrite(s3, HIGH);
    grn = grn + pulseIn(outPin, LOW);

    // let's read blue component
    digitalWrite(s2, LOW);
    digitalWrite(s3, HIGH);
    blu = blu + pulseIn(outPin, LOW);
  }
  red = red / n;
  grn = grn / n;
  blu = blu / n;
}

/***************************************************
 * Showing captured data at Serial Monitor
 ****************************************************/
void printData(void) {
  Serial.print("red= ");
  Serial.print(red);
  Serial.print("   green= ");
  Serial.print(grn);
  Serial.print("   blue= ");
  Serial.print(blu);
  Serial.print(" - ");
  Serial.print(color);
  Serial.println(" detected!");
}

/***************************************************
 * Showing captured data on LCD
 ****************************************************/
void showDataLCD(void) {
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("R");
  lcd.print(red);
  lcd.setCursor(6, 0);
  lcd.print("G");
  lcd.print(grn);
  lcd.setCursor(12, 0);
  lcd.print("B");
  lcd.print(blu);

  lcd.setCursor(0, 1);
  lcd.print("Color: ");
  lcd.print(color);
}

void getColor() {
  readRGB();

  if (red > 41 && red < 43 && grn > 77 && grn < 81 && blu > 61 && blu < 63) color = "RED";
  else if (red > 17 && red < 78 && grn > 11 && grn < 77 && blu > 12 && blu < 72) color = "GREEN";
  else if (red > 22 && red < 70 && grn > 11 && grn < 680 && blu > 6 && blu < 42) color = "BLUE";
  else if (red > 4 && red <  24 && grn > 6 && grn < 30 && blu > 9 && blu < 32) color = "YELLOW";
  else if (red > 5 && red < 8 && grn > 4 && grn < 8 && blu > 3 && blu < 7) color = "WHITE";
  else if (red > 28 && red < 74 && grn > 25 && grn < 85 && blu > 20 && blu < 42) color = "BLACK";
  else color = "NO_COLOR";
}

Credits

JACK

JACK

21 projects • 2 followers

Comments