ADJD-S311 I2C Color Sensor

ADJD-S311-CR999 I2C Color Sensor

This page presents a library to use an I2C color light sensor.

/media/uploads/Jiaqi/10701-01.jpg

The Avago ADJD-S311-CR999 is a 4 channel digital color light sensor with 10-bit per channel resolution. The module's features include:

  • 10 bit per channel resolution
  • Independent gain selection for each channel
  • Wide sensitivity
  • Two wire serial communication
  • Built in oscillator/selectable external clock
  • Low power mode (sleep mode)

/media/uploads/Jiaqi/08618-01.jpg

The Avago ADJD-S311 is a I2C which connects to mbed through SDA(data) and SCL(clock) lines. The ADJD-S311 is composed of four different color sensors: red, green, blue, and clear. The clear sensor is used for detecting and sensing brightness. ADJD-S311 reads in data from each sensor individually.

There are two system of gains for the sensor: the number of capacitor (ranging from 0 to 15 with lower value for higher sensitivity), and number of integration time slot (ranging from 0 to 4095 with higher value for higher sensitivity). The gain can be set manually if absolute color info was to obtained or set by the calibration member function which tries to get a relatively high sensitivity from the current lighting condition.

The sensor can be used to measure reflected color or projected color. To measure the reflected color, turn on the onboard LED and put the object 1-2mm from the sensor so it can collect the reflected light. And for projected color, try to minimize the influence from ambient lighting source.

For more information on the I2C color light sensor, please refer to Avago ADJD-S311-CR999 and Color Sensor Evaluation Board

Pinout and Wiring

/media/uploads/Jiaqi/10701-04.jpg

Below is the wiring for the demo program:

mbedADJD S311
3.3V=VOUT3.3V
GNDGND
P22LED
P27SCL
P28SDA

/media/uploads/Jiaqi/img_20140323_163954_1_2.jpg

Hello World Example

Import programADJD-S311_HelloWorld

A simple hello_world program for the ADJD-S311 color sensor

main.cpp

#include "mbed.h"
#include "ADJDs311.h"

/**
 * An example program using the ADJD-S311 color sensor to obtain the reflexive
 * color of objects.
 */
 
ADJDs311 colorSensor(p28, p27, p23);
Serial pc(USBTX, USBRX);

int main() {
RGBC color;
RGBC cap;
RGBC inte;
RGBC offset;
    
    
    colorSensor.ledMode(true);  // turn on the on board led
    pc.printf("Calibartion started.\n\rPress any key to continue...\n\r");
    
    while(!pc.readable());     // waiting for the setup of calibration (the brightest condition to be measued)
   
    colorSensor.calibrate();   // calibrate the sensor to get the optimised gain

   while(pc.readable())         // discard extra char input
    {
        pc.getc();
    }
   
    pc.printf("Getting offset\n\rPress any key to continue...\n\r");
     while(!pc.readable());     // waiting for the set up of calibration (the brightest condition to be measued)
    
    offset = colorSensor.setOffset();   // get and set the color offset to compensate on board LED color
    
    while(pc.readable())        // discard extra char input
    {
        pc.getc();
    }
    
    cap = colorSensor.getColorCap();    // get the gain of the capacitors
    inte = colorSensor.getColorInt();   // get the gain of integration time slot
    pc.printf("\n\n\n\rCalibartion completed.\n\r");
    pc.printf("\nCaps:\t\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", cap.red, cap.green, cap.blue, cap.clear);
    pc.printf("Ints:\t\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", inte.red, inte.green, inte.blue, inte.clear);
   pc.printf("Offsets:\tr: %4d,\tg: %4d,\tb: %4d,\tc: %4d\n\r", offset.red, offset.green, offset.blue, offset.clear);
    pc.printf("\nPress any key to continue\n\r");
    while(!pc.readable());  // waiting calibration and offset info confirmation
    while(pc.readable())    // discard extra char input
    {
        pc.getc();      
    }
    colorSensor.ledMode(true);  // node that wetOffset will turn off the on board LED automatically. Need to turn it on again
    while(1) {
        color = colorSensor.read();     // get the rgb and clear data from sensor
        pc.printf("red: %4d,\tgreen: %4d,\tblue: %4d,\tclear: %4d\r", color.red, color.green, color.blue, color.clear);
        wait(0.15);
    }    
}

Demo Video

ADJD-S311-CR999 mbed Library

You can import the ADJD-S311 mbed Library from here :

Import libraryADJDs311

A library for the Avago ADJD-S311-CR999 Color Light Sensor

API

Import library

Public Member Functions

ADJDs311 (PinName sda, PinName scl, PinName led)
Create a color sensor interface.
void calibrate ()
Calibrate the capacitance and integration time slot so that the current readings are as close to 1000 as possible and the difference between RGB readings are as small as possible.
void ledMode (bool ledOn)
Turn the on board LED on/off.
RGBC getOffset ()
Get the current offset stored in offset registers.
RGBC setOffset (bool useOffset=true)
Use the current light condition to set the offset.
void offsetMode (bool useOffset)
Use the offset registers to automatically subtract offset from the readings.
RGBC read ()
Read in the color value from the sensor.
RGBC getColorCap ()
Get the gain of number of capacitor for each channel, in the range of 0 to 15.
RGBC getColorInt ()
Get the gain of number of integration time slot, in the range of 0 to 4095.
void setColorCap (int red, int green, int blue, int clear)
Set the gain of number of capacitor for each channel, in the range of 0 to 15.
void setColorInt (int red, int green, int blue, int clear)
Set the gain of number of integration time slot, in the range of 0 to 4095.

References


Please log in to post comments.