Color Sensor with Arduino Uno: TCS34725 explaination, wiring and code

4.8
(4)

Last Updated on 13th April 2024 by peppe8o

In this tutorial, I’ll show you how to interface the color sensor TCS34725 with Arduino, explaining with a wiring diagram, code, and components list.

To sort out the things with respect to the colors like different color balls, Arduino can interface the TCS34725 that is the cheap and best color sensor which can handle such application

TCS34725 Color Sensor Features

In order to differentiate the stunning colors or to detect the RGB TCS34725 is the best module to use. With the best color sensing elements, this module is easily available in the market.

  1. This module has an IR filter that filters out the IR spectrum resulting in a larger color spectrum that is viewable once the high accuracy is available.
  2. It is suitable and highly recommended that a sensor should be used behind the dark glass.

Working Explanation

color sensor

The constituents of RGB TCS34725 are a 3×4 photodiode array, analog to digital converter (ADC) data registers, and I2C interface. 3×4 photodiode array contains red filtered, green filtered, and blue filtered photodiodes. The photodiode current is converted into 16bit value by the analog to digital converters. After the completion of the conversion of the data, it sends results to data registers. The communication of the TCS34725 color sensor is carried out under the I2C communication protocol. The I2C protocol enables the sensor to be easily interfaced with any microcontroller.

In addition to the I2C protocol, TCS34725 has interrupt signal output. The interrupt enables the user to set upper and lower limit thresholds.

Maximum Ratings

Sr.ParameterVoltage
1Supply Voltage6V
2Input Terminal Voltage-0.5V to 3.8V
3Output Terminal Voltage-0.5V to 3.8V

TCS34725 Color Sensor Pinout

There are 7 pins in TCS34725 Color Sensor Module namely VCC, 3.3, GND, SCL, SDA, INT, and LED.

  • VCC: Module Power Supply 5v
  • 3.3 V: Module Power Supply 3.3V
  • GND: Ground Pin
  • SCL: Clock Pin which is connected to a dedicated A5 pin on Arduino Uno.
  • SDA: Data Pin which is connected to a dedicated A4 pin on Arduino Uno.

What We Need

Arduino Uno R3 board

As usual, I suggest adding from now to your favourite e-commerce shopping cart all the needed hardware, so that at the end you will be able to evaluate overall costs and decide if to continue with the project or remove them from the shopping cart. So, hardware will be only:

Check hardware prices with the following links:

Amazon Arduino Uno box
Amazon TCS34725 color sensor box
Amazon Breadboard box
Amazon Dupont Wiring box

Step-by-Step Procedure

Interfacing TCS34725 with Arduino Uno

The following diagram is self-explanatory when it comes to connecting the TCS34725 color sensor with Arduino Uno.

  • The LED pin is connected to any digital pin
  • SDA is a data pin that is connected to an A4 pin on Arduino Uno.
  • SCL is a clock pin that is connected with an A5 pin on Arduino Uno.
  • 3.3 V is connected with a 3V3 pin on Arduino Uno
  • GND is connected with a ground pin on Arduino Uno.

Please arrange the wiring as shown in the following picture, according to Arduino Uno Pinout:

color sensor with arduino connection_bb

Get the TCS34725 Color Sensor Code for Arduino

Connect your PC to Arduino and open Arduino IDE. For the very first steps, you can refer to Connecting Windows PC with Arduino tutorial. Get the .ino code from my download area with the following link:

Add the Adafruit_TCS34725-master.zip library in your Arduino IDE (if you need help in this, please refer to my Install Arduino Libraries: methods to add libraries with Arduino IDE). Open the “.ino” file in your Arduino IDE.

Please find below a brief code description.

TCS34725 Code Explaination

In the beginning, we include two libraries:

#include <Wire.h>
#include "Adafruit_TCS34725.h"
int led = 5;

We start the serial monitor at the baud rate of 9600. The following “IF” statement checks and warns whether the connections are right or not. If that condition gives value one, this means that connections are correct. Otherwise, please recheck your wiring.

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

  pinMode (led, OUTPUT); 
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }
}

Now, in the loop section, the following function detects RGB values. Then we print these values on the screen of the serial monitor.

void loop(void) {
  uint16_t r, g, b, c;
  tcs.getRawData(&r, &g, &b, &c);
  Serial.print("Red: "); Serial.print(r, DEC); Serial.print(" ");
  Serial.print("Green: "); Serial.print(g, DEC); Serial.print(" ");
  Serial.print("Blue: "); Serial.print(b, DEC); Serial.print(" ");
  Serial.print("Color: "); Serial.print(c, DEC); Serial.print(" ");
  Serial.println(" ");
analogWrite (led, c); 
  delay(100);
}

Determining the Color from RGB values

Every color has a distinct RGB value. From these values, you can judge any color. You can find easily on internet matrices showing the RGB values for every color. We can also find out the RGB value and code it.

For example, let’s suppose that we have a scenario whit two papers: one looks like red and the other one looks like green. We want that whenever the green color is detected, the door should open. And for the red color, the door must remain closed.  If the color of paper doesn’t match the standard values of RGB, we can still note the range of RGB values and, upon those values, red-colored paper and green color paper can be differentiated. Hence the reliance on standard values is not a must. We can also read values with the sensor, note them down, and programme according to real-time samples.

Result

As a result, the serial monitor will show the measured color:

TCS34725 colour sensor arduino serial monitor results

Enjoy!

Please find more tutorials on Arduino in peppe8o Arduino archives.

Umar Jamil

For any queries and help for work, please contact me at:
Whatsapp: +92-346-661-7017
Email: [email protected]

How useful was this post?

Click on a star to rate it anonymously!

Average rating 4.8 / 5. Vote count: 4

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?