A look at the TCS34725 Color sensor with various examples

In this example we look at the TCS34725 color sensor with arduino, raspberry pi and mbed examples

The TCS34725 device provides a digital return of red, green, blue (RGB), and clear light sensing values. An IR blocking filter, integrated on-chip and localized to the color sensing photodiodes, minimizes the IR spectral component of the incoming light and allows color measurements to be made accurately. The high sensitivity, wide dynamic range, and IR blocking filter make the TCS34725 an ideal color sensor solution for use under varying lighting conditions and through attenuating materials. This data is transferred via an I2C to the host.

Features

  • Integrated IR blocking filter
  • 3.8M:1 dynamic range
  • Four independent analog-to-digital converters
  • A reference channel for color analysis (clear channel photodiode)

Typically you will use a breakout so you can easily connect to your board, here is the sensor I used

Arduino example

This will work on many platforms that you can add to the Arduino IDE, I have tested the ESP32, ESP8266 and Micro:bit. All you have to do is connect the I2C as per the module connections

Parts List

Name link
Arduino Uno UNO R3 CH340G/ATmega328P, compatible for Arduino UNO
TCS34725 Color Sensor TCS34725 Color Sensor RGB color sensor
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

arduino and tcs34725 layout
arduino and tcs34725 layout

Code

We will use the adafruit library, you can add this to the Arduino IDE using the library manager – https://github.com/adafruit/Adafruit_TCS34725

This is one of the default example

[codesyntax lang=”cpp”]

#include <Wire.h>
#include "Adafruit_TCS34725.h"

/* Example code for the Adafruit TCS34725 breakout library */

/* Connect SCL to analog 5
Connect SDA to analog 4
Connect VDD to 3.3V DC
Connect GROUND to common ground */

/* Initialise with default values (int time = 2.4ms, gain = 1x) */
// Adafruit_TCS34725 tcs = Adafruit_TCS34725();

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

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

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

// Now we're ready to get readings!
}

void loop(void) {
uint16_t r, g, b, c, colorTemp, lux;

tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);

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(" ");
}

[/codesyntax]

Open the serial monitor, this is what you should see

Color Temp: 4554 K – Lux: 379 – R: 1122 G: 831 B: 776 C: 1429
Color Temp: 3173 K – Lux: 181 – R: 475 G: 339 B: 272 C: 707
Color Temp: 3425 K – Lux: 224 – R: 604 G: 435 B: 364 C: 868
Color Temp: 2833 K – Lux: 1497 – R: 2983 G: 2240 B: 1461 C: 5723
Color Temp: 5847 K – Lux: 109 – R: 4109 G: 1327 B: 890 C: 5814
Color Temp: 2767 K – Lux: 460 – R: 4468 G: 1703 B: 1062 C: 6734
Color Temp: 4381 K – Lux: 463 – R: 1379 G: 1012 B: 938 C: 1789
Color Temp: 4276 K – Lux: 588 – R: 1464 G: 1136 B: 997 C: 2153
Color Temp: 3952 K – Lux: 646 – R: 1424 G: 1135 B: 933 C: 2350
Color Temp: 3528 K – Lux: 835 – R: 1713 G: 1362 B: 1036 C: 3101

 

Raspberry Pi example

Parts List

Name Link
Raspberry Pi 2018 new original Raspberry Pi 3 Model B+ (plug) Built-in Broadcom 1.4GHz quad-core 64 bit processor Wifi Bluetooth and USB Port
TCS34725 Color Sensor TCS34725 Color Sensor RGB color sensor
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

pi and tcs34725 layout
pi and tcs34725 layout

Code

Save this as tcs34725.py

[codesyntax lang=”python”]

# Distributed with a free-will license.
# Use it any way you want, profit or free, provided it fits in the licenses of its associated works.
# TCS34725
# This code is designed to work with the TCS34725_I2CS I2C Mini Module available from ControlEverything.com.
# https://www.controleverything.com/products
# NT

import smbus
import time

# Get I2C bus
bus = smbus.SMBus(1)

# I2C Address of the device
TCS34725_DEFAULT_ADDRESS			= 0x29

# TCS34725 Register Set
TCS34725_COMMAND_BIT				= 0x80
TCS34725_REG_ENABLE					= 0x00 # Enables states and interrupts
TCS34725_REG_ATIME					= 0x01 # RGBC integration time
TCS34725_REG_WTIME					= 0x03 # Wait time
TCS34725_REG_CONFIG					= 0x0D # Configuration register
TCS34725_REG_CONTROL				= 0x0F # Control register
TCS34725_REG_CDATAL					= 0x14 # Clear/IR channel low data register
TCS34725_REG_CDATAH					= 0x15 # Clear/IR channel high data register
TCS34725_REG_RDATAL					= 0x16 # Red ADC low data register
TCS34725_REG_RDATAH					= 0x17 # Red ADC high data register
TCS34725_REG_GDATAL					= 0x18 # Green ADC low data register
TCS34725_REG_GDATAH					= 0x19 # Green ADC high data register
TCS34725_REG_BDATAL					= 0x1A # Blue ADC low data register
TCS34725_REG_BDATAH					= 0x1B # Blue ADC high data register

# TCS34725 Enable Register Configuration
TCS34725_REG_ENABLE_SAI				= 0x40 # Sleep After Interrupt
TCS34725_REG_ENABLE_AIEN			= 0x10 # ALS Interrupt Enable
TCS34725_REG_ENABLE_WEN				= 0x08 # Wait Enable
TCS34725_REG_ENABLE_AEN				= 0x02 # ADC Enable
TCS34725_REG_ENABLE_PON				= 0x01 # Power ON

# TCS34725 Time Register Configuration
TCS34725_REG_ATIME_2_4				= 0xFF # Atime = 2.4 ms, Cycles = 1
TCS34725_REG_ATIME_24				= 0xF6 # Atime = 24 ms, Cycles = 10
TCS34725_REG_ATIME_101				= 0xDB # Atime = 101 ms, Cycles = 42
TCS34725_REG_ATIME_154				= 0xC0 # Atime = 154 ms, Cycles = 64
TCS34725_REG_ATIME_700				= 0x00 # Atime = 700 ms, Cycles = 256
TCS34725_REG_WTIME_2_4				= 0xFF # Wtime = 2.4 ms
TCS34725_REG_WTIME_204				= 0xAB # Wtime = 204 ms
TCS34725_REG_WTIME_614				= 0x00 # Wtime = 614 ms

# TCS34725 Gain Configuration
TCS34725_REG_CONTROL_AGAIN_1		= 0x00 # 1x Gain
TCS34725_REG_CONTROL_AGAIN_4		= 0x01 # 4x Gain
TCS34725_REG_CONTROL_AGAIN_16		= 0x02 # 16x Gain
TCS34725_REG_CONTROL_AGAIN_60		= 0x03 # 60x Gain

class TCS34725():
	def __init__(self):
		self.enable_selection()
		self.time_selection()
		self.gain_selection()
	
	def enable_selection(self):
		"""Select the ENABLE register configuration from the given provided values"""
		ENABLE_CONFIGURATION = (TCS34725_REG_ENABLE_AEN | TCS34725_REG_ENABLE_PON)
		bus.write_byte_data(TCS34725_DEFAULT_ADDRESS, TCS34725_REG_ENABLE | TCS34725_COMMAND_BIT, ENABLE_CONFIGURATION)
	
	def time_selection(self):
		"""Select the ATIME register configuration from the given provided values"""
		bus.write_byte_data(TCS34725_DEFAULT_ADDRESS, TCS34725_REG_ATIME | TCS34725_COMMAND_BIT, TCS34725_REG_ATIME_700)
		
		"""Select the WTIME register configuration from the given provided values"""
		bus.write_byte_data(TCS34725_DEFAULT_ADDRESS, TCS34725_REG_WTIME | TCS34725_COMMAND_BIT, TCS34725_REG_WTIME_2_4)
	
	def gain_selection(self):
		"""Select the gain register configuration from the given provided values"""
		bus.write_byte_data(TCS34725_DEFAULT_ADDRESS, TCS34725_REG_CONTROL | TCS34725_COMMAND_BIT, TCS34725_REG_CONTROL_AGAIN_1)

	def readluminance(self):
		"""Read data back from TCS34725_REG_CDATAL(0x94), 8 bytes, with TCS34725_COMMAND_BIT, (0x80)
		cData LSB, cData MSB, Red LSB, Red MSB, Green LSB, Green MSB, Blue LSB, Blue MSB"""
		data = bus.read_i2c_block_data(TCS34725_DEFAULT_ADDRESS, TCS34725_REG_CDATAL | TCS34725_COMMAND_BIT, 8)
		
		# Convert the data
		cData	= data[1] * 256 + data[0]
		red		= data[3] * 256 + data[2]
		green	= data[5] * 256 + data[4]
		blue	= data[7] * 256 + data[6]
		
		# Calculate luminance
		luminance = (-0.32466 * red) + (1.57837 * green) + (-0.73191 * blue)
		
		return {'c' :  cData, 'r' : red, 'g' : green, 'b' : blue, 'l' : luminance}

from TCS34725 import TCS34725
tcs34725 = TCS34725()

while True:
	lum = tcs34725.readluminance()
	print "Clear Data Color Luminance : %d lux"%(lum['c'])
	print "Red Color Luminance : %d lux"%(lum['r'])
	print "Green Color Luminance : %d lux"%(lum['g'])
	print "Blue Color Luminance : %d lux"%(lum['b'])
	print "Ambient Light Luminance : %.2f lux"%(lum['l'])
	print " ***************************************************** "
	time.sleep(1)

[/codesyntax]

You should see something like this when the python script is run

tcs34725 pi output
tcs34725 pi output

MBed example

Parts List

Name Link
Mbed 1768
TCS34725 Color Sensor TCS34725 Color Sensor RGB color sensor
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Schematic/Connection

 

mbed and TCS34725 layout
mbed and TCS34725 layout

Code

This example uses the following library which you can import into the MBED compiler –  https://os.mbed.com/components/Adafruit-TCS34725-RGB-Color-Sensor/

[codesyntax lang=”cpp”]

#include "mbed.h"

I2C i2c(p9, p10); //pins for I2C communication (SDA, SCL)
Serial pc(USBTX, USBRX);

int sensor_addr = 41 << 1;


int main() {
    pc.baud(9600);

    
    // Connect to the Color sensor and verify whether we connected to the correct sensor. 
    
    i2c.frequency(200000);
    
    char id_regval[1] = {146};
    char data[1] = {0};
    i2c.write(sensor_addr,id_regval,1, true);
    i2c.read(sensor_addr,data,1,false);
    

    
    // Initialize color sensor
    
    char timing_register[2] = {129,0};
    i2c.write(sensor_addr,timing_register,2,false);
    
    char control_register[2] = {143,0};
    i2c.write(sensor_addr,control_register,2,false);
    
    char enable_register[2] = {128,3};
    i2c.write(sensor_addr,enable_register,2,false);
    
    // Read data from color sensor (Clear/Red/Green/Blue)
    
    while (true) { 
        char clear_reg[1] = {148};
        char clear_data[2] = {0,0};
        i2c.write(sensor_addr,clear_reg,1, true);
        i2c.read(sensor_addr,clear_data,2, false);
        
        int clear_value = ((int)clear_data[1] << 8) | clear_data[0];
        
        char red_reg[1] = {150};
        char red_data[2] = {0,0};
        i2c.write(sensor_addr,red_reg,1, true);
        i2c.read(sensor_addr,red_data,2, false);
        
        int red_value = ((int)red_data[1] << 8) | red_data[0];
        
        char green_reg[1] = {152};
        char green_data[2] = {0,0};
        i2c.write(sensor_addr,green_reg,1, true);
        i2c.read(sensor_addr,green_data,2, false);
        
        int green_value = ((int)green_data[1] << 8) | green_data[0];
        
        char blue_reg[1] = {154};
        char blue_data[2] = {0,0};
        i2c.write(sensor_addr,blue_reg,1, true);
        i2c.read(sensor_addr,blue_data,2, false);
        
        int blue_value = ((int)blue_data[1] << 8) | blue_data[0];
        
        // print sensor readings
        
        pc.printf("Clear (%d), Red (%d), Green (%d), Blue (%d)\r\n", clear_value, red_value, green_value, blue_value);
        wait(0.5);
    }
    
}

[/codesyntax]

Using a terminal program such as TeraTerm you should see something like this when run

Clear (4248), Red (2551), Green (2133), Blue (1913)
Clear (3323), Red (1739), Green (1630), Blue (1458)
Clear (3323), Red (1739), Green (1630), Blue (1458)
Clear (4069), Red (3035), Green (2304), Blue (2184)
Clear (4124), Red (3078), Green (2338), Blue (2215)
Clear (4133), Red (3084), Green (2341), Blue (2218)
Clear (4235), Red (3128), Green (2390), Blue (2244)
Clear (5749), Red (2732), Green (2673), Blue (1664)
Clear (4989), Red (3010), Green (2574), Blue (2025)
Clear (4112), Red (3071), Green (2331), Blue (2210)
Clear (4112), Red (3071), Green (2331), Blue (2210)
Clear (2820), Red (2077), Green (1557), Blue (1474)
Clear (424), Red (275), Green (206), Blue (162)
Clear (409), Red (267), Green (202), Blue (160)
Clear (1167), Red (841), Green (634), Blue (578)
Clear (4342), Red (3158), Green (2387), Blue (2233)
Clear (6270), Red (3737), Green (3013), Blue (2516)
Clear (4007), Red (2979), Green (2250), Blue (2132)

 

Links

TCS34725 RGB Light Color Sensor Recognition Module

LEAVE A REPLY

Please enter your comment!
Please enter your name here