tutorialspinouttools
Sensors · ESP32-C3

TCS34725 on OLED: RGB Color Sensor

Free ESP32 & Arduino tutorial · wiring, code & parts · gokul.builds

Overview & Wiring

What it is: The TCS34725 reads the colour of a surface as red, green, blue and clear (brightness) values, with an onboard white LED to illuminate the target. Great for colour-sorting robots and mood lamps. How it works: I2C device at address 0x29. It returns 16-bit raw R/G/B/C counts; this sketch scales them to the familiar 0–255 range using the clear channel. Shares SDA/SCL with the OLED. Wiring (shared I2C bus with the OLED): TCS34725 SDA -> GPIO4 TCS34725 SCL -> GPIO5 TCS34725 VIN -> 3V3 TCS34725 GND -> GND (optional) LED pin -> GND to force the white LED off, or leave open for on OLED VCC=3V3, GND=GND, SDA=GPIO4, SCL=GPIO5 Libraries: "Adafruit TCS34725" (installs "Adafruit BusIO"), plus "Adafruit SSD1306" and "Adafruit GFX Library". On screen / Serial: R, G and B values 0–255. Hold coloured paper about 1 cm away and watch the channels change. Gotchas: Hold the target close and steady; ambient light throws readings off, which is why the onboard LED helps. Address 0x29 is fixed.

Circuit Diagram

TCS34725 on OLED: RGB Color Sensor wiring diagram for ESP32-C3

Parts You Need

ESP32-C3 SuperMiniBuy →
0.96" SSD1306 OLEDBuy →

Source Code (Arduino / ESP32)

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_TCS34725.h>

Adafruit_SSD1306 display(128, 64, &Wire, -1);
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_154MS, TCS34725_GAIN_4X);

void setup() {
  Serial.begin(115200);
  Wire.begin(4, 5);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  if (!tcs.begin()) {
    Serial.println("TCS34725 not found");
    display.setTextColor(SSD1306_WHITE);
    display.setCursor(0, 28); display.println("TCS34725 not found");
    display.display();
    while (1) delay(10);
  }
}

void loop() {
  uint16_t r, g, b, c;
  tcs.getRawData(&r, &g, &b, &c);
  int R = c ? (int)r * 255 / c : 0;           // scale by clear channel
  int G = c ? (int)g * 255 / c : 0;
  int B = c ? (int)b * 255 / c : 0;

  display.clearDisplay();
  display.setTextColor(SSD1306_WHITE);
  display.setTextSize(1);
  display.setCursor(0, 0);  display.println("TCS34725 Color");
  display.setTextSize(2);
  display.setCursor(0, 16); display.printf("R %d", R);
  display.setCursor(0, 34); display.printf("G %d", G);
  display.setCursor(0, 52); display.setTextSize(1); display.printf("B %d", B);
  display.display();

  Serial.printf("R %d  G %d  B %d\n", R, G, B);
  delay(500);
}
Browse all tutorials ESP32-C3 pinout Calculators