TCS34725 Color Sensor
| ||
Overview
This is a color sensor module based on TCS34725, which will output RGB data and light intensity through the I2C interface. Its advantages include high sensitivity, wide dynamic range, accurate measuring, etc.
Features
- Onboard TCS34725FN, embedded ADC, high sensitivity, wide dynamic range.
- I2C communication uses a few pins.
- Integrates IR blocking filter, minimizes the IR spectral component of the incoming light.
- Outputs RGB data without white balance.
- Outputs light intensity, feel the light like human eyes.
- Supports light intensity interrupt output and programmable upper and lower thresholds.
- Supports fill-light by onboard LED, and adjustable brightness by PWM.
- Onboard voltage level translator, compatible with 3.3V/5V operating voltage.
Specifications
- Sensor: TCS34725FN
- Communication interface: I2C
- Operating voltage: 3.3V/5V
- Dimension: 27mm × 20mm
- Resolution: 4-ch RGBC, 16-bit per channel
- Recommended measuring distance: 2mm
Pinouts
Pin | Function |
VCC | 3.3V/5V |
GND | Power ground |
SDA | I2C data input |
SCL | I2C clock pin |
INT | Interrupt output (open drain output) |
LED | Light emitting diode |
Controller
TCS34725 is used for color sensing. TCS34725 is an I2C bus-based color light-to-digital converter with an IR filter, providing a digital return of red, green, blue (RGB), and clear light sensing values. 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.
Communication Protocol
I2C bus has two lines, one is a data line (SDA) and another is a lock line (SDL). There are three kinds of signals when communicating, Start signal, Stop signal, and Answer signal.
Start signal: When SCL is High, SDA change from High to Low, and it starts to transmit data.
Stop signal: When SCL is High, SDA change from Low to High, and it stops transmitting.
Answer signal: Every time IC sends back a certain Low plus to the sender after it receives 8 bits of data.
I2C Write
When working, Raspberry Pi (hereafter named Master) will first send a Start signal, then send a byte to TCS34725(hereafter named Slaver), whose first 7 bits are the address of Slaver and 1-bit write bit. The slave responds with an Answer signal every time it receives any data. The master sends the command register address to Slaver, then the data of the command register. Stop signals are sent to slaves to stop communicating.
I2C Read
When working, Master will first send a Start signal, then send a byte to Slaver, whose first 7 bits are the address of Slaver and 1-bit write bit. The slave responds with an Answer signal every time it receives any data. The master sends the command register address to the Slave. After that, Mater will send a Start signal again, and then send a byte (7-bit address and 1-bit read bit) to Slaver. Slaver response and send data of the register to Master, master answer as well. Stop signals will be sent to stop communicating.
I2C Address
The I2C device address of TCS34725 is 0x29.
Note: 0x29 is 7bit in fact, therefore, when you set the I2C address, you should left-shift one bit, and turn it to 0x52.
Enable I2C Interface
Open a terminal and run the following commands:
sudo raspi-config Choose Interfacing Options -> I2C -> Yes.
Reboot Raspberry Pi:
sudo reboot
Install Library
If you use bookworm system, only the lgpio library is available, bcm2835 and wiringPi libarary cannot be installed or used. Please note that the python library does not need to install, you can directly run the demo.
BCM2835
#Open the Raspberry Pi terminal and run the following command wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.71.tar.gz tar zxvf bcm2835-1.71.tar.gz cd bcm2835-1.71/ sudo ./configure && sudo make && sudo make check && sudo make install # For more, you can refer to the official website at: http://www.airspayce.com/mikem/bcm2835/
WiringPi
#Open the Raspberry Pi terminal and run the following command cd sudo apt-get install wiringpi #For Raspberry Pi systems after May 2019 (earlier than that can be executed without), an upgrade may be required: wget https://project-downloads.drogon.net/wiringpi-latest.deb sudo dpkg -i wiringpi-latest.deb gpio -v # Run gpio -v and version 2.52 will appear, if it doesn't it means there was an installation error # Bullseye branch system using the following command: git clone https://github.com/WiringPi/WiringPi cd WiringPi . /build gpio -v # Run gpio -v and version 2.70 will appear, if it doesn't it means there was an installation error
lgpio
wget https://github.com/joan2937/lg/archive/master.zip unzip master.zip cd lg-master sudo make install #for more details, you can refer to https://github.com/gpiozero/lg
Python
#python2 sudo apt-get update sudo apt-get install python-pip sudo apt-get install python-pil sudo apt-get install python-numpy sudo pip install RPi.GPIO sudo pip install spidev
Download Examples
Open a terminal of the Raspberry Pi:
sudo apt-get install p7zip-full -y sudo wget https://files.waveshare.com/upload/a/a1/TCS34725_Color_Sensor_code.7z 7z x TCS34725_Color_Sensor_code.7z -O./TCS34725_Color_Sensor_code cd TCS34725_Color_Sensor_code/RaspberryPi/
Hardware Connection
TCS34725 | Raspberry Pi | Description |
Board order | ||
VCC | 5V | Power input |
GND | GND | Power ground |
SDA | 3 | I2C data input |
SCL | 5 | I2C clock pin |
INT | 11 | Interrupt output (open drain output) |
LED | 12 | Light emitting diode |
Run Examples
C Examples
BCM2835
cd bcm2835 sudo make clean sudo make sudo ./main
WiringPi
cd wiringPi sudo make clean sudo make sudo ./main
Python Example
cd python sudo python main.py
Expected Result
The RGB data are printed in the terminal:
R, G, and B values are printed in RGB888 format (DEC), C is a light value without processing, and RGB565 and RGB888 are HEX data printed in a certain format. LUX is light value processed. CT is color temperature. (https://en.wikipedia.org/wiki/Color_temperature) If you want to measure CT, please turn off the LED. INT is interrupted, 1: light value is over the threshold.
You can turn the RGB value to color with tools:
The STM32 examples are based on the STM32F103RBT6 and the STM32H743. The connection provided below is based on the STM32F103RB. If you need to use other STM32 boards, you may need to change the hardware connection and port the code yourself.
STM32
Hardware Connection
Sensor | STM32 | Description |
---|---|---|
VCC | 3.3V | Power input |
GMD | GND | Power ground |
SDA | PB9 | I2C data input |
SCL | PB8 | I2C clock pin |
INT | PB4 | Interrupt output (open drain output) |
LED | PB10 | Light emitting diode |
Examples
The examples are developed based on the HAL libraries. Download the Demo codes archive to your PC. Unzip and find the STM32 project from TCS34725_Color_Sensor_code\STM32\XNUCLEO-F103RB\MDK-ARM.
- Open the TCS34725_Color_Sensoc.uvprojx file by Keil.
- Build and the project.
- Program the project to your STM32 board.
- Connect the UART1 of your STM32 board to the PC and check the serial data with SSCOM software.
The Arduino example is written for the Arduino UNO. If you want to connect it to other Arduino boards, you may need to change the connection.
Arduino
Hardware Connection
Sensor | Arduino | Description |
---|---|---|
VCC | 5V | Power input |
GMD | GND | Power ground |
SDA | SDA | I2C data input |
SCL | SCL | I2C clock input |
INT | D5 | Interrupt output (open drain output) |
LED | D6 | Light emitting diode |
Examples
- Download the demo codes to your PC and unzip them.
- Install the Arduino IDE on your PC.
- Go into TCS34725_Color_Sensor_code/Arduino/Color_Sensor.
- Run the Color_Sensor.ino file.
- Select the correct Board and the Port.
- Build the project and upload it to the board.
- Open the serial monitor of the Arduino IDE or the SSCOM software and check the serial data.
Resources
Datasheet
FAQ
{{{5}}}
{{{5}}}
Python demo:
If the above problem occurs, it is a device data I2C data transmission error. Most of them are hardware connection errors, please check whether the hardware connection is correct, check whether there is any problem with the hardware connection, and run i2cdetect -y 1 If the IIC address is displayed, it means that the hardware connection is no problem.
If the hardware connection is correct, it may be caused by incorrect use of Raspberry Pi control (see below for details), just restart the Raspberry Pi.
{{{5}}}
{{{5}}}
Question:STM32 and Arduino demo serial port output RGB data are all 0 or initialization failed? As shown
{{{5}}}
{{{5}}}
{{{5}}}
Question:Modifying the integration time cannot trigger an interrupt or keep repeating the interrupt?
Therefore, you should modify the threshold value if the sample rate is fast. And please increase the brightness of the LED when you set integrate time to 2.4ms.
{{{5}}}
Support
Technical Support
If you need technical support or have any feedback/review, please click the Submit Now button to submit a ticket, Our support team will check and reply to you within 1 to 2 working days. Please be patient as we make every effort to help you to resolve the issue.
Working Time: 9 AM - 6 PM GMT+8 (Monday to Friday)