Interfacing a TCS34725 RGB Color Sensor With Arduino – A Complete Guide
Introduction:
An RGB Color sensor accurately detects an object’s color. There are a lot of uses for color sensors: sorting packages based on color, detecting the freshness of perishable items, correcting lighting color, and controlling light based on screen content. This tutorial will help you set up the TCS34725 RGB Sensor for Arduino boards. We’re using Arduino UNO here. You’ll also find example code to communicate with the RGB color sensor.
What Is An RGB Sensor?
RGB stands for Red, Green, and Blue. An RGB sensor can independently detect the color intensity of red, green, and blue colors and measure brightness. The sensor does this by using an RGB color filter at it’s lens-for example, the red color filter allows only red light to pass through it.
Since the three colors can be used in different proportions to create many colors, RGB sensors can detect several colors using just those three. With an 8-bit RGB color sensor, the red, green, and blue values range from 0 to 255, and 256 * 256 * 256 = ~16 million colors!
Light entering the sensor from the lens falls on a photodiode, whose current output will vary depending on the amount of expose light. The current will be converted to a voltage using a signal conditioner which can be read using an ADC.
The IR block filter in the TCS34725 helps in sensing ambient light. The TCS34725 has an I2C interface (SDA and SCL) that connects to the Arduino Board. The IC also has an optional interrupt output pin which can be used to interrupt Arduino.
The table below gives the pin description of the color sensor IC – TCS34725.
TCS RGB Color Sensor Board with pinout
Table 2
Pin number
Pin Name
Pin Type
Description
Pin number
Pin Name
Pin Type
Description
1
1
VDD
Power
Supply voltage
2
2
SCL
Input
I2C Clock signal
3
3
GND
Power
Supply ground reference
4
4
NC
–
Do not connect
5
5
INT
Output
Open drain interrupt signal
6
6
SDA
Input/Output
I2C Data signal
There are no rows in this table
You can find the datasheet of the TCS34725 color sensor
Makerguides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to products on Amazon.com.
Step-By-Step Instructions To Connect A TCS34725 Color Sensor To Arduino
Here are the step-by-step details to connect the color sensor to your Arduino board. Several versions of the boards are available, but the guide applies to all the variants of the TCS34725 RGB sensor boards.
Here are the details required to complete the Arduino and the color sensor TCS34725 RGB sensor. The final connection will look like the image below.
Step 1: Unpack and prepare TCS34725 color sensor
There are seven pins available on this particular module. Depending on the module you have, the number of pins may differ. Make sure that you match the pins according to the module.
Step 2: Connect the GND pin on the color sensor module with Arduino
Connect the GND pin of the Arduino (there are multiple GND pins. You can choose one for eases of connection) to the GND pin on the RGB sensor.
Always start with the ground connection so both boards will have a common reference before making other connections!
Step 3: Connect the I2C data line next
Connect the Pin A4 on the Arduino to the SDA pin on the color sensor IC.
Step 4: Connect the I2C Clock line
Connect the pin A5 on the Arduino to the SCL pin on the color sensor IC (orange wire in the below picture)
A note about I2C lines-pins A4 and A5 on the Arduino pins have I2C as one of the functions if the I2C is used. You can read about the I2C pins and the supported Arduino I2C functions
Connect the Sensor pin labelled VIN to the 5 V pin on the Arduino. The RGB color sensor module also provides 3.3 V. You can leave the 3.3 V pin unconnected on the RGB color sensor.
This will complete the required connections between the Arduino and the TCS34725 RGB color sensor.
TCS34725 Library Installation And Arduino Code Examples
In this section you’ll install the Adafruit library for the RGB color sensor. This library comes with a host of useful examples which you can easily edit and apply to your projects.
Step 1: Open Library manager
Open the Arduino IDE and click on Tools menu. Select “Manage Library” from the options available.
Step 2: Search for the Adafruit Library
Once you type “TCS” in the search bar, you will find a list of matching libraries available. Select the “Adafruit TCS34725” from the library options.
Click on the install button to install the Arduino Library.
Once installed, you’ll see the status as “installed”, as shown below.
Step 3: Open the Adafruit example code
You have installed the Adafruit library successfully. Refer to the below image to view the available example codes.
Click on File
Select Examples option
Browse and locate “Adafruit TCS34725” Example
Click on the Colorview example.
The example will open in the IDE. The code is given below.
#include "Wire.h"
#include "Adafruit_TCS34725.h"
// Pick analog outputs, for the UNO these three work well
// use ~560 ohm resistor between Red & Blue, ~1K for green (its brighter)
#define redpin 3
#define greenpin 5
#define bluepin 6
// for a common anode LED, connect the common pin to +5V
// for common cathode, connect the common to ground