ECE 110/Equipment/Color Sensor

From PrattWiki
Jump to navigation Jump to search

Introduction

The TCS34725 RGB Color Sensor from Adafruit is a device that can take, and examine, the components of color in the light shining on its sensor. The library that comes with it has several handy functions for initializing the device and then using it. The end result is a system that will report back the red, blue, and green components of the light it sees as values between 0 and 255. The range of this sensor is relatively limited and it is highly impacted by any light noise, including the reflectivity of the object whose color you are scanning.

Note: This sensor uses the data and clock lines (SDA and SCL); as a result, it cannot be used at the same time as the ECE 110/Equipment/IR Thermal Sensor unless you disable one device's pullup resistors!

Leads

  • LED: N/R (Ground this to turn off onboard LED)
  • INT: N/R (Interrupt)
  • SDA: Data line
  • SCL: Clock line
  • 3V3: N/R
  • GND: Ground
  • VIN: Supply voltage, typically 5 V

Operation

The Adafruit TCS34725 Library has functions that handle much of the interaction between the Arduino and the color sensor. You will need to include the library at the start of your sketch with the code:

#include "Adafruit_TCS34725.h"

Next you will create an instance of the sensor object in your code. The library creates a new object of type Adafruit_TCS34725 that you use. The line below will create an object of that type called tcs. The arguments are the integration time (exposure time) and the gain. The colorview.ino example in the library uses a 50ms integration time and a 4x gain, so those are what are used here:

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

In the setup, you need to tell the sensor to begin. The colorview.ino example in the library has code to see whether the sensor actually started; here is a version of that code:

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor Test");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
}

Once the sensor has been initialized, you can use the library's built-in getRGB command to ask the sensor for the RGB values. The values will be reported back as floating point numbers between 0.0 and 255.0. The colorview.ino example in the library has code that reads and prints the values. Note that there is a delay in the code to make sure the previous reading has been taken - that time is related to the integration time specified above. The code below is a simplified version of the code in colorview.ino.

void loop() {
  float red, green, blue;

  delay(60);  // takes 50ms to read
  tcs.getRGB(&red, &green, &blue);

  Serial.print("R:\t"); Serial.print(int(red)); 
  Serial.print("\tG:\t"); Serial.print(int(green)); 
  Serial.print("\tB:\t"); Serial.print(int(blue));
  Serial.print("\n");
}

Sample Code

Here is the full, simplified sample code (adapted from colorview.ino in the Adafruit TCS34725 Library):

// Based on colorview.ino from Adafruit_TCS34725 library
// Simplified and modified to work with CX-Bot

#include "Adafruit_TCS34725.h"

// Initialize color sensor
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

void setup() {
  Serial.begin(9600);
  Serial.println("Sensor Test!");

  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
}

void loop() {
  float red, green, blue;

  delay(60);  // takes 50ms to read
  tcs.getRGB(&red, &green, &blue);

  Serial.print("R:\t"); Serial.print(int(red)); 
  Serial.print("\tG:\t"); Serial.print(int(green)); 
  Serial.print("\tB:\t"); Serial.print(int(blue));
  Serial.print("\n");
}

Notes

  • The best way to get an accurate reading is with the color swatch close to the sensor and some kind of shading around the sensor to reduce additional light sources.

References