TCS3200 Arduino Library
Comprehensive Arduino library for TCS3200 Color Sensor.
Loading...
Searching...
No Matches
Examples and Usage

This page contains the comprehensive list of examples and usage of TCS3200 Arduino library.

Setup and Initialization

To use the TCS3200 Arduino Library, include the "tcs3200.h" header file and create an instance of the TCS3200 class. Use the begin() function to initialize the TCS3200 sensor, configure pins, and set default integration time and frequency scaling.

#include "tcs3200.h"
// Pin assignments for S0, S1, S2, S3, and OUT pins
#define S0_PIN 2
#define S1_PIN 3
#define S2_PIN 4
#define S3_PIN 5
#define OUT_PIN 6
// Create an instance of TCS3200
TCS3200 colorSensor(S0_PIN, S1_PIN, S2_PIN, S3_PIN, OUT_PIN);
void setup() {
// Initialize the sensor
colorSensor.begin();
}
TCS3200 Color Sensor Arduino Library
Class representing the TCS3200 color sensor.
Definition TCS3200.h:219

Calibration

The TCS3200 sensor requires calibration for accurate color sensing. Calibration involves capturing readings for both the lightest and darkest colors to establish the range for color intensity mapping. Use the calibrate() function to enable calibration mode, and after invoking calibrate_light() and calibrate_dark() functions to calibrate the sensor with white and black reference colors, respectively.

void setup() {
// Initialize the sensor
colorSensor.begin();
// Perform the actual calibration
Serial.println("Please face the sensor to light surface.");
colorSensor.calibrate_light(); // Calibrate with a white reference color
delay(2000);
Serial.println("Please face the sensor to dark surface.");
colorSensor.calibrate_dark(); // Calibrate with a black reference color
colorSensor.calibrate();
}

Reading Color Intensity

To obtain color intensity values for individual color channels (red, green, blue, and clear), use the following functions: read_red(), read_green(), read_blue(), and read_clear(). The read_rgb_color() function returns the RGB color intensity values as an RGBColor structure.

void loop() {
// Read color intensity values for individual color channels
uint8_t redIntensity = colorSensor.read_red();
uint8_t greenIntensity = colorSensor.read_green();
uint8_t blueIntensity = colorSensor.read_blue();
uint8_t clearIntensity = colorSensor.read_clear();
// Or read RGB color intensity values as an RGBColor structure
RGBColor color = colorSensor.read_rgb_color();
}
Structure to represent RGB color values.
Definition TCS3200.h:167

Integration Time and Frequency Scaling

The library provides control over the integration time and frequency scaling of the TCS3200 sensor. Integration time is set using the integration_time() function, and frequency scaling is set using the frequency_scaling() function. Integration time affects the accuracy and sensitivity of color measurements, while frequency scaling optimizes the trade-off between accuracy and response time.

void setup() {
// Initialize the sensor
colorSensor.begin();
// Set integration time to 2500 milliseconds (2.5 seconds)
colorSensor.integration_time(2500);
// Set frequency scaling to 20%
colorSensor.frequency_scaling(TCS3200_OFREQ_20P);
}
#define TCS3200_OFREQ_20P
20% frequency scaling
Definition TCS3200.h:159

White Balancing

White balancing is essential for achieving accurate color measurements. It allows users to calibrate the sensor based on a known white reference color. Use the white_balance() function to set the white balance with an RGBColor structure, and white_balance() to retrieve the current white balance settings.

void setup() {
// Initialize the sensor
colorSensor.begin();
// Perform white balancing with a known white reference color (255, 255, 255)
RGBColor whiteReference = {255, 255, 255};
colorSensor.white_balance(whiteReference);
}

Color Space Conversions

The TCS3200 library supports color space conversions from RGB to other color spaces, including HSV, CMYK, and CIE 1931 XYZ. Use the corresponding functions (read_hsv(), read_cmyk(), and read_cie1931()) to obtain color representations in the desired color space.

void loop() {
// Read color in HSV color space
HSVColor hsvColor = colorSensor.read_hsv();
// Read color in CMYK color space
CMYKColor cmykColor = colorSensor.read_cmyk();
// Read color in CIE 1931 XYZ color space
CIE1931Color cie1931Color = colorSensor.read_cie1931();
}
Structure to represent CIE 1931 XYZ color values.
Definition TCS3200.h:201
Structure to represent CMYK color values.
Definition TCS3200.h:189
Structure to represent HSV color values.
Definition TCS3200.h:178

Nearest Color Detection

The library provides a template function nearest_color() that allows users to find the nearest color label based on the current sensor readings. This is useful for applications like color classification and sorting.

void setup() {
// Initialize the sensor
colorSensor.begin();
// Color labels and corresponding RGBColor values
String colorLabels[] = {"Red", "Green", "Blue", "Yellow", "Orange"};
RGBColor colorValues[] = {
{255, 0, 0},
{0, 255, 0},
{0, 0, 255},
{255, 255, 0},
{255, 165, 0}
};
// Find the nearest color label based on current sensor readings
String nearestColorLabel = colorSensor.nearest_color<String>(colorLabels, colorValues, 5);
}

Interrupt Callbacks

The library allows users to define interrupt callbacks that trigger when the sensor readings exceed specified color thresholds. Use the upper_bound_interrupt() and lower_bound_interrupt() functions to set upper and lower color thresholds, respectively.

// Define callback functions
void upperThresholdExceeded() {
// Code to execute when the sensor readings exceed the upper threshold
}
void lowerThresholdExceeded() {
// Code to execute when the sensor readings fall below the lower threshold
}
void setup() {
// Initialize the sensor
colorSensor.begin();
// Set interrupt callbacks with upper and lower color thresholds
RGBColor upperThreshold = {200, 200, 200};
RGBColor lowerThreshold = {100, 100, 100};
colorSensor.upper_bound_interrupt(upperThreshold, upperThresholdExceeded);
colorSensor.lower_bound_interrupt(lowerThreshold, lowerThresholdExceeded);
}

Integration in the Loop

The TCS3200 library supports continuous color sensing and interrupt triggering in the loop. The loop() function should be called in the main loop to trigger interrupt callbacks if defined.

void loop() {
// Perform color sensing and execute interrupt callbacks if thresholds are exceeded
colorSensor.loop();
}