Grove - I2C Color Sensor

From Wiki
Jump to: navigation, search

Introduction

This module is based on the color sensor TCS3414CS with digital output I2C. Based on the 8*2 array of filtered photodiodes and 16-bit analog-to-digital converters, you can measure the color chromaticity of ambient light or the color of objects. Of the 16 photodiodes, 4 have red filters, 4 have green filters, 4 have blue filters and 4 have no filter(clear). With the synchronization input pin, an external pulsed light source can provide precise synchronous conversion control. Previous Version:V0.9b


400px-I2C Color Sensor.jpg


Specification

PCB Size :2.0cm*4.0cm Interface :2.0mm pitch pin header IO Structure :SCL,SDA,VCC,GND ROHS :YES VCC :3.3 ~6.0 V Operating Free-air Temperature :-40 ~ 85 °C


Demonstration

This module can be used to detect the color of light source or the color of objects. When used to detect the color of the light source, the led switch should be turned off, and the light source should shine the sensor directly. When used to detect the color of things, the led should be on and you should put the object on the top of the enclosure closely. The theory of sensing the color of objects is Reflective Sensing Theory. Like the picture below.


400px-Reflcect.jpg



The following sketch demonstrates a simple application of sensing the color of the paper.

Connect the Color Sensor to www.simgin-chip.com through the Grove-Base Shield using the I²C interface. Connect the www.simgin-chip.com to PC via a USB cable.

Copy and paste code below to a new Arduino sketch. /******************************************************************************/

  1. include <Wire.h>
  2. include <math.h>
  3. define COLOR_SENSOR_ADDR 0x39//the I2C address for the color sensor
  4. define REG_CTL 0x80
  5. define REG_TIMING 0x81
  6. define REG_INT 0x82
  7. define REG_INT_SOURCE 0x83
  8. define REG_ID 0x84
  9. define REG_GAIN 0x87
  10. define REG_LOW_THRESH_LOW_BYTE 0x88
  11. define REG_LOW_THRESH_HIGH_BYTE 0x89
  12. define REG_HIGH_THRESH_LOW_BYTE 0x8A
  13. define REG_HIGH_THRESH_HIGH_BYTE 0x8B
  14. define REG_BLOCK_READ 0xCF
  15. define REG_GREEN_LOW 0xD0
  16. define REG_GREEN_HIGH 0xD1
  17. define REG_RED_LOW 0xD2
  18. define REG_RED_HIGH 0xD3
  19. define REG_BLUE_LOW 0xD4
  20. define REG_BLUE_HIGH 0xD5
  21. define REG_CLEAR_LOW 0xD6
  22. define REG_CLEAR_HIGH 0xD7
  23. define CTL_DAT_INIITIATE 0x03
  24. define CLR_INT 0xE0

//Timing Register

  1. define SYNC_EDGE 0x40
  2. define INTEG_MODE_FREE 0x00
  3. define INTEG_MODE_MANUAL 0x10
  4. define INTEG_MODE_SYN_SINGLE 0x20
  5. define INTEG_MODE_SYN_MULTI 0x30
  1. define INTEG_PARAM_PULSE_COUNT1 0x00
  2. define INTEG_PARAM_PULSE_COUNT2 0x01
  3. define INTEG_PARAM_PULSE_COUNT4 0x02
  4. define INTEG_PARAM_PULSE_COUNT8 0x03

//Interrupt Control Register

  1. define INTR_STOP 40
  2. define INTR_DISABLE 0x00
  3. define INTR_LEVEL 0x10
  4. define INTR_PERSIST_EVERY 0x00
  5. define INTR_PERSIST_SINGLE 0x01

//Interrupt Souce Register

  1. define INT_SOURCE_GREEN 0x00
  2. define INT_SOURCE_RED 0x01
  3. define INT_SOURCE_BLUE 0x10
  4. define INT_SOURCE_CLEAR 0x03

