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: 
      
http://www.sensirion.com/fileadmin/user_upload/customers/sensirion/Dokumente/Humidity/Sensirion_Humidity_SHT21_Datasheet_V3.pdf
      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
    _I2C_ADDRESS = 0x40
    _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
        self.bus.write_byte(self._I2C_ADDRESS, self._SOFTRESET)
        time.sleep(0.015)

    def read_temperature(self):    
        """Reads the temperature from the sensor.  Not that this call blocks
    for 250ms to allow the sensor to return the data"""
        data = []
        self.bus.write_byte(self._I2C_ADDRESS, self.
_TRIGGER_TEMPERATURE_NO_HOLD)
        time.sleep(0.250)
        data.append(self.bus.read_byte(self._I2C_ADDRESS))
        data.append(self.bus.read_byte(self._I2C_ADDRESS))
        return self._get_temperature_from_buffer(data)
        

    def read_humidity(self):    
        """Reads the humidity from the sensor.  Not that this call blocks 
    for 250ms to allow the sensor to return the data"""
        data = []
        self.bus.write_byte(self._I2C_ADDRESS, self.
_TRIGGER_HUMIDITY_NO_HOLD)
        time.sleep(0.250)
        data.append(self.bus.read_byte(self._I2C_ADDRESS))
        data.append(self.bus.read_byte(self._I2C_ADDRESS))
        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 *= 175.72
        unadjusted /= 65536 # divide by 2^16
        unadjusted -= 46.85
        return unadjusted
    
    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 *= 125.0
        unadjusted /= 1 << 16 # divide by 2^16
        unadjusted -= 6.0
        return unadjusted
        
                

    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]
 
        # POLYNOMIAL = 0x0131 = x^8 + x^5 + x^4 + 1
        # 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_humidity()
            
            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
from Adafruit_I2C import Adafruit_I2C
import math 

class HTU21D():
 
    # HTU21D Address
    address = 0x40
 
    # Commands
    TRIGGER_TEMP_MEASURE_HOLD = 0xE3
    TRIGGER_HUMD_MEASURE_HOLD = 0xE5
    READ_USER_REG = 0xE7
    WRITE_USER_REG = 0xE6
 
    # Constructor
    def __init__(self):
        self.i2c = Adafruit_I2C(self.address)
        
    def writeUserRegister(self, value):
        self.i2c.write8(self.WRITE_USER_REG, value)
    
    def readUserRegister(self):
        # Read a byte
        value = self.i2c.readU8(self.READ_USER_REG)
 
        return value
 
    def readTemperatureData(self):
        # value[0], value[1]: Raw temperature data
        # value[2]: CRC
        value = self.i2c.readList(self.TRIGGER_TEMP_MEASURE_HOLD, 3)
        #    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
 
    def readHumidityData(self, korjaus=0):
        # value[0], value[1]: Raw relative humidity data
        # value[2]: CRC
        value = self.i2c.readList(self.TRIGGER_HUMD_MEASURE_HOLD, 3)
 
        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()




a = sensor.readHumidityData()
b = sensor.readTemperatureData()
print a
print b
  




And this prints:
49.549621582
5.45361999512



-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to