Page 1 of 1

Trouble using I2C to read light sensor (TSL4531)

Posted: Fri Nov 17, 2017 10:25 pm
by physiii
I'm trying to read data from an ambient light senor. I started with the I2C esp-idf example which I used to read data from two other sensors on the same board. The datasheet seems to follow the same procedure but I'm still reading only 0x00.

write operation

Code: Select all

    i2c_cmd_handle_t cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, 0x29 << 1 | WRITE_BIT, ACK_CHECK_EN);
    i2c_master_write_byte(cmd, 0x04, ACK_CHECK_EN);
    i2c_master_stop(cmd);

    int ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    if (ret == ESP_FAIL) {
        return ret;
    }
IMG_20171117_161515.jpg
IMG_20171117_161515.jpg (995.95 KiB) Viewed 4388 times
read operation

Code: Select all

    vTaskDelay(1000 / portTICK_RATE_MS);
    cmd = i2c_cmd_link_create();
    i2c_master_start(cmd);
    i2c_master_write_byte(cmd, 0x29 << 1 | READ_BIT, ACK_CHECK_EN);
    i2c_master_read_byte(cmd, data_h, ACK_VAL);
    i2c_master_read_byte(cmd, data_l, NACK_VAL);
    i2c_master_stop(cmd);

    ret = i2c_master_cmd_begin(i2c_num, cmd, 1000 / portTICK_RATE_MS);
    i2c_cmd_link_delete(cmd);
    if (ret == ESP_FAIL) {
        return ESP_FAIL;
    }
    
IMG_20171117_161534.jpg
IMG_20171117_161534.jpg (939.1 KiB) Viewed 4388 times
Any suggestions?

Re: Trouble using I2C to read light sensor (TSL4531)

Posted: Sat Nov 18, 2017 1:18 pm
by ESP_Sprite
If I understand the sensors datasheet correctly, all you're doing is reading out the data register, without powering the device on or starting a measurement cycle; my guess is that is the reason why the data you read is 0. Suggest you set register 0 and 1 in your light sensor to some sane values first before trying to get the resulting data.

Re: Trouble using I2C to read light sensor (TSL4531)

Posted: Mon Nov 20, 2017 12:41 pm
by physiii
ESP_Sprite wrote:If I understand the sensors datasheet correctly, all you're doing is reading out the data register, without powering the device on or starting a measurement cycle; my guess is that is the reason why the data you read is 0. Suggest you set register 0 and 1 in your light sensor to some sane values first before trying to get the resulting data.
Appreciate the advice, I finally figured it out!

All I needed was to set the write bit (7) in the command code:

Code: Select all

    i2c_master_write_byte(cmd, 0x04 | 0x80, ACK_CHECK_EN);
Definitely something I will be looking out for for now on.