Trouble using I2C to read light sensor (TSL4531)

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

Trouble using I2C to read light sensor (TSL4531)

Postby physiii » Fri Nov 17, 2017 10:25 pm

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 4387 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 4387 times
Any suggestions?

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

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

Postby ESP_Sprite » Sat Nov 18, 2017 1:18 pm

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.

physiii
Posts: 23
Joined: Fri Nov 17, 2017 9:37 pm

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

Postby physiii » Mon Nov 20, 2017 12:41 pm

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.

Who is online

Users browsing this forum: Bing [Bot] and 88 guests