HTU21D Null Read Out Values

The following piece of code give this output:

2014-02-12 16:04:00 UTC-6: [Device] (null : 0x0) Temp
2014-02-12 16:04:01 UTC-6: [Device] (null : 0x0) Hum

`function readTandH(){
local alsAddr = 0x40;
hardware.i2c89.write(alsAddr<<1,"\xf3");
//imp.sleep(0.07);
local temp = hardware.i2c89.read(alsAddr<<1, “”, 3);
hardware.i2c89.write(alsAddr<<1,"\xf5");
//imp.sleep(60);
local hum = hardware.i2c89.read(alsAddr<<1, “”, 3);
server.log(temp + " Temp");
server.log(hum + " Hum");
}

readTandH();`

I am stumped. Any help would be greatly appreciated.

The following file is the code without the breaks caused by coding in html

You need to call i2c.configure first to enable the port.

Thank you for that. I attached my current answer. However just changing that didn’t seem to make a difference. I have however allowed it to sleep in between reading and writing and that gives me weird out put like:

2014-02-12 19:42:33 UTC-6: [Status] Downloading new code; 2.45% program storage used
2014-02-12 19:42:53 UTC-6: [Device] ZUTbIFRlbXA=
2014-02-12 19:42:53 UTC-6: [Device] H8LSIEh1bQ==
2014-02-12 19:43:23 UTC-6: [Device] e8^ Temp
2014-02-12 19:43:23 UTC-6: [Device] HnKZIEh1bQ==
2014-02-12 19:43:53 UTC-6: [Device] e( Temp
2014-02-12 19:43:53 UTC-6: [Device] HvpaIEh1bQ==

You’re logging the raw bytes there. You might want to make them printable:

server.log(format("Temp read: %02x %02x %02x", temp[0], temp[1], temp[2]));

Thank you. That works great of course :slight_smile: I will post the code when I’m done figuring out what to do with that formula.

how accurate is the HTU21D temp?

Good are you using the sparkfun breakout?
I’ve used several for over a year and
sparkfun uses D3204
+/-3%RH tolerance @55%RH
Temperature Accuracy @25°C typ ±0.3 °C

I am using the sparkfun breakout for the sensor with the april breakout too. Thanks for updating that link. You are faster than me.

Are what I meant was one of these
https://www.sparkfun.com/products/12064

@controlCloud you mean me… right? i have the D3204 laying around and will try it. My NTC and LM35 isn’t good enough. I got a lot of different readings although i used averaging.

@Chrischi yes but I’m surprised by LM35 performance as that’s 0.5C accuracy and I used the NTC probe (http://www.instructables.com/id/TempBug-internet-connected-thermometer/) it was stable some folk have seen issues with wifi being on and causing noise analog readings.

The link is my finished product with pictures and code. Thanks for the help.

is there a nice/good way to put the HTU21 in a case?
It’s what i wonder about every temp sensor, how to put it in a case and still get
the right temperature.

any good ideas/informations?

I suppose that every case will get the same temperature as it’s surrounding environment.

Here’s the code I put together for this:
`
class HTU21D {
_i2c = null;
static ALSADDR = 0x40;
static READTEMP = “\xf3”;
static READHUMID = “\xf5”;
constructor(i2c) {
_i2c = i2c;
_i2c.configure(CLOCK_SPEED_400_KHZ);
}
function GetTemp() {
_i2c.write(ALSADDR<<1, READTEMP);
imp.sleep(0.05);
local a = _i2c.read(ALSADDR<<1, “”, 3);
return (((a[0] << 8) | (a[1] & 0xfc)) * 175.72 / 65536.0) - 46.85
}
function GetHumid() {
_i2c.write(ALSADDR<<1, READHUMID);
imp.sleep(0.05);
local b = _i2c.read(ALSADDR<<1, “”, 3);
return (((b[0] << 8) | (b[1] & 0xf0)) * 120.00 / 65536.0) - 6.00;
}
}

tempSensor <- HTU21D(hardware.i2c89); // init the device
server.log(tempSensor.GetTemp());
server.log(tempSensor.GetHumid());
`

Thanks for the lib @boffin! Works perfect.

For the record the code that boffin wrote is very close to mine. I appreciate the look at other people’s code. The hardest part of some of these project is getting the wires correctly placed or making it look “pretty.” Boffin I’m hoping maybe you have some pictures of what you did??

@boffin - can we pull this code into our Reference Library?

@Boffin - I’m using your code and am getting an error: " ERROR: the index ‘0’ does not exist"

I believe I’ve connected the HTU21D correctly and so am not sure why I’m getting this, but is it telling me there’s an issue with the code or that I’m not getting data from my HTU21D? Any help is appreciated. Thanks!