TCS34725 RGB Color Sensor with Arduino Tutorial

TCS34725 with Arduino wiring diagram schematic pinout

Things used in this tutorial:

Hardware components

Parts are available on Amazon via the links below. You can click the plus sign on the right for additional information about the specific item.
NameDescriptionQuantityBuy
TCS34725 RGB color sensorSold by Adafruit and other manufacturers.x 1Amazon
Arduino UNO R3The motor shield works with the Arduino UNO. You can also use the SMD version.x 1Amazon
USB Type-B cableUsed to power the Arduino UNOx 1Amazon
BreadboardI highly recommend to buy at least 1 good quality breadboard like the BusBoard Prototype Systems BB400 or BB830.x 1Amazon
Jumper wires~ 10Amazon
RGB LED stick (WS2812 / Neopixel)You can also use an RGB ring or LED strip. Note that you can only power roughly 8 LED pixels with the Arduino at full brightness. If you want to use many LEDs you will need an external power supply.x 1Amazon

My sensor didn’t come with the header pins installed, so I soldered them in place so I could use the sensor with a breadboard.

Tools

NameDescriptionBuy
Soldering ironI like the Hakko FX888D.Amazon

Software

PictureNameLink
Arduino-LogoArduino IDE

*Note: the above links are affiliated which means – at no additional cost to you – if you purchase anything using them you’re helping to support my work on this site. My humble thanks (I really appreciate it)! 

Wiring - Connecting TCS34725 RGB color sensor to Arduino

Wiring the TCS34725 to an Arduino or other microcontroller is super easy, you only need to connect 4 wires.

I soldered the header pins that came with the sensor in place, so I could wire it up using a breadboard.

The wiring diagram/schematic below shows you how to connect the sensor. 

The sensor communicates with the Arduino using I2C, this requires a clock pin (SCL) and a data pin (SDA). On older Arduinos the analog pin A4 functions as the SDA pin and A5 as the SCL pin. On my Arduino UNO there is also a dedicated SCK and SDA pin next to AREF close to digital pin 13, but the analog pins work fine too.

TCS34725 with Arduino wiring diagram schematic pinout
TCS34725 RGB color sensor with Arduino wiring diagram.

The connections are also given in the table below. 

TCS34725 RGB Color Sensor to Arduino Connections

TCS34725 RGB Color SensorArduino
VIN5V
GNDGND
SDAA4
SCLA5

The rest of the pins can be left disconnected.

Example codes for TCS34725 color sensor and Arduino

In order to simplify the programming, we will be using the Adafruit TCS34725 library in the next two examples.

To install the library navigate to Tools > Manage Libraries… or type Ctrl + Shift + I when using Windows. The Library Manager will open and update the list of installed libraries.

Installing an Arduino library step 1 open Library Manager

Search for ‘tcs34725‘ and look for the library by Adafruit. Select the latest version and then click Install.

Installing an Arduino library step 2 TCS34725

In the first example we will test the functionality of the sensor. In the second example we will hook up an RGB LED strip to the Arduino, which will light up in the color measured by the TCS34725 sensor. 

1. Example code for TCS34725: testing functionality

You can upload the following example code to your Arduino using the Arduino IDE

Note that this is the slightly modified example code that comes with the library, written by Adafruit.

You can open the code in a new window by clicking on the button in the top right corner.

/*Example code for TCS34725 RGB Color Sensor with Arduino and Adafruit TCS34725 library. More info: https://www.makerguides.com/tcs34725-rgb-color-sensor-arduino-tutorial/ */

// Include the libraries:
#include <Wire.h>
#include <Adafruit_TCS34725.h>

// Define the variables:
uint16_t r, g, b, c, colorTemp, lux;

// Initialise with specific int time and gain values:
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup()
{
  // Begin Serial communication:
  Serial.begin(9600);

  // Check if the sensor is wired correctly:
  if (tcs.begin()) {
    Serial.println("Found sensor");
  }
  else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }
}

void loop()
{
  // Get the raw sensor data for the red, green, blue and clear photodiodes:
  tcs.getRawData(&r, &g, &b, &c);
  // Calculate the color temperature using all the sensor data:
  colorTemp = tcs.calculateColorTemperature_dn40(r, g, b, c);
  // Calculate lux using red, green and blue sensor data:
  lux = tcs.calculateLux(r, g, b);

  // Print the data to the Serial Monitor:
  Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
  Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
  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.print(" ");
  Serial.println(" ");
}

