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.
🧰 Components Required
📦 Required Arduino Libraries
| Library | Purpose | Install As |
|---|---|---|
| Wire.h | I2C communication (built-in) | Built-in — no install needed |
| Adafruit_TCS34725 | TCS34725 RGB color sensor driver | Search "Adafruit TCS34725" |
| LiquidCrystal_I2C | I2C 16x2 LCD control | Search "LiquidCrystal I2C" |
| Adafruit_GFX | Graphics library (OLED dependency) | Search "Adafruit GFX" |
| Adafruit_SSD1306 | SSD1306 OLED display driver | Search "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
| Component | Component Pin | Arduino Pin | Notes |
|---|---|---|---|
| TCS34725 Sensor | SDA | A4 | I2C Data |
| TCS34725 Sensor | SCL | A5 | I2C Clock |
| TCS34725 Sensor | VIN / VCC | 5V | Power |
| TCS34725 Sensor | GND | GND | Ground |
| TCS34725 Sensor | LED (enable) | A0 | Set HIGH to enable onboard LED |
| 16x2 LCD (I2C) | SDA | A4 | Shared I2C — address 0x27 |
| 16x2 LCD (I2C) | SCL | A5 | Shared I2C Clock |
| 16x2 LCD (I2C) | VCC | 5V | Power |
| 16x2 LCD (I2C) | GND | GND | Ground |
| SSD1306 OLED | SDA | A4 | Shared I2C — address 0x3C |
| SSD1306 OLED | SCL | A5 | Shared I2C Clock |
| SSD1306 OLED | VCC | 3.3V or 5V | Check your module spec |
| SSD1306 OLED | GND | GND | Ground |
🛠️ Step-by-Step Build Instructions
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.
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.
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.
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.
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.
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.
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.
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.
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
#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.
Comments
Post a Comment