TCS34725 color sensor with Arduino

FunduinoPublished on

Farben erkennen und die erkannte Farbe mit einer RGB-LED anzeigen

Der TCS34725 misst, wie viel Rot, Grün und Blau im Licht steckt, das auf ihn fällt. Eine weiße LED auf dem Modul leuchtet das Messobjekt dabei gleichmäßig aus, damit die Werte vergleichbar bleiben.

In dieser Anleitung liest du die Farbwerte am Seriellen Monitor mit und lässt dir anzeigen, welche Farbe der Sensor erkannt hat. Im letzten Schritt erweiterst du den Aufbau um eine RGB-LED, die die erkannte Farbe direkt sichtbar macht.

Small board with the TCS34725 color sensor and white LED
The TCS34725 color sensor module
Color sensor module with a colored paper strip over it
A colored strip is held over the color sensor
1 / 2

You need this

RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode — Common Cathode 0,49 € *Item no. F23106750About the product — RGB LED 5 mm - wired, 4 pins, 20 mA per color channel, common anode or cathode — Common Cathode
RGB LED 10 mm on PCB - common cathode, 5 V RGB LED 10 mm on PCB - common cathode, 5 V 2,20 € *Item no. F23106011About the product — RGB LED 10 mm on PCB - common cathode, 5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 4 parts together 15,55 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

Material

For the basic setup, you need:

  • 1× Arduino UNO R3 with a suitable USB cable
  • 1× TCS34725 color sensor module
  • 4× male-to-female jumper wires
  • Colored paper strips in red, green, and blue for testing

For the extension in the final step, add:

  • 1× common-cathode RGB LED or an RGB LED on a board
  • 3× 220-ohm resistors (not needed for the board version)
  • 1× breadboard

The RGB color space

The RGB color space consists of the three primary colors red, green, and blue. Almost all colors can be created by mixing these three. Each primary color has 256 brightness levels: 0 means off, 255 means full brightness.

Examples:

  • Red: 255, 0, 0
  • Green: 0, 255, 0
  • Blue: 0, 0, 255
  • Yellow: 255, 255, 0
  • Black: 0, 0, 0
  • White: 255, 255, 255

Color table with RGB values

Three overlapping circles in red, green, and blue
Additive color mixing in the RGB color space (source: Wikimedia Commons, Synthese+.svg)

The sensor and its connection

The TCS34725 distinguishes the proportions of red, green, and blue in the incoming light. The module also has a white LED that evenly illuminates the object being measured - it is only switched on for the measurement in the sketch.

Connect the sensor via the I2C bus:

  • VCC → 5V
  • GND → GND
  • SDA → A4
  • SCL → A5

The module uses a Grove connector. If you don't have a Grove shield, simply connect the four wires using jumper wires and connect them directly to the microcontroller.

Small board with the TCS34725 color sensor and white LED
The TCS34725 color sensor module
Grove connector with four colored wires and their assignments
Grove connector pinout
1 / 2

Install library

Open the Library Manager in the Arduino IDE via Sketch → Include Library → Manage Libraries and search for Adafruit TCS34725.

After clicking “Install,” the IDE asks whether to install the required dependencies as well. Confirm by clicking “Install all.”

Arduino IDE Library Manager with the Adafruit TCS34725 library
The “Adafruit TCS34725” library in the Library Manager

Display color values in the Serial Monitor

Upload the sketch and open the Serial Monitor at 9600 baud. The three color values will appear side by side. If you hold a red, green, or blue strip over the sensor, the program will also report which color it detected.

The measured values depend heavily on the ambient brightness. Monitor your own readings in the Serial Monitor and adjust the threshold - 100 in this example - to suit your setup. Always keep the object being measured at roughly the same distance from the sensor, otherwise the readings will fluctuate even more.

Circuit diagram with Arduino and color sensor
Setup without RGB LED: only the color sensor on the I2C bus
Color sensor module with a colored paper strip over it
A colored strip is held over the color sensor
1 / 2
Sketch 1: Display color values in the Serial Monitor
#include "Adafruit_TCS34725.h"

/*
  TCS34725_INTEGRATIONTIME
  Laengere Zeiten erhoehen die Empfindlichkeit, besonders bei schwachem Licht.
  Moegliche Werte: 2_4MS, 50MS, 101MS, 154MS, 700MS
  -----
  TCS34725_GAIN
  Empfindlichkeit einstellen: 1X, 4X, 16X, 60X
*/
Adafruit_TCS34725 RGBSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

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

  // Farbsensor starten
  if (RGBSensor.begin())
  {
    Serial.println("Sensor gefunden!");
  }
  // Wenn der Sensor nicht gefunden wurde
  else
  {
    Serial.println("Sensor nicht gefunden");
    // Programm anhalten
    while (1);
  }
}

