SKU:RB-02S161 IIC 颜色传感器

来自ALSROBOT WiKi
跳转至: 导航搜索
02S161000.png

目录

产品概述

IIC颜色传感器使用TCS34725颜色传感器进行颜色识别。TCS34725是一款高性价比的RGB全彩颜色识别传感器,传感器通过光学感应来识别物体的表面颜色。支持红、绿、蓝(RGB)三基色,支持明光感应,可以输出对应的具体数值,帮助您还原颜色本真。
为了提高精度,防止周边环境干扰,我们在传感器上添加了一个镜头罩,有效减少外界杂光干扰传感器,让颜色管理更加准确。板载自带2个高亮LED,可以让传感器在低环境光的情况下依然能够正常使用,实现“补光”的功能。模块采用I2C通信,拥有4P防插反接口,更加便利。

产品参数

基本参数

1.品名:IIC颜色传感器
2.货号:RB-02S161
3.品牌:奥松机器人
4.产地:哈尔滨
5.尺寸:25*40
6.固定孔:M3*2

电气参数

1.接口类型:KF2510-4P防插反接口
2.信号类型:IIC通讯
3.指示灯:高亮LED
4.工作电压:5V
5.工作电流:100mA
6.引脚定义:

+:电源正极
-:电源负极
SDA:IIC数据端口
SCL:IIC时钟端口

7.扩展接口:

LED:两个板载LED控制端口,悬空或高电平点亮,低电平熄灭
INT:中断引脚

8.连接线:4P 传感器连接线
9.检测范围:红、绿、蓝及白光检测
10.工作温度:-40 - 85℃
产品尺寸图:

02S16101.png

使用方法

example1_Arduino

  • 主要硬件
Arduino UNO 控制器
传感器扩展板 V5.0
IIC 颜色传感器
USB 数据线
  • 硬件连接
02S161020.png
  • 示例程序
/* Example code for the ALS_TCS34725 breakout library */

/* Connect SCL    to analog 5
   Connect SDA    to analog 4
   Connect VDD    to 5V DC
   Connect GROUND to common ground */

#include <Wire.h>
#include "ALS_TCS34725.h"

ALS_TCS34725 tcs = ALS_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);

void setup(void) {
  Serial.begin(9600);
  
  if (tcs.begin()) {
    Serial.println("Found sensor");
  } else {
    Serial.println("No TCS34725 found");
    while (1);
  }
  }

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(" ");
}
  • 程序效果

打开 Arduino IDE 自带的串口监视器,将颜色传感器放于不同颜色的表面,可以看到串口监视器中打印不同的数值

02S16103.png

example2_Arduino

  • 主要硬件
Arduino UNO 控制器
Motor Driver Shield
串行 RGB LED
RB - 65PG舵机
IIC 颜色传感器
  • 硬件连接
02S1610210.png
  • 示例程序
#include <Wire.h>
#include <ALS_TCS34725.h>
#include <ChainableLED.h>
#include <Servo.h>
float r, g, b;

Servo myservo; 
#define NUM_LEDS  1
ChainableLED leds(5, 6, NUM_LEDS); //DIN 连接 D6, CIN 连接 D5

ALS_TCS34725 tcs = ALS_TCS34725(TCS34725_INTEGRATIONTIME_24MS, TCS34725_GAIN_1X);
const int interruptPin = 2;
volatile boolean state = false;
int pos = 0; 

//Interrupt Service Routine
void isr() 
{
  state = true;
}

void getRawData_noDelay(uint16_t *r, uint16_t *g, uint16_t *b, uint16_t *c)
{
  *c = tcs.read16(TCS34725_CDATAL);
  *r = tcs.read16(TCS34725_RDATAL);
  *g = tcs.read16(TCS34725_GDATAL);
  *b = tcs.read16(TCS34725_BDATAL);
}


void setup() {
   myservo.attach(10);
   myservo.write(20);
   
   leds.init();
   
  pinMode(interruptPin, INPUT_PULLUP); //TCS interrupt output is Active-LOW and Open-Drain
  attachInterrupt(digitalPinToInterrupt(interruptPin), isr, FALLING);

  Serial.begin(9600);
  
  if (tcs.begin()) {
  } else {
    while (1);
  }
  
  // Set persistence filter to generate an interrupt for every RGB Cycle, regardless of the integration limits
tcs.write8(TCS34725_PERS, TCS34725_PERS_NONE); 
tcs.setInterrupt(true);
  
Serial.flush();
}


void loop() {
  checkcolor();
    for (pos = 20; pos <= 160; pos += 1) { 
    myservo.write(pos);              
    checkcolor();
    for (byte i=0; i<NUM_LEDS; i++)
    leds.setColorRGB(i, (int)r, (int)g, ( int)b); 
   }
   
   for (pos = 160; pos >= 20; pos -= 1) { 
    myservo.write(pos);            
    checkcolor();
    for (byte i=0; i<NUM_LEDS; i++)
 leds.setColorRGB(i, (int)r, (int)g, (int)b); 
  }

}

