Template: TCS34725 Color Sensor RPi Guide

From Waveshare Wiki
Jump to: navigation, search

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