You should see the following output in the Serial Monitor (Ctrl + Shift + M).

Serial Monitor output

Code explanation:

The first step is to include the libraries that we will need. The Wire.h library is needed for the I2C communication and the Adafruit_TCS34725 library will be used to read and convert the sensor data. The Wire.h should come pre installed with the Arduino IDE.

// Include the libraries:
#include <Wire.h>
#include <Adafruit_TCS34725.h>

The next step is to define some variables that we can use to store the sensor data. The function that we will use later returns data in the uint16_t format, which can be a number between 0 and 65535.

We need variables for the raw data that the sensor provides for the red, green, blue and clear photodiodes and for the color temperature and the lux values that the library calculates.

// Define the variables:
uint16_t r, g, b, c, colorTemp, lux;

After this we create a new instance of the Adafruit_TCS34725 class named ‘tcs’. We also set a specific integration time and gain value. You can find the settings that are allowed here (line 140). You can play around a bit with these values, to improve the sensor readings.

To set the default integration time and gain value, just use:

Adafruit_TCS34725 tcs = Adafruit_TCS34725();

// Initialise with specific int time and gain values:
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

In the setup() section of the code, we begin serial communication with Serial.begin() at a baudrate of 9600. Make sure the serial monitor is also set to 9600. 

Next we check if the sensor is communicating properly with the tcs.begin(). If there is a problem with the connection, the code will remain stuck indefinitely in the while loop.

void setup()
{
  // Begin Serial communication:
  Serial.begin(9600);

  // Check if the sensor is wired correctly:
  if (tcs.begin()) {
    Serial.println("Found sensor");
  }
  else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }
}

2. Example code for TCS34725: RGB LED strip

The previous example is a good way to test the functionality of the sensor, but the output data is not very practical. For many applications you will probably want to use clean RGB values that are properly scaled. 

In this example I use a different function of the library to get RGB values between 0 and 255, which we will use to set the color of an Neopixel/WS2812 RGB LED stick. 

First you will need to wire up the LED stick with 8 individually addressable LEDs. Note that if you want to drive more than 8 LEDs you will need an external power supply. 

The wiring diagram below shows you how to wire the LED stick in combination with the sensor.

TCS34725-with-Arduino-and-Neopixel-Stick-wiring-diagram-schematic-pinout
TCS34725 RGB color sensor with Arduino and RGB LED stick wiring diagram.

The LED stick is connected to 5V and GND and the data line is connected to digital pin 2 of the Arduino.

An easy way to control WS2812 LEDs also known as Adafruit Neopixels, is to use the FastLED library. 

You can install the library in the same way as before, just search for ‘fastled‘.

Installing an Arduino library step 2 FastLED

One you have wired up the LED stick, you can upload the code below to the Arduino. Next I will explain how this code works.

/*Example code for TCS34725 RGB Color Sensor with Arduino and WS2812 LED stick and Adafruit TCS34725 library. More info: https://www.makerguides.com/tcs34725-rgb-color-sensor-arduino-tutorial/ */

// Include the libraries:
#include <Wire.h>
#include <Adafruit_TCS34725.h>
#include <FastLED.h>

// Define variables:
float r, g, b;
#define NUM_LEDS 8
#define BRIGHTNESS  100

// Define connections:
#define DATA_PIN 2

CRGB leds[NUM_LEDS];

// Initialise with specific int time and gain values:
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_1X);


void setup()
{
  // Begin Serial communication:
  Serial.begin(9600);

  // Check if the sensor is wired correctly:
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1);
  }

  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(BRIGHTNESS);
}

void loop()
{
  // Get calculated RGB values:
  tcs.getRGB(&r, &g, &b);

  // Convert to integers:
  int red = (int)r;
  int green = (int)g;
  int blue = (int)b;

  // Print the data to the Serial Monitor:
  Serial.print(red); Serial.print(" "); Serial.print(green); Serial.print(" "); Serial.println(blue);

  CRGB color = CRGB(red, green, blue);

  // Set all the LEDs to the measured color:
  fill_solid(leds, NUM_LEDS, color);
  FastLED.show();
}

Conclusion

Share on facebook
Share on twitter
Share on pinterest
Share on email
Share on whatsapp

Beginner

Creative Commons License