Example Code for Raspberry Pi – TCS34725 I2C Color Sensor
Last revision 2026/01/20
This article offers a detailed guide on using the TCS34725 I2C Color Sensor with a Raspberry Pi, showcasing setup instructions and Python code for accurate RGB color detection.
Introduction
Want to know the secret to chameleons’ ability to change color? Want know how the most popular electronic color picking pen picks color?
Don't think too complicated, because their principle is really simple. Nature, is the best teacher of mankind. There are so many interesting ideas inspired by creatures.
Through tens of millions of years of derivation, the chameleon has formed a biological instinct, which can be perfectly hidden in the surroundings by changing the protective color of its skin. This is a process from "color picking" to "color matching".
Today, electronic color picking pens use the same principle. First, obtain the RGB three primary color values by detecting the color of the object. Then blend these values to get the color of the object. It's just like kneading plasticine when you were a kid. After knowing a certain ratio, you can knead the color you want.
Hardware Preparation
TCS34725 is a low-cost and cost-effective RGB full-color sensor. The sensor recognizes the surface color of an object through optical sensing. It supports the three primary colors of red, green, and blue (RGB), supports bright light sensing, and can output the corresponding specific values to help restore the true colors.

For a higher precision, it specifically employs a IR blocking filter at bottom to avoid interference from the surroundings, minimizing the infrared spectrum component of the incident light. In this case, it gets a more accurate color management. Besides, this sensor also includes four ultra-bright LEDs to allow itself to work without external light resources. And the module works via I2C bus, featuring PH2.0-4P and XH2.54 (breadboard) interfaces to meet different using requirements.
Wiring Diagram
- Power the Raspberry Pi on and install the Raspberry Pi expansion board correctly
- Connect the sensor to corresponding IIC interface on the expansion board

- Configure to enable I2C and restart the Raspberry Pi. If configured, you can skip this step. Configure the Raspberry Pi according to the following procedure and restart it.

Sample Code
- Install I2C libraries and tools, and you need to get your Raspberry Pi connect to internet for this step(skip if installed). In the terminal, type the following instructions and press ‘Enter’
sudo apt-get install i2c-tools
- When the I2C device is connected, the I2C address can be checked by the following command. In the terminal, type the following instructions and press ‘Enter’
sudo i2cdetect -y -a 1

- Read all register data of I2C device. In the terminal, type the following instructions and press ‘Enter’
sudo i2cdump -y 1 0x29
-y means cancelling the user interaction process and directly executing the command
1 is the I2C device number
0×29 is I2C device address

- Open Thonny Python IDE to copy the following program into it
import smbus
import time
bus = smbus.SMBus(1)
addr = 0x29
CDATAL = 0x94
CDATAH = 0x95
RDATAL = 0x96
RDATAH = 0x97
GDATAL = 0x98
GDATAH = 0x99
BDATAL = 0x9a
BDATAH = 0x9b
while True:
ClearL = bus.read_byte_data(addr , CDATAL)
ClearH = bus.read_byte_data(addr , CDATAH)
Clear = ClearH * 0x100 + ClearL
print("Clear = 0X%x" %Clear)
RedL = bus.read_byte_data(addr , RDATAL)
RedH = bus.read_byte_data(addr , RDATAH)
Red = RedH * 0x100 + RedL
print("Red = 0X%x" %Red)
GreenL = bus.read_byte_data(addr , GDATAL)
GreenH = bus.read_byte_data(addr , GDATAH)
Green = GreenH * 0x100 + GreenL
print("Green = 0X%x" %Green)
BlueL = bus.read_byte_data(addr , BDATAL)
BlueH = bus.read_byte_data(addr , BDATAH)
Blue = BlueH * 0x100 + BlueL
print("Blue = 0X%x" %Blue)
print(" ")
time.sleep(1)

- Click ‘Run’, it will print the color information.
Was this article helpful?
