I2C Colour Sensor

08 Oct 2012

Has anyone used this colour sensor http://littlebirdelectronics.com/products/color-light-sensor-evaluation-board-1 or atleast something simmilar? If so could you please show me some of your code/code snippet that was used to do this.

10 Oct 2012

Anyone?

11 Oct 2012

Hi I'm just responding as I know what it's like when nothing happens.....

Sorry I haven't used this part, but it looks interesting. Having looked at your link there are some pretty good example files available on the page, have you tried developing your code from these sequences?

Looking at the comments https://www.sparkfun.com/products/10701 seems to indicate a few issues users are having!

What stage are you actually at or just wanting to see if anyone else have tested it?

This is quite a good document as well.

http://www.avagotech.com/docs/AV02-0359EN

Sorry not to be of more help

14 Oct 2012

Well I have written code to print data at 0x74 to terminal (comes up as 0.0 no matter what) and used the example I2C code and changed the address (results in static number).

14 Oct 2012

Hi Adin, I can only suggest you post your code (just the minimum to show the issue) and you'll probably get some of the advanced coders look over it, even if they don't have experience of the specific part.

Just having a quick look at the pdf link I attached to previous post it states

Quote:

The 7-bit slave device address is 74H or decimal 116. Thus, the device address for writing is E8H and for reading is E9H.

I also note it staes the sensor has to be calibrated before use

Quote:

"Programming Before the ADJD-S311/S371 can be used, a one time sensor gain optimization routine is done so that the digital values ranges from 0 to 1000."

Have you done this?

14 Oct 2012

Considering this sensor apparently has no WHO_AM_I register you cannot check that, however to see if you can talk with the sensor you can at least still check if he ACKs his address. As Kevin stated it should be 0xE8 since mbed uses 8-bit addresses. Following code should for example tell you if there is contact with the sensor:

bool testConnection( void )
{
    if (_i2c.write(0xE8, NULL, 0) == 0 )
        return true;
    else
        return false;
}

(Where _i2c must be replaced by whatever you use as name for your i2c connection).

14 Oct 2012

I used the following code and recieved CONTACT as a response (equal to BOOL == 1 in your statement) and 0.0 at address 0xE9:

#include "mbed.h"

I2C i2c(p28, p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx

const int addr = 0xE9; // define the I2C Address

int main()
{
    while(1) {
            if (i2c.write(0xE8, NULL, 0) == 0 )
            pc.printf("CONTACT");
            else
            pc.printf("NO CONTACT");
        i2c.read(addr);
        // print the ranging data to the screen
        pc.printf("Colour Value = %.2f/n");
        i2c.read(0x74);
    }
}
14 Oct 2012

So communication is okay, good to know.

I2C read doesnt work that way. For some reason the acknowledge setting of a read command is an int and not a boolean, so it does not complain about your code, but that way you read a single byte writting on i2c bus, while sending an ACK back. (see: http://mbed.org/projects/libraries/api/mbed/trunk/I2C#I2C.read)

I havent checked this sensor out, but generally you need to write the register address you want to read to the device, then you transmit a restart, and then you send the device address again in read mode. In mbed code this is done by:

char read_i2c(char address)
{
    char retval;
    i2c.write(0xE8, &address, 1, true);     //Write the register address you want to read to your sensor, do not transmit stop condition in end
    i2c.read(MMA7660_ADDRESS, &retval, 1);  //Open device in read mode, read 1 byte. If you need more you can simply read more bytes, normally address pointer auto increments
    return retval;
}

Then your printf needs to have included which value you want to output. So for example:

address = 0x74;
pc.printf("Colour Value = %d/n",read_i2c(address));

Aditionally quite many sensors wont start automatically until you specifically put them in a measurement mode.

Edit: Had a look at its datasheet, and looks you have to do the following, do take into account it was only a quick look, so might be wrong: Write 0x01 to register 0x00 to start a measurement. Wait until this register is cleared again, which indicates measurement is complete. Read the result.

So in code it would be something like:

//Write function, read function is already shown above
void write_i2c(char address, char data)
{
    char temp[2];
    temp[0]=address;
    temp[1]=data;

    i2c.write(0xE8, temp, 2);
}

int main() {
  while(1) {
    address=0x00;
    write_i2c(address, 0x01);  //Set control register to do measurement
    while(read_i2c(address)==0x01);  //Wait as long as control register has value 0x01
    
    address=0x74;
    //When code gets here the control register is cleared and you can read the data
    printf("Your data: %d \n", read_i2c(address));
   }
}

(Note: I just wrote this code in the forums, so probably there will be syntax errors)

17 Nov 2012

Sorry for quite a late reply, but unfortunately (bear in mind I don't have much experience with this) using the following code I still Get 0 as a response:

#include "mbed.h"
#include "m3pi.h"
I2C i2c(p28, p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx
m3pi m3pi;
//#define Colour
char read_i2c(char address) 
{
    char retval;
    i2c.write(0xE8, &address, 1, true);     //Write the register address you want to read to your sensor, do not transmit stop condition in end
    i2c.read(address, &retval, 1);  //Open device in read mode, read 1 byte. If you need more you can simply read more bytes, normally address pointer auto increments
    return retval;
}

//Write function, read function is already shown above
void write_i2c(char address, char data)
{
    char temp[2];
    temp[0]=address;
    temp[1]=data;

    i2c.write(0xE8, temp, 2);
}

int main()
{
    while(1) {
        char address=0x00;
        write_i2c(address, 0x01);  //Set control register to do measurement
        while(read_i2c(address)==0x01);  //Wait as long as control register has value 0x01

        address=0x74;
        //When code gets here the control register is cleared and you can read the data
        pc.printf("Colour Value: %d \n", read_i2c(address));
        m3pi.printf("Colour Value: %d \n", read_i2c(address));
        wait (1);
    }
}
18 Nov 2012

There is a mistake is your read_i2c() function. The i2c.read() should use the slave address as its first parameter, rather than the register address that you are using:

Current code:

char read_i2c(char address) 
{
    char retval;
    i2c.write(0xE8, &address, 1, true);     //Write the register address you want to read to your sensor, do not transmit stop condition in end
    i2c.read(address, &retval, 1);  //Open device in read mode, read 1 byte. If you need more you can simply read more bytes, normally address pointer auto increments
    return retval;
}

should be:

char read_i2c(char address) 
{
    char retval;
    i2c.write(0xE8, &address, 1, true);     //Write the register address you want to read to your sensor, do not transmit stop condition in end
    i2c.read(0xE8, &retval, 1);  //Open device in read mode, read 1 byte. If you need more you can simply read more bytes, normally address pointer auto increments
    return retval;
}

The i2c.write() will set the register address by using the first value following the slaveaddress. Subsequent writes or reads will then start at that register address and auto-increment the registeraddress after every new read or write.

23 Nov 2012

Even with that correct I still get 0.

23 Nov 2012

Just checked the datasheet, but 0x74 is no register, there is nothing there (it is the 7-bit I2C address, but not an internal register). The measurement done stores the data in registers 64-71 (decimal), so read those instead of 0x74.

20 Mar 2013

hello, did you get this thing working? i can access the registers and calibrate the thing, but if i opposite it to a color on my lcd screen or something else, the colors he reads are changing all the time. Sometimes color data red is 250 sometimes its 10, for the same screen. Anybody had the sameprobleme? thnx in advance

24 Sep 2014

Thanks for sharing! I came accross this website to read more info on this great product: http://www.directindustry.com/industrial-manufacturer/color-sensor-64641.html. Hope you enjoy it!