HTU21D Maths question

neiltechspec

Senior Member
I'm playing around with one of those HTU21DF sensors.

Reading data ok & masking off the two status bits ok, but I'm having trouble trying to work out the maths for actual RH & actual Temp conversions for an M2 chip.

Basically they are :-

actualRH = -6 + (125.0 * rawRHData / 65536)

&

actualTemp = -46.85 + (175.72 * rawTempData / 65536)

Any help appreciated.

Neil.
 

AllyCat

Senior Member
Hi neil,

I don't know in what form the data comes from the HTU21DF, but the actual maths seems to be quite easy, using the ** operator, which multiplies two (word) variables and then effectively divides by 65536.

So, if rawRHData and rawTempData are simple (unsigned integer) binary words (i.e. a maximum 16 bits, or 65,536) then the following should work:

actualRH = 125 ** rawRHData - 6 (presumably result is in %)

actualTemp = 17572 ** rawTempData - 4685 (result should be in hundredths of a degree)

Either result could potentially go negative, so you might need to trap that and/or display the corresponding negative number.

Cheers, Alan.
 

neiltechspec

Senior Member
Thanks Alan, I'll have a play with that.

The received values from the HTU21DF are simple binary words (with last two bits of LSB masked out which are status bits).
With default settings, RH is a 12 bit word & Temp is a 14 bit word.

I haven't fully digested the datasheet yet, but, RH range is 0% to 100%, Temperature could potentially go negative as range is -40C to +125C.

I'm probably not going to bother with Temp yet anyway but may do in future (currently reading outdoor temp with a DS18B20).

Neil

Neil.
 

neiltechspec

Senior Member
Seems to work, using the following :-

Code:
hi2csetup i2cmaster,$80,i2cslow,i2cbyte  '$80 is HTU21D address

humidity:
	hi2cin $E5,(b1,b0) 'humidity measurement
	
	w0 = w0 & $FFFC
	w1 = 125 ** w0 - 6
	bintoascii w1,b25,b26,b27
	sertxd("Humidity : ",b26,b27," %",cr,lf)
	
	return
	

temperature:
	hi2cin $E3,(b1,b0) 'temperature measurement

	w0 = w0 & $FFFC
	w1 = 17572 ** w0 - 4685
	bintoascii w1,b23,b24,b25,b26,b27
	sertxd("Temp : ",b24,b25,".",b26,b27," ",$b0,"c",cr,lf,cr,lf)
	
	return
Doesn't take negative temperature into account though.

I'm using the Adafruit version which is 5V tolerant for supply & I2C - nice little module although somewhat expensive.

Neil.
 
Last edited:

AllyCat

Senior Member
Hi Neil,

Glad that it works. :) The humidity subroutine potentially could be simplified and refined because 125 ** w0 can never generate a value greater than 125, so you can avoid another word variable and the bintoascii. For example:

Code:
humidity:
	hi2cin $E5,(b1,b0)        'humidity measurement
	w0 = w0 & $FFFC
	b2 = 125 ** w0 [B]MIN 6[/B] - 6       ; (Optional) "MIN 6" prevents an underflow (negative number) if "bad" data.
	sertxd("Humidity : ",#b2," %",cr,lf)
	return
You could also add a "MAX 100" at the end of the b2 calculation to avoid the display of a "silly" number.

Thanks, Alan.
 

neiltechspec

Senior Member
Yep, I get your point.

Will be useful in the final application (just running on the bench on an 08M2 at the mo) where I am running low on variables.

(Barometric Pressure is my next idea for inclusion, using a BMP180).

Neil.
 

Andrew NZ

New Member
This gave me 'near enough'
~ Andrew

Temperature:
hi2cin 0xE3,(b1,b0) 'MSB,LSB 'CMD_READ_TEMP_HOLD = 0xe3
w1 = w0 / 256
w2 = w1 * 176
w3 = w2 / 256
w4 = w3 - 47
'Debug or sertxd (#w0,9,#w1,9,#w2,9,#w3,9,#w4,13,10)

Humidity:
hi2cin 0xE5,(b1,b0) 'MSB,LSB 'CMD_READ_HUM_HOLD = 0xe5
w1 = w0 / 256
w2 = w1 * 125
w3 = w2 / 256
w4 = w3 - 6
'Debug or sertxd (#w0,9,#w1,9,#w2,9,#w3,9,#w4,13,10)
 
Top