Digital color sensor module S11059-02DT

The sensor we are going to use detects primary colors (Red, Blue, Green) and Infrared light waves.

Each color has a 16-bit output. 
It operates at low voltage (3V3).

Two-step sensitivity switching function (sensitivity ratio: 1/10).

What do we need

  • an Arduino Yun board
  • a Digital Color Sensor Module (S11059-02DT)
  • a breadboard
  • a bunch of wires

The circuit

Image  

Be sure to follow the connections shown in the fritzing circuit  and in the next table :

Sensor (GND)     => Arduino GND pin
Sensor (SCL)     => Arduino PIN 3
Sensor (SDA)     => Arduino PIN 2
Sensor (Vcc)     => Arduino Vcc pin

The code

#include "Wire.h"

#define device_address 0x2A
#define control_reg 0x00
#define timing_reg_H  0x01
#define timing_reg_L 0x02

uint16_t red = 0;
uint16_t green = 0;
uint16_t blue = 0;
uint16_t IR = 0;

void getRGB();

void setup() {
  Wire.begin();
  Serial1.begin(230400); 
}

void loop() {
  getRGB();
  Serial1.print("red: "),
  Serial1.print(red);
  Serial1.print(", green: ");
  Serial1.print(green);
  Serial1.print(", blue: "),
  Serial1.print(blue);
  Serial1.print(", IR: "),
  Serial1.println(IR);
  delay(500);
}

void getRGB() {
  uint16_t colordata= 0;
  
  Wire.beginTransmission(device_address);
  Wire.write(control_reg);
  Wire.write(0x83);
  Wire.endTransmission(false);
  Wire.beginTransmission(device_address);
  Wire.write(control_reg);
  Wire.write(0x03);
  Wire.endTransmission(true);

  delay(180*4);

  Wire.beginTransmission(device_address);
  Wire.write(0x03);
  Wire.endTransmission(false);
  Wire.requestFrom(0x2A,8,true);
  
  //read data
  colordata = Wire.read();
  colordata <<= 8;
  colordata |= Wire.read();
  red = colordata;
  colordata =0;
  colordata = Wire.read();
  colordata <<= 8;
  colordata |= Wire.read();
  green = colordata;
  colordata =0;
  colordata = Wire.read();
  colordata <<= 8;
  colordata |= Wire.read();
  blue = colordata;
  colordata =0;
  colordata = Wire.read();
  colordata <<= 8;
  colordata |= Wire.read();
  IR = colordata;

  return;
}

void setManualtGain(uint16_t ex_time) {
  uint8_t reg =0;
  Wire.beginTransmission(device_address);
  reg =(ex_time >>= 8);
  Wire.write(reg);
  Wire.endTransmission(false);
  Wire.beginTransmission(device_address);
  reg |= ex_time;
  Wire.write(reg);
  Wire.endTransmission(true);
}
Write or paste code in Arduino IDE, then load the sketch on Yun board.

Python Serial Test

Read data from shell. 
To do this we used the included library “pyserial”. You must create a python script to be launched from a terminal. Then connect to your board with ssh, create file serialTest.py with the following code and start it with the command  “python serialTest.py”. Read data now.
import serial
try:
     print "Trying..."
     arduino = serial.Serial('/dev/ttyATH0',230400)
except:
     print "Failed to connect on"
while True:
     x=arduino.readline()
     print x

Referral link: 

Digital Color Sensor Module S11059-02DT
Example code referral