Arduino Color Detection Project Using Adafruit TCS34725 RGB Color Sensor

Arduino Color Detection Using TCS34725 RGB Sensor with LCD & OLED | MakeMindz
🎨 Arduino Color Sensor Project

Arduino Color Detection System
Using TCS34725 RGB Sensor

High-precision color recognition with real-time RGB display on 16x2 LCD and SSD1306 OLED — complete with bar graph visualization, full wiring guide, code, and simulation.

⏱ 45 min build ⚡ Intermediate 🔧 Arduino Uno 🎨 TCS34725 Sensor 📺 LCD + OLED
🎨
This project builds a high-accuracy color detection system using the Adafruit TCS34725 RGB sensor with Arduino Uno. It reads 16-bit R, G, B, and ambient (Clear) values via I2C, identifies the dominant color, and displays RGB bar graphs live on an OLED — ideal for automation, robotics, and quality inspection.

🧰 Components Required

Arduino Uno
Adafruit TCS34725 Sensor
16x2 I2C LCD Display
SSD1306 OLED (128×64)
Jumper Wires
Breadboard
USB Cable (programming)
5V Power Supply / USB

📦 Required Arduino Libraries

ℹ️
Install all libraries via Arduino IDE → Tools → Manage Libraries before uploading.
LibraryPurposeInstall As
Wire.hI2C communication (built-in)Built-in — no install needed
Adafruit_TCS34725TCS34725 RGB color sensor driverSearch "Adafruit TCS34725"
LiquidCrystal_I2CI2C 16x2 LCD controlSearch "LiquidCrystal I2C"
Adafruit_GFXGraphics library (OLED dependency)Search "Adafruit GFX"
Adafruit_SSD1306SSD1306 OLED display driverSearch "Adafruit SSD1306"

🔌 Circuit Diagram

All three I2C devices (TCS34725, LCD 0x27, OLED 0x3C) share the same SDA (A4) and SCL (A5) lines on Arduino Uno.

📋 Pin Connections / Wiring Guide

⚠️
All three I2C devices (TCS34725, LCD, OLED) connect to the same A4 (SDA) and A5 (SCL) lines. Each device has a unique I2C address so they don't conflict.
ComponentComponent PinArduino PinNotes
TCS34725 SensorSDAA4I2C Data
TCS34725 SensorSCLA5I2C Clock
TCS34725 SensorVIN / VCC5VPower
TCS34725 SensorGNDGNDGround
TCS34725 SensorLED (enable)A0Set HIGH to enable onboard LED
16x2 LCD (I2C)SDAA4Shared I2C — address 0x27
16x2 LCD (I2C)SCLA5Shared I2C Clock
16x2 LCD (I2C)VCC5VPower
16x2 LCD (I2C)GNDGNDGround
SSD1306 OLEDSDAA4Shared I2C — address 0x3C
SSD1306 OLEDSCLA5Shared I2C Clock
SSD1306 OLEDVCC3.3V or 5VCheck your module spec
SSD1306 OLEDGNDGNDGround

🛠️ Step-by-Step Build Instructions

1

Install all required libraries

Before wiring anything, open Arduino IDE and install all five libraries listed above via Manage Libraries. Verify the Adafruit_TCS34725 example sketch compiles without errors.

2

Set up the I2C power and bus rails

On your breadboard, connect Arduino 5V → positive rail and GND → negative rail. All three I2C components (TCS34725, LCD, OLED) share 5V and GND from these rails.

3

Connect the TCS34725 RGB sensor

Wire SDA → Arduino A4, SCL → Arduino A5. VCC → 5V, GND → GND. Connect the LED pin to Arduino A0 — the code sets A0 HIGH in setup() to keep the onboard illumination LED active for stable readings.

4

Connect the 16x2 I2C LCD

The LCD uses an I2C backpack (usually PCF8574) at address 0x27. Connect SDA → A4, SCL → A5 (same bus as the sensor). VCC → 5V, GND → GND. Adjust the contrast potentiometer on the backpack if the display appears blank.

5

Connect the SSD1306 OLED

The OLED operates at I2C address 0x3C. Connect SDA → A4, SCL → A5 (same shared bus). VCC → 3.3V or 5V depending on your module's specifications. GND → GND.

6

Verify all I2C addresses

Upload the I2C scanner sketch from Arduino examples to confirm all three devices are detected. You should see 0x29 (TCS34725), 0x27 (LCD), and 0x3C (OLED) listed in the Serial Monitor.

7

Upload the main code

Paste the code from the section below into Arduino IDE. Select Board: Arduino Uno and the correct COM port. Click Upload. The OLED should immediately show "hello" during setup, then transition to live RGB bars.

8

Test with colored objects

Hold a red, green, or blue object 1–2 cm directly under the TCS34725 sensor. The LCD should display the detected color name and the OLED bars should shift in real time. Open the Serial Monitor at 9600 baud to see raw R, G, B, C values.

9

Calibrate the map() values if needed