void loop()
{
  // Variablen fuer die Farben
  float rot, gruen, blau;

  // Beleuchtungs-LED des Moduls einschalten
  RGBSensor.setInterrupt(false);
  delay(60);

  // Daten erfassen
  RGBSensor.getRGB(&rot, &gruen, &blau);

  // Beleuchtungs-LED wieder ausschalten
  RGBSensor.setInterrupt(true);

  // Farbwerte anzeigen
  Serial.print("rot:\t");
  Serial.print(int(rot));
  Serial.print("\tgruen:\t");
  Serial.print(int(gruen));
  Serial.print("\tblau:\t");
  Serial.println(int(blau));

  // Grenzwert, ab dem die entsprechende Farbe als erkannt gilt.
  // Im Beispiel 100 - mit Hilfe der Anzeige im Seriellen Monitor anpassen.
  if (rot > 100)   Serial.println("roter Streifen");
  if (gruen > 100) Serial.println("gruener Streifen");
  if (blau > 100)  Serial.println("blauer Streifen");

  delay(500);
}

Extension: Make color values visible with an RGB LED

Now you’ll expand the circuit with an RGB LED. Hold a colored strip over the sensor, and the LED will light up in the detected color.

Connect the LED’s three pins to pins 6 (red), 5 (green), and 4 (blue), and the common pin to GND. Each of the three color pins needs its own 220-ohm resistor, unless you’re using an RGB LED on a board that already has resistors.

The sketch assumes an RGB LED with a common cathode: HIGH turns a color on. With a common-anode LED, it’s exactly the opposite - connect the common pin to 5V, and HIGH and LOW switch meanings.

Circuit diagram with Arduino, color sensor, and RGB LED
Extended setup with RGB LED on pins 4, 5, and 6
Sketch 2: Visualizing color values with an RGB LED
#include "Adafruit_TCS34725.h"

/*
  TCS34725_INTEGRATIONTIME
  Laengere Zeiten erhoehen die Empfindlichkeit, besonders bei schwachem Licht.
  Moegliche Werte: 2_4MS, 50MS, 101MS, 154MS, 700MS
  -----
  TCS34725_GAIN
  Empfindlichkeit einstellen: 1X, 4X, 16X, 60X
*/
Adafruit_TCS34725 RGBSensor = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

// Pins der RGB-LED
int roteLED = 6;
int grueneLED = 5;
int blaueLED = 4;

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

  pinMode(roteLED, OUTPUT);
  pinMode(grueneLED, OUTPUT);
  pinMode(blaueLED, OUTPUT);

  // Farbsensor starten
  if (RGBSensor.begin())
  {
    Serial.println("Sensor gefunden!");
  }
  // Wenn der Sensor nicht gefunden wurde
  else
  {
    Serial.println("Sensor nicht gefunden");
    // Programm anhalten
    while (1);
  }
}

void loop()
{
  // Variablen fuer die Farben
  float rot, gruen, blau;

  // Beleuchtungs-LED des Moduls einschalten
  RGBSensor.setInterrupt(false);
  delay(60);

  // Daten erfassen
  RGBSensor.getRGB(&rot, &gruen, &blau);

  // Beleuchtungs-LED wieder ausschalten
  RGBSensor.setInterrupt(true);

  // Farbwerte anzeigen
  Serial.print("rot:\t");
  Serial.print(int(rot));
  Serial.print("\tgruen:\t");
  Serial.print(int(gruen));
  Serial.print("\tblau:\t");
  Serial.println(int(blau));

  // Jeden Kanal einzeln schalten: ueber dem Grenzwert an, darunter aus.
  // Grenzwert mit Hilfe der Anzeige im Seriellen Monitor anpassen.
  if (rot > 100)
  {
    digitalWrite(roteLED, HIGH);
    Serial.println("roter Streifen");
  }
  else digitalWrite(roteLED, LOW);

  if (gruen > 100)
  {
    digitalWrite(grueneLED, HIGH);
    Serial.println("gruener Streifen");
  }
  else digitalWrite(grueneLED, LOW);

  if (blau > 100)
  {
    digitalWrite(blaueLED, HIGH);
    Serial.println("blauer Streifen");
  }
  else digitalWrite(blaueLED, LOW);

  delay(500);
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.