Arduino 驱动 TCS34725颜色识别模块

Arduino 驱动 TCS34725颜色识别模块

简介

TCS34725提供了红、绿、蓝(RGB)和清晰的光传感值的数字返回。一个红外阻挡滤光片,集成在芯片上,并定位到彩色传感光电二极管,最小化入射光的红外光谱成分,并允许准确地进行颜色测量。高灵敏度、宽动态范围和红外阻挡滤镜使TCS34725成为一个理想的颜色传感器解决方案,可在不同的照明条件下和通过衰减材料使用。
在这里插入图片描述

电气特征

1.工作电压:3.3~5V
2.检测距离:3~10mm
3.通信方式:IIC
4.电源管理:低功率-2.5uA休眠状态,65uA等待状态与可编程等待状态时间从2.4 ms到>7m

时序图

在这里插入图片描述

TCS34725光-数字转换器包含一个3×4光电二极管阵列,四个模数转换器(ADC),它集成了光电二极管电流,数据寄存器,一个状态机,和一个I2C接口。3×4光电二极管阵列由红滤、绿滤、蓝滤和透明(未过滤)光电二极管组成。此外,光电二极管被涂有一个红外阻挡滤波器。四个集成的adc同时将放大的光电二极管电流转换为一个16位的数字值。在一个转换周期完成后,结果被传输到数据寄存器,这些寄存器被双缓冲,以确保数据的完整性。所有的内部定时,以及低功耗的等待状态,都是由状态机控制的。TCS34725数据的通信是通过一个快速到400 kHz的双线I2C串行总线来完成的。行业标准I2C总线便于方便、直接地连接到微控制器和嵌入式处理器。除了I2C总线外,TCS34725还提供了一个单独的中断信号输出。当启用中断并超过用户定义的阈值时,将断言活动低中断并保持断言,直到控制器清除为止。

接线

Arduino TCS34725
Vcc 5v
GND GND
A4 SDA
A5 SCL

代码

#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]);
}

结果

在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_42250136/article/details/131854548