# Reading I2C device with HOLD/NO HOLD gives different values?

I have two Python codes for reading a SHT21 temp/hum sensor, but they give different readings! I know the difference seems to be small, but the sensor is located in a place where temperature is very stable, and both of the scripts show the same reading almost all the time (the sensor is not broken). And when I tested this earlier, the difference was for the temperature about 1.1 degree C and for the humidity about 6 % RH. So what does cause this difference?

Here’s one:

`
import smbus
import time

class SHT21:
“”“Class to read temperature and humidity from SHT21.
Ressources:
https://github.com/jaques/sht21_python/blob/master/sht21.py
Martin Steppuhn’s code from http://www.emsystech.de/raspi-sht21"”"

#control constants
_SOFTRESET = 0xFE
_TRIGGER_TEMPERATURE_NO_HOLD = 0xF3
_TRIGGER_HUMIDITY_NO_HOLD = 0xF5

def init(self, bus):
“”“According to the datasheet the soft reset takes less than 15 ms.”""
self.bus = bus
time.sleep(0.015)

“”“Reads the temperature from the sensor. Not that this call blocks
for 250ms to allow the sensor to return the data”""
data = []
time.sleep(0.250)
return self._get_temperature_from_buffer(data)

“”“Reads the humidity from the sensor. Not that this call blocks
for 250ms to allow the sensor to return the data”""
data = []
time.sleep(0.250)
return self._get_humidity_from_buffer(data)

def _get_temperature_from_buffer(self, data):
“”“This function reads the first two bytes of data and
returns the temperature in C by using the following function:
T = =46.82 + (172.72 * (ST/2^16))
where ST is the value from the sensor
“””
unadjusted = (data[0] << 8) + data[1]
unadjusted /= 65536 # divide by 2^16

def _get_humidity_from_buffer(self, data):
“”“This function reads the first two bytes of data and returns
the relative humidity in percent by using the following function:
RH = -6 + (125 * (SRH / 2 ^16))
where SRH is the value read from the sensor
“””
unadjusted = (data[0] << 8) + data[1]
unadjusted /= 1 << 16 # divide by 2^16

def close(self):
“”“Closes the i2c connection”""
self.bus.close()

def enter(self):
“”“used to enable python’s with statement support”""
return self

def exit(self, type, value, traceback):
“”“with support”""
self.close()

def crc8check(self, value):
remainder = ( ( value[0] << 8 ) + value[1] ) << 8
remainder |= value[2]

# divsor = 0x988000 is the 0x0131 polynomial shifted to farthest left of three bytes

divsor = 0x988000

for i in range(0, 16):
if( remainder & 1 << (23 - i) ):
remainder ^= divsor
divsor = divsor >> 1

if remainder == 0:
return True
else:
return False

if name == “main”:
try:
bus = smbus.SMBus(1)
with SHT21(bus) as sht21:

`print sht21.read_temperature()`

except IOError, e:
print e
print ‘Error creating connection to i2c.’

`

That gives me just now these values:
49.1910400391
5.99255615234

The second:

`
import time
import math

class HTU21D():

# Commands

TRIGGER_TEMP_MEASURE_HOLD = 0xE3
TRIGGER_HUMD_MEASURE_HOLD = 0xE5
WRITE_USER_REG = 0xE6

# Constructor

def init(self):

def writeUserRegister(self, value):
self.i2c.write8(self.WRITE_USER_REG, value)

return value

# return -255

rawTempData = ( value[0] << 8 ) + value[1]

# Zero out the status bits but keep them in place

#rawTempData = rawTempData & 0xFFFC;

# Calculate the actual temperature

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

return actualTemp

# value[2]: CRC

rawRHData = ( value[0] << 8 ) + value[1]

# Zero out the status bits but keep them in place

rawRHData = rawRHData & 0xFFFC;

# Calculate the actual RH

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

return actualRH

sensor = HTU21D()

print a
print b

`

And this prints:
49.549621582
5.45361999512