The code maps raw 16-bit sensor values (0–21504) to a 0–1025 range. In very bright or dark environments, adjust the upper map value to match your real readings seen on the Serial Monitor for best accuracy.

🎨 Color Identification Logic

The system determines color by comparing mapped R, G, and B values:

🔴

RED

r > g && r > b

🟢

GREEN

g > r && g > b

🔵

BLUE

b > r && b > g

🟡

GREEN & RED

g == r && g > b

🩵

GREEN & BLUE

g == b && g > r

🟣

RED & BLUE

r == b && r > g

ALL / WHITE

g == b && r == b

OTHER

else condition

💻 Arduino Code

ℹ️
Ensure all 5 libraries are installed before uploading. The code initialises TCS34725 with 50ms integration time and 4× gain — good for indoor ambient light conditions.
Arduino C++ / .ino — color_detection_tcs34725.ino
#include <Wire.h>
#include "Adafruit_TCS34725.h"
#include <LiquidCrystal_I2C.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define WIDTH  128
#define HEIGHT 64

LiquidCrystal_I2C lcd(0x27, 20, 4);
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
Adafruit_SSD1306 display(WIDTH, HEIGHT, &Wire, 1);

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

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);  // Halt if sensor not found
  }

  lcd.init();
  lcd.clear();
  lcd.backlight();
  lcd.setCursor(0, 0);

  if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println("OLED not found");
  }

  Serial.println("OLED FOUND");
  display.clearDisplay();
  display.setTextColor(WHITE);
  display.setTextSize(1);
  display.setCursor(0, 28);
  display.println("hello");
  display.display();

  pinMode(A0, OUTPUT);
  digitalWrite(A0, HIGH);  // Enable TCS34725 onboard LED
}

void loop(void) {
  uint16_t red, green, blue, c;
  tcs.getRawData(&red, &green, &blue, &c);  // Read raw RGBC values

  // Map 16-bit sensor values to 0–1025 range
  int r = map(red,   0, 21504, 0, 1025);
  int g = map(green, 0, 21504, 0, 1025);
  int b = map(blue,  0, 21504, 0, 1025);

  Serial.print("R: "); Serial.print(r, DEC);   Serial.print(" ");
  Serial.print("G: "); Serial.print(g, DEC);   Serial.print(" ");
  Serial.print("B: "); Serial.print(b, DEC);   Serial.print(" ");
  Serial.print("C: "); Serial.print(c, DEC);   Serial.println(" ");

  // ── Color detection logic ──
  if      (r > g && r > b)           { lcd.clear(); lcd.setCursor(0,0); lcd.print("RED");          }
  else if (g > r && g > b)           { lcd.clear(); lcd.setCursor(0,0); lcd.print("GREEN");        }
  else if (b > r && b > g)           { lcd.clear(); lcd.setCursor(0,0); lcd.print("BLUE");         }
  else if (g == b && r == b)         { lcd.clear(); lcd.setCursor(0,0); lcd.print("ALL");          }
  else if (g == b && g > r)          { lcd.clear(); lcd.setCursor(0,0); lcd.print("GREEN & BLUE");  }
  else if (g == r && g > b)          { lcd.clear(); lcd.setCursor(0,0); lcd.print("GREEN & RED");   }
  else if (r == b && r > g)          { lcd.clear(); lcd.setCursor(0,0); lcd.print("RED & BLUE");    }
  else                               { lcd.clear(); lcd.setCursor(0,0); lcd.print("else");         }

  // ── OLED RGB bar graph ──
  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(SSD1306_WHITE);

  display.setCursor(0, 0);   display.print("R:");
  display.fillRect(20, 0,  r, 10, SSD1306_WHITE);  // Red bar

  display.setCursor(0, 15); display.print("G:");
  display.fillRect(20, 15, g, 10, SSD1306_WHITE);  // Green bar

  display.setCursor(0, 30); display.print("B:");
  display.fillRect(20, 30, b, 10, SSD1306_WHITE);  // Blue bar

  display.display();

  delay(500);  // Update every 500ms
}

Key Features & Applications

🎯

16-bit Precision

Raw sensor output is 16-bit, far exceeding the 8-bit range of cheap RGB modules.

🚫

IR Blocking Filter

Built-in filter eliminates infrared interference for stable, accurate readings under ambient light.

📺

Dual Display Output

Color name on LCD + animated RGB bar graph on OLED for a polished demo interface.

🔗

I2C — Minimal Wiring

Three devices on just two signal wires (SDA + SCL). Leaves most Arduino pins free.

⚙️

Adjustable Sensitivity

Gain and integration time can be tuned in software for bright or dim environments.

📡

Expandable

Easily add servo motors, conveyor belts, or IoT connectivity for real automation systems.

🏭 Applications

Smart color sorting machines
Packaging verification
Fruit ripeness detection
Food freshness monitoring
Line-following robots
Object classification bots
STEM / embedded systems labs
IoT prototype development

Comments

try for free