iic颜色识别模块代码
时间: 2024-01-30 13:02:52 浏览: 79
颜色识别模块.zip
```arduino
#include <Wire.h>
#include <Adafruit_TCS34725.h>
// 定义颜色传感器对象
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X);
void setup() {
Serial.begin(9600);
// 初始化颜色传感器
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}
}
void loop() {
// 读取颜色值
uint16_t r, g, b, c;
tcs.getRawData(&r, &g, &b, &c);
// 输出颜色值
Serial.print("R: "); Serial.print(r);
Serial.print(" G: "); Serial.print(g);
Serial.print(" B: "); Serial.print(b);
Serial.print(" C: "); Serial.print(c);
// 计算颜色温度和亮度
float temperature = tcs.calculateColorTemperature(r, g, b);
float lux = tcs.calculateLux(r, g, b);
// 输出颜色温度和亮度
Serial.print(" Color Temperature: "); Serial.print(temperature, 2); Serial.print(" K");
Serial.print(" Lux: "); Serial.print(lux, 2);
Serial.println();
delay(500);
}
```
阅读全文