ESP8266-Arduino编程实例-TCS34725颜色传感器驱动

TCS34725颜色传感器驱动

1、TCS34725介绍

TCS3472 器件提供红色、绿色、蓝色 (RGB) 和清晰光感应值的数字返回。 集成在芯片上并定位于颜色传感光电二极管的 IR 阻挡滤光片可最大限度地减少入射光的 IR 光谱分量,并允许准确地进行颜色测量。 高灵敏度、宽动态范围和 IR 阻挡滤光片使 TCS3472 成为理想的颜色传感器解决方案,可在不同的照明条件下并通过衰减材料使用。 该数据通过 I2C 传输到主机。

在这里插入图片描述

2、硬件准备

  • ESP8266 NodeMCU开发板一块
  • TCS34725传感器模块一个
  • 面板板一个
  • 杜邦线若干
  • 数据线一条

硬件接线如下:

在这里插入图片描述

3、软件准备

  • Arduino IDE或VSCode + PlatformIO

在前面的文章中,对如何搭建ESP8266开发环境做了详细的介绍,请参考:

ESP8266 NodeMCU的引脚介绍在前面的文章中做了详细的介绍,请参考:

4、代码实现

本次实例使用的TCS34725驱动库如下:

1)导入依赖库头文件

1
2
3
4
5
6
#include <Wire.h>
#include "Adafruit\_TCS34725.h"

// 创建TCS34725设备
Adafruit_TCS34725 tcs = Adafruit\_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

2)设备初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
void setup(void) {
// 串口初始化
Serial.begin(9600);
// TCS34725初始化
if (tcs.begin()) {
Serial.println("Found sensor");
} else {
Serial.println("No TCS34725 found ... check your connections");
while (1);
}

}

3)颜色检测

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void loop(void) {
uint16\_t r, g, b, c, colorTemp, lux;

tcs.getRawData(&r, &g, &b, &c);
colorTemp = tcs.calculateColorTemperature(r, g, b);
lux = tcs.calculateLux(r, g, b);

Serial.print("Color Temp: "); Serial.print(colorTemp, DEC); Serial.print(" K - ");
Serial.print("Lux: "); Serial.print(lux, DEC); Serial.print(" - ");
Serial.print("R: "); Serial.print(r, DEC); Serial.print(" ");
Serial.print("G: "); Serial.print(g, DEC); Serial.print(" ");
Serial.print("B: "); Serial.print(b, DEC); Serial.print(" ");
Serial.print("C: "); Serial.print(c, DEC); Serial.print(" ");
Serial.println(" ");
}

文章来源: https://iotsmart.blog.csdn.net/article/details/126436750