Arduino颜色传感器-Color Sensor-TCS34725 颜色传感器
简介
- 若有一个能为你判断物体颜色的装置,是否为你的生活增添了不少色彩?
- Color Sensor-TCS34725 颜色传感器是你不错的选择,TCS34725 芯片提供红、绿、蓝 (RGB) 以及明光感应的数字返回值。
- 色敏光电二极管集成片裁和局部化的红外遮光滤光片,最大程度减小了入射光的红外频谱成份,让颜色管理更加准确。
- 高敏感性、宽动态范围以及红外遮光滤光片使得 TCS34725 成为光线条件变化和通过衰减材料条件下的理想色敏元件解决方案。
- TCS34725 彩色传感器有着广泛的应用,包括 RGB LED 背光控制,固态照明、 健康产品、工业过程控制和医疗诊断设备等。
产品参数
- 工作电压: 3.3-5V
- 检测距离:3-10mm
- 接口: IIC接口和2.54间距接口
- 尺寸: 18.5mm*23mm
引脚说明
使用教程
检测红色色卡的三基值。
所需硬件
- UNO x1
- 颜色传感器 x1
- Cookie-IO Expansion Shield x1
- 4P传感器线 x1
所需软件
Arduino IDE 版本1.6.8 点击下载Arduino IDE
接线图
#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(60); // 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]);
}
结果
红色R的值高于蓝色B和绿色G。
本文整理于DFRobot wiki
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号