void checkcolor()
{
  if (state) {
    uint16_t  red, green, blue,clear;
    getRawData_noDelay(&red, &green, &blue,&clear);
    uint32_t sum = clear;
  
  r = red; r /= sum;
  g = green; g /= sum;
  b = blue; b /= sum;
  r *= 256; g *= 256; b *= 256;
 Serial.print("\tR:\t"); Serial.print(r);
 Serial.print("\tG:\t"); Serial.print(g);
 Serial.print("\tB:\t"); Serial.print(b);
 Serial.println();
 Serial.flush();

 tcs.clearInterrupt();
 state = false;
  }
  }
  • 程序效果

颜色传感器识别不同颜色的同时,RGB LED 发出相应颜色的光。

example_Raspberry Pi

  • 使用软件
编程软件:Python 2.7.13
操作系统:Linux raspberrypi 4.14.50
前提:PC 端已通过 SSH 软件登陆到 RaspberryPi 控制器
  • 主要硬件
Raspberry Pi 控制器
Raspberry Pi GPIO 扩展板
16G SD 卡
5V 2.5A 电源适配器
公母头连接线
面包板
TCS34725 颜色传感器
  • 硬件连接
02S16130.png
  • 示例程序

1.使用 FileZilla 软件,将文件夹(TCS34725-RPi)拷贝到树莓派中

02S16132.png

2.进入文件夹 TCS34725-RPi 使用命令
cd TCS34725-RPi
3.安装需要用到的库文件,安装完成后会提示 Finished
sudo python setup.py install
4.打开树莓派的 IIC
sudo raspi-config
选择第五项

02S16133.png

选择I2C

02S16134.png

使用 TAB 键选择 YES,单击回车

02S16135.png

再次单击回车

02S16136.png

使用 TAB 键选择 Finish,然后单击回车,完成开启 IIC 的配置

02S16137.png

执行程序:进入程序目录,可以使用 ls 查看代码是否在当前目录下,使用下列命令执行例程
sudo python simpletest.py

02S16138.png
# Simple demo of reading color data with the TCS34725 sensor.
# Will read the color from the sensor and print it out along with lux and
# color temperature.
# Author: Tony DiCola
# License: Public Domain
import time

# Import the TCS34725 module.
import TCS34725


# Create a TCS34725 instance with default integration time (2.4ms) and gain (4x).
import smbus
tcs = TCS34725.TCS34725()

# You can also override the I2C device address and/or bus with parameters:
#tcs = TCS34725.TCS34725(address=0x30, busnum=2)

# Or you can change the integration time and/or gain:
#tcs = TCS34725.TCS34725(integration_time=TCS34725.TCS34725_INTEGRATIONTIME_700MS,
#                                 gain=TCS34725.TCS34725_GAIN_60X)
# Possible integration time values:
#  - TCS34725_INTEGRATIONTIME_2_4MS  (2.4ms, default)
#  - TCS34725_INTEGRATIONTIME_24MS
#  - TCS34725_INTEGRATIONTIME_50MS
#  - TCS34725_INTEGRATIONTIME_101MS
#  - TCS34725_INTEGRATIONTIME_154MS
#  - TCS34725_INTEGRATIONTIME_700MS
# Possible gain values:
#  - TCS34725_GAIN_1X
#  - TCS34725_GAIN_4X
#  - TCS34725_GAIN_16X
#  - TCS34725_GAIN_60X

# Disable interrupts (can enable them by passing true, see the set_interrupt_limits function too).
tcs.set_interrupt(False)

# Read the R, G, B, C color data.
r, g, b, c = tcs.get_raw_data()

# Calculate color temperature using utility functions.  You might also want to
# check out the colormath library for much more complete/accurate color functions.
color_temp = TCS34725.calculate_color_temperature(r, g, b)

# Calculate lux with another utility function.
lux = TCS34725.calculate_lux(r, g, b)

# Print out the values.
print('Color: red={0} green={1} blue={2} clear={3}'.format(r, g, b, c))

# Print out color temperature.
if color_temp is None:
    print('Too dark to determine color temperature!')
else:
    print('Color Temperature: {0} K'.format(color_temp))

# Print out the lux.
print('Luminosity: {0} lux'.format(lux))

# Enable interrupts and put the chip back to low power sleep/disabled.
tcs.set_interrupt(True)
tcs.disable()
  • 程序效果
02S16131.png

相关资料

Erweima.png
  • IIC 颜色传感器 datasheet & 示例程序

下载链接:https://pan.baidu.com/s/1o2l7MoCGbaR_o64IlEOPLA 密码:311e