//Gain Register

  1. define GAIN_1 0x00
  2. define GAIN_4 0x10
  3. define GAIN_16 0x20
  4. define GANI_64 0x30
  5. define PRESCALER_1 0x00
  6. define PRESCALER_2 0x01
  7. define PRESCALER_4 0x02
  8. define PRESCALER_8 0x03
  9. define PRESCALER_16 0x04
  10. define PRESCALER_32 0x05
  11. define PRESCALER_64 0x06

int readingdata[20]; int i,green,red,blue,clr,ctl; double X,Y,Z,x,y,z; void setup() { Serial.begin(9600); Wire.begin(); // join i2c bus (address optional for master) } void loop() {

 setTimingReg(INTEG_MODE_FREE);//Set trigger mode.Including free mode,manually mode,single synchronizition mode or so.
 setInterruptSourceReg(INT_SOURCE_GREEN); //Set interrupt source 
 setInterruptControlReg(INTR_LEVEL|INTR_PERSIST_EVERY);//Set interrupt mode
 setGain(GAIN_1|PRESCALER_4);//Set gain value and prescaler value
 setEnableADC();//Start ADC of the color sensor
 while(1)
{ 
  readRGB();
  calculateCoordinate();
  delay(1000);
  clearInterrupt();  
}

} /************************************/ void setTimingReg(int x) {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_TIMING);
  Wire.write(x);
  Wire.endTransmission();  
  delay(100); 

} void setInterruptSourceReg(int x) {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_INT_SOURCE);
  Wire.write(x);
  Wire.endTransmission();  
  delay(100);

} void setInterruptControlReg(int x) {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_INT);
  Wire.write(x);
  Wire.endTransmission();  
  delay(100);

} void setGain(int x) {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_GAIN);
  Wire.write(x);
  Wire.endTransmission();

} void setEnableADC() {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_CTL);
  Wire.write(CTL_DAT_INIITIATE);
  Wire.endTransmission();  
  delay(100);  

} void clearInterrupt() {

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(CLR_INT);
  Wire.endTransmission(); 

} void readRGB() {

 Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.write(REG_BLOCK_READ);
  Wire.endTransmission();

  Wire.beginTransmission(COLOR_SENSOR_ADDR);
  Wire.requestFrom(COLOR_SENSOR_ADDR,8);
  delay(500);
  if(8<= Wire.available())    // if two bytes were received 
 { 
   for(i=0;i<8;i++)
   {
     readingdata[i]=Wire.read();
     //Serial.println(readingdata[i],BIN);
    }
 }
 green=readingdata[1]*256+readingdata[0];
 red=readingdata[3]*256+readingdata[2];
 blue=readingdata[5]*256+readingdata[4];
 clr=readingdata[7]*256+readingdata[6];
 Serial.println("The RGB value and Clear channel value are");
 Serial.println(red,DEC);
 Serial.println(green,DEC);
 Serial.println(blue,DEC);
 Serial.println(clr,DEC);  

} void calculateCoordinate() {

 X=(-0.14282)*red+(1.54924)*green+(-0.95641)*blue;
 Y=(-0.32466)*red+(1.57837)*green+(-0.73191)*blue;
 Z=(-0.68202)*red+(0.77073)*green+(0.56332)*blue;
 x=X/(X+Y+Z);
 y=Y/(X+Y+Z);
 if((X>0)&&(Y>0)&&(Z>0))
 {
   Serial.println("The x,y value is");

Serial.print("(");

   Serial.print(x,2);

Serial.print(" , ");

   Serial.print(y,2);

Serial.println(")"); Serial.println("Please reference the figure(Chromaticity Diagram) in the wiki "); Serial.println("so as to get the recommended color.");

 }
else
Serial.println("Error,the value overflow");

} Upload the code. Open the Serial Monitor, you will see the output result of Color Sensor as show below:

I2C Color Sensor Result.jpg When we get coordinates (x, y), please reference the below figure so as to get the recommended color.


Chromaticity Diagram.jpg


400px-Coordinates transform.png



400px-Equations.png