TCS34725 Color Sensor

From Waveshare Wiki
Jump to: navigation, search
TCS34725 Color Sensor
TCS34725-Color-Sensor-intro.jpg

TCS34725 Color Sensor, I2C interfaces
{{{name2}}}

{{{name3}}}

{{{name4}}}

{{{name5}}}

{{{name6}}}

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.

TCS34725-Color-Sensor- (1).png

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

TCS34725-Color-Sensor- (2).png

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

TCS34725-Color-Sensor- (3).png

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.

TCS34725-Color-Sensor- (4).png

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

RPI open i2c.png

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

TCS34725 RPI 2.jpg

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:

C example
Python example

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:

TCS34725 RPI 3.jpg

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
TCS34725 STM32 2.jpg

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.
TCS34725 STM32 1.jpg

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
TCS34725 Arduino 2.jpg

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.
MQ5 Arduino 1.jpg
  • 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.
TCS34725 Arduino 1.jpg

Resources

Datasheet

FAQ

 Answer:
Please check if the pin connection is correct.

{{{5}}}


 Answer:
Please check if you connect the correct pins.
{{{3}}}
{{{4}}}

{{{5}}}


 Answer:
This prompt appears for the BCM2835 and wiringPi demo:

TCS34725 Color Sensor02.png
Python demo:
TCS34725 Color Sensor03.png
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.
TCS34725 Color Sensor04.png
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}}}


 Answer:
If running the wiringPi demo is normal, then running Python or BCM2835 may cause the screen to fail to refresh normally, because the bcm2835 library is a library function of the Raspberry Pi CPU chip, and the bottom layer is to directly operate the registers, while the bottom layer of the wiringPi library and python are read and written by reading and writing. The device file of the Linux system operates the device, which may cause the GPIO port to be abnormal. Restarting the Raspberry Pi can solve the problem perfectly.

{{{5}}}


 Answer:
Confirm whether the baud rate is set to 115200. For the STM32 routine, please confirm that the computer is correctly connected to the development board USART2 (PA2, PA3), PA2 is TXD, and the correct COM port is selected. Control Panel -> Hardware -> Device Manager.

TCS34725 Color Sensor05.png

{{{5}}}


 Answer:
Please check the hardware connection first, and press the RESET button if there is no problem.

{{{5}}}


 Answer:
In this case, the light intensity exceeds the inspection range, and reducing the gain can solve the problem (modify in the initial, or rewrite the gain setting TCS34725_Set_Gain(TCS34725_GAIN_16X) after the initialization).
{{{3}}}
{{{4}}}

{{{5}}}


 Answer:
Because the integration time determines the maximum value of the RGBC channel data, modifying the integration time will result in darker or whiter colors. Just need to increase or decrease the brightness of the LED.
{{{3}}}
{{{4}}}

{{{5}}}


 Answer:
The interrupt is compared with the data in the Clear channel. The data in the Clear channel is related to the integration time. After actual measurement, the gain is 60 times.

TCS34725 Integrate time.jpg
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.

{{{3}}}
{{{4}}}

{{{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)