Difference between revisions of "TCS34725 Color Sensor"

From Waveshare Wiki
Jump to: navigation, search
Line 1: Line 1:
 +
<div class="tabber">
 +
<div class="tabbertab" title="Introduction">
 
{{Infobox item|colorscheme=green
 
{{Infobox item|colorscheme=green
 
|img=[[File:TCS34725-Color-Sensor-intro.jpg|300px| link=https://www.waveshare.com/tcs34725-color-sensor.htm]]
 
|img=[[File:TCS34725-Color-Sensor-intro.jpg|300px| link=https://www.waveshare.com/tcs34725-color-sensor.htm]]
Line 9: Line 11:
 
|interface1 = I2C
 
|interface1 = I2C
 
}}
 
}}
== Introduction ==
+
{{TCS34725 Color Sensor Spec}}
TCS34725 Color Sensor, I2C interfaces
+
</div>
 
+
<div class="tabbertab" title="Raspberry Pi Guides">
{{Amazon|{{#urlget:amazon|default}}=display
+
{{TCS34725 Color Sensor RPi Guide}}
|More = [https://www.waveshare.com/tcs34725-color-sensor.htm More]}}
+
</div>
 +
<div class="tabbertab" title="STM32 Guides">
 +
{{TCS34725 Color Sensor STM32 Guide}}
 +
</div>
 +
<div class="tabbertab" title="Arduino Guides">
 +
{{TCS34725 Color Sensor Arduino Guide}}
 +
</div>
 +
<div class="tabbertab" title="Resources">
  
 
==Resources==
 
==Resources==
Line 23: Line 32:
  
 
*[[:File:TCS3472 Datasheet.pdf|TCS3472]]
 
*[[:File:TCS3472 Datasheet.pdf|TCS3472]]
 
+
</div>
==FAQ==
+
<div class="tabbertab" title="FAQ">
 
+
{{FAQ|Whiy does the data are all be 0?
 +
|Please check if you connect the correct pins
 +
}}
 +
{{FAQ| All the RGB data output is 253 as the picture. <br />
 +
:[[File:TCS34725-Color-Sensor- (27).png|400px]]
 +
|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).
 +
}}
 +
{{FAQ|The color is abnormal after changing the integration time?
 +
|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.
 +
}}
 +
{{FAQ|Modifying the integration time cannot trigger an interrupt or keep repeating the interrupt?
 +
|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. <br />
 +
[[File:TCS34725 Integrate time.jpg|400px]] <br />
 +
Therefore, you should modify the threshold value if sample rate is fast. And please increase brightness of LED when you set integrate time to 2.4ms.<br />
 +
}}
 +
</div>
 +
<div class="tabbertab" title="Supports">
 
==Support==
 
==Support==
 
{{Service00}}
 
{{Service00}}
 +
</div>
 +
</div>

Revision as of 09:33, 27 November 2021

TCS34725 Color Sensor
TCS34725-Color-Sensor-intro.jpg

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

{{{name3}}}

{{{name4}}}

{{{name5}}}

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 the bookworm system, you can only use lgpio library, bcm2835 and wiringPi can't be installed and used.

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

#Open the Raspberry Pi terminal and run the following command
wget https://github.com/joan2937/lg/archive/master.zip
unzip master.zip
cd lg-master
sudo make install

# You can refer to the official website for more: 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
 Answer:
Please check if you connect the correct pins
{{{3}}}
{{{4}}}

{{{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 sample rate is fast. And please increase brightness of LED when you set integrate time to 2.4ms.

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

{{{5}}}


Support

Support

If you require technical support, please go to the Support page and open a ticket.