Example Code for Arduino-Color Detection

In this tutorial, we'll detect the specimen RGB value, and simulate it with RGB LEDs

Hardware Preparation

  • DFRduino UNO (or similar) x 1
  • RGB LED Module
  • M-M/F-M/F-F Jumper wires

Software Preparation

Wiring Diagram

SEN0212 Gravity: TCS34725 RGB Color Sensor For Arduino Connection Diagram

The sensor should be placed above the specimen, 3 ~ 10 mm**

Sample Code

/*!
 * @file  colorview.ino
 * @brief Gets the ambient light color
 * @copyright   Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license     The MIT License (MIT)
 * @author      PengKaixing([email protected])
 * @version     V1.0.0
 * @date        2022-03-16
 * @url         https://github.com/DFRobot/DFRobot_TCS
 */
#include "DFRobot_TCS34725.h"

DFRobot_TCS34725 tcs = DFRobot_TCS34725(&Wire, TCS34725_ADDRESS,TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_1X);
void setup() 
{
  Serial.begin(115200);
  Serial.println("Color View Test!");

  while(tcs.begin() != 0)
  {
    Serial.println("No TCS34725 found ... check your connections");
    delay(1000);
  }
}

void loop() {
  uint16_t clear, red, green, blue;
  tcs.getRGBC(&red, &green, &blue, &clear);
  tcs.lock();  
  Serial.print("C:\t"); Serial.print(clear);
  Serial.print("\tR:\t"); Serial.print(red);
  Serial.print("\tG:\t"); Serial.print(green);
  Serial.print("\tB:\t"); Serial.print(blue);
  Serial.println("\t");
  
  // Figure out some basic hex code for visualization
  uint32_t sum = clear;
  float r, g, b;
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
  Serial.print("\t");
  Serial.print((int)r, HEX); Serial.print((int)g, HEX); Serial.print((int)b, HEX);
  Serial.println();
}

Result

SEN0212 Gravity: TCS34725 RGB Color Sensor For Arduino Expected Results

Was this article helpful?

TOP