Home Raspberry PI Raspberry Pi and TCS34725 Color Sensor example in C

Raspberry Pi and TCS34725 Color Sensor example in C

by iainhendry

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.

Key Features

  • Integrated IR blocking filter
  • 3.8M:1 dynamic range
  • Four independent analog-to-digital converters
  • A reference-channel for Color Analysis (Clear channel photo-diode)




pi and TCS34725

pi and TCS34725




save the file below as TCS34725.c

[codesyntax lang=”cpp”]

// 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

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <fcntl.h>

void main()
// Create I2C bus
int file;
char *bus = “/dev/i2c-1”;
if ((file = open(bus, O_RDWR)) < 0)
printf(“Failed to open the bus. \n”);
// Get I2C device, TCS34725 I2C address is 0x29(41)
ioctl(file, I2C_SLAVE, 0x29);

// Select enable register(0x80)
// Power ON, RGBC enable, wait time disable(0x03)
char config[2] = {0};
config[0] = 0x80;
config[1] = 0x03;
write(file, config, 2);
// Select ALS time register(0x81)
// Atime = 700 ms(0x00)
config[0] = 0x81;
config[1] = 0x00;
write(file, config, 2);
// Select Wait Time register(0x83)
// WTIME : 2.4ms(0xFF)
config[0] = 0x83;
config[1] = 0xFF;
write(file, config, 2);
// Select control register(0x8F)
// AGAIN = 1x(0x00)
config[0] = 0x8F;
config[1] = 0x00;
write(file, config, 2);

// Read 8 bytes of data from register(0x94)
// cData lsb, cData msb, red lsb, red msb, green lsb, green msb, blue lsb, blue msb
char reg[1] = {0x94};
write(file, reg, 1);
char data[8] = {0};
if(read(file, data, 8) != 8)
printf(“Erorr : Input/output Erorr \n”);
// Convert the data
int cData = (data[1] * 256 + data[0]);
int red = (data[3] * 256 + data[2]);
int green = (data[5] * 256 + data[4]);
int blue = (data[7] * 256 + data[6]);

// Calculate luminance
float luminance = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);
if(luminance < 0)
luminance = 0;

// Output data to screen
printf(“Red color luminance : %d lux \n”, red);
printf(“Green color luminance : %d lux \n”, green);
printf(“Blue color luminance : %d lux \n”, blue);
printf(“IR luminance : %d lux \n”, cData);
printf(“Ambient Light Luminance : %.2f lux \n”, luminance);



compile this like the following

gcc TCS34725.c -o TCS34725 -lwiringPi - lm


run this like this

sudo ./TCS34725


This is the output you should see


TCS34725 output

TCS34725 output


TCS34725 RGB Color Sensor with IR Filter and White LED for Arduino UNO R3

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More