Arduino driver TCS34725 color recognition module

Arduino driver TCS34725 color recognition module

Introduction

The TCS34725 provides digital return of red, green, blue (RGB) and clear light sensing values. An IR-blocking filter, integrated on-chip and positioned to the color-sensing photodiode, minimizes the IR spectral component of the incident light and allows accurate color measurement. High sensitivity, wide dynamic range, and IR blocking filters make the TCS34725 an ideal color sensor solution for use under varying lighting conditions and through attenuating materials.
insert image description here

electrical characteristics

1. Working voltage: 3.3~5V
2.Detection distance: 3~10mm
3. Communication method: IIC
4. Power management: low power - 2.5uA sleep state, 65uA wait state with programmable wait state time from 2.4 ms to >7m

Timing diagram

insert image description here

The TCS34725 light-to-digital converter contains a 3×4 photodiode array, four analog-to-digital converters (ADCs), which integrate photodiode current, data registers, a state machine, and an I2C interface. The 3×4 photodiode array consists of red-filtered, green-filtered, blue-filtered, and clear (unfiltered) photodiodes. Additionally, the photodiode is coated with an IR blocking filter. Four integrated ADCs simultaneously convert the amplified photodiode current into a 16-bit digital value. After one conversion cycle is complete, the results are transferred to data registers, which are double-buffered to ensure data integrity. All internal timing, as well as low-power wait states, are controlled by the state machine. TCS34725 data communication is through a fast to 400 kHz two-wire I2C serial bus to complete. The industry-standard I2C bus allows easy, direct connection to microcontrollers and embedded processors. In addition to the I2C bus, the TCS34725 also provides a separate interrupt signal output. When interrupts are enabled and a user-defined threshold is crossed, the active-low interrupt is asserted and remains asserted until cleared by the controller.

wiring

Arduino TCS34725
Vcc 5v
GND GND
A4 SDA
A5 SCL

the code

#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

// set to false if using a common cathode LED
#define commonAnode true

// our RGB -> eye-recognized gamma color
byte gammatable[256];

Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);

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

  if (tcs.begin()) {
    
    
    Serial.println("Found sensor");
  } else {
    
    
    Serial.println("No TCS34725 found ... check your connections");
    while (1); // halt!
  }
  
  // use these three pins to drive an LED
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  
  // thanks PhilB for this gamma table!
  // it helps convert RGB colors to what humans see
  for (int i=0; i<256; i++) {
    
    
    float x = i;
    x /= 255;
    x = pow(x, 2.5);
    x *= 255;
      
    if (commonAnode) {
    
    
      gammatable[i] = 255 - x;
    } else {
    
    
      gammatable[i] = x;      
    }
    //Serial.println(gammatable[i]);
  }
}


void loop() {
    
    
  uint16_t clear, red, green, blue;

  tcs.setInterrupt(false);      // turn on LED

  delay(500);  // takes 50ms to read 
  
  tcs.getRawData(&red, &green, &blue, &clear);

  tcs.setInterrupt(true);  // turn off LED
  
  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);
  // 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();

  //Serial.print((int)r ); Serial.print(" "); Serial.print((int)g);Serial.print(" ");  Serial.println((int)b );

  analogWrite(redpin, gammatable[(int)r]);
  analogWrite(greenpin, gammatable[(int)g]);
  analogWrite(bluepin, gammatable[(int)b]);
}

result

insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/131854548