Error Writing 16 bits to I2C register: Code for HDC1008/HDC1000 humidity sensor from TI

Hi,

I am trying to interface HDC1008 with imp002. For making a reading , I have to write 0x0000 at the address 0x02 and then read the humidity from address 0x01. While it works on Arduino and WICED, it throws an I2C read error -6 with imp. Any suggestions on how to resolve this error will be helpful! I have added the code and datasheet.

`

//initialize I2C

hardware.i2c12.configure(CLOCK_SPEED_100_KHZ);

//read humidity sensor
function read_humidity()
{
local bytes=hardware.i2c12.read(0x82, “\x01”, 2);
local z=hardware.i2c12.readerror();
server.log(z); //prints -6 on running this code
server.log(bytes);
if (typeof(bytes)!= “string”)
{
//server.log("i2c read error: "+bytes);
return;
}
local data_low = bytes[0];
data_low = data_low << 8;
local data_high = bytes[1];

local humidity= data_low+data_high;
 
humidity=humidity/65536;
server.log("Humidity:"+humidity);
return humidity;

}

//initialize humidity sensor
function humidity_init()
{
local y;

y=hardware.i2c12.write(0x82, "\\x02"+ "\\x00"+"\\x00");  
server.log(y); //prints 0
y=hardware.i2c12.write(0x82, "\\x01");
server.log(y); //prints 0
imp.wakeup(1, read_humidity);

}
humidity_init();

`

From what I have understood while debugging, the 16 bit write operation is not happening properly. This operation configures the chip for the subsequent read operation.

What’s the intention of the second write() in humidity_init()?

I’m a newbie at i2c, but shouldn’t the base address be 0x80 based upon the documentation?

I think it depends on how he has it the chip’s two address select pins wired up, @hvacspei, but changing the address to 0x80 is certainly something to try, I’d say.

Hi,

Its wired in such a way that the address becomes 0x82. The following code worked out:

`

//initialize I2C
hardware.i2c12.configure(CLOCK_SPEED_100_KHZ);

//read humidity sensor
function read_humidity()
{
hardware.i2c12.write(0x82, “\x01”);
imp.sleep(1);
local bytes=hardware.i2c12.read(0x82, “”, 2);
server.log(bytes);
if (typeof(bytes)!= “string”)
{
//server.log("i2c read error: "+bytes);
return;
}
local data_low = bytes[0];
data_low = data_low << 8;
local data_high = bytes[1];
local humidity= data_low+data_high;
humidity=humidity/655.36;
server.log(“Humidity:”+humidity);
imp.wakeup(1, read_humidity);
return humidity;
}

//initialize humidity sensor
function sensor_init()
{

hardware.i2c12.write(0x82, "");
local y;
y=hardware.i2c12.write(0x82, "\\x02"+"\\x00"+"\\x00");
server.log(y);

local bytes=hardware.i2c12.read(0x82, "\\xFC", 2);
server.log(bytes);

imp.sleep(2);

}
sensor_init();
read_humidity();

`

Ah, the datasheet says that after writing the address pointer you must wait the conversion time (or wait for DRDY).

You should be able to change your imp.sleep(1) to imp.sleep(0.00635) - table 7.5 says 6.35ms is the max time for a 14-bit conversion.

Yes a delay of 6.35ms works. Thanks for the suggestion!