I am getting erroneous readings for Pressure(hPa) and Relative Humidity (%) using the LPS25H and the HTS221 Sensors respectively.
I have been using both these sensors on my Raspberry Pi, with Python cording.
Here is a copy of my init() routine for both sensors:
def init():
#Initialise the LPS25H sensor for continous reading
#---------------------------------------------------
i2c_bus.write_byte_data(LPS25H_address,0x21,0x04) #Software Reset and Boot
time.sleep(0.4) #Wait for reset
i2c_bus.write_byte_data(LPS25H_address,0x20,0x00) #Reseting the sensor
i2c_bus.write_byte_data(LPS25H_address,0x21,0x40) #FIFO Enable=40
i2c_bus.write_byte_data(LPS25H_address,0x2E,0xDF) #FIFO Mean,Stream:Newest Members, 16 sample moving avg
#Only powerup device after setting registers.
i2c_bus.write_byte_data(LPS25H_address,0x10,0x0C) #RES_CONF: Temp and Press Avg. 8 samples
i2c_bus.write_byte_data(LPS25H_address,0x20,0xC4) #PowerUp,BDU,25Hz
#Initialise the HTS221 Sensor for continous reading
#----------------------------------------------------
i2c_bus.write_byte_data(HTS221_address,0x21,0x80) #Boot
time.sleep(0.4) #Wait for Boot and Reset
i2c_bus.write_byte_data(HTS221_address,0x20,0x00) #Reset the sensor
i2c_bus.write_byte_data(HTS221_address,0x10,0x3F) #AVGT:256 and AVGH:512
i2c_bus.write_byte_data(HTS221_address,0x21,0x02) #Enable Heater
#time.sleep(120) #Sleep for 20ms whilst sensor gets heated
#i2c_bus.write_byte_data(HTS221_address,0x21,0x00) #Disable Heater
#Only powerup device after setting registers.
i2c_bus.write_byte_data(HTS221_address,0x20,0x87) #PU,BDU and 12.5HzThis is the code for my Read_Pressure_HPa() function:
def Read_Pressure_HPa():
while True:
if (IsDataReady(LPS25H_address,False)):
break
#Read press_xl,press_l and press_h registers
press_xl=i2c_bus.read_byte_data(LPS25H_address,0x28)
press_l=i2c_bus.read_byte_data(LPS25H_address,0x29)
press_h=i2c_bus.read_byte_data(LPS25H_address,0x2A)
#Concatonating
press=bytearray([press_xl,press_l,press_h,0x00])
#Converting to int32
press_i=(struct.unpack('<i',press)[0])
#Scaling
pressure= int(press_i/4096)
return pressureHere is the code for the Read_Humidity_Perc() function:
def Read_Humidity_Perc():
while True:
if (IsDataReady(HTS221_address,False)):
break
#Read h0 and h1 registers
h0=i2c_bus.read_byte_data(HTS221_address,0x30)
h1=i2c_bus.read_byte_data(HTS221_address,0x31)
#Read h0t0 registers
h0t0_l=i2c_bus.read_byte_data(HTS221_address,0x36)
h0t0_h=i2c_bus.read_byte_data(HTS221_address,0x37)
#Read t1 registers
h1t0_l=i2c_bus.read_byte_data(HTS221_address,0x3A)
h1t0_h=i2c_bus.read_byte_data(HTS221_address,0x3B)
#Read raw_humidity registers
rawHum_l=i2c_bus.read_byte_data(HTS221_address,0x28)
rawHum_h=i2c_bus.read_byte_data(HTS221_address,0x29)
#Converting h0 and h1 to short
h0_us=(int.from_bytes(bytearray([0x00,h0]),signed=False,byteorder='big')/2)
h1_us=(int.from_bytes(bytearray([0x00,h1]),signed=False,byteorder='big')/2)
#Converting h0t0 to short
h0t0=bytearray([h0t0_h,h0t0_l])
h0t0_s=int.from_bytes(h0t0,signed=True,byteorder='big')
#Converting t0 to short
h1t0=bytearray([h1t0_h,h1t0_l])
h1t0_s=int.from_bytes(h1t0,signed=True,byteorder='big')
#Converting raw_hum to short
rawHum=bytearray([rawHum_h,rawHum_l])
rawHum_s=int.from_bytes(rawHum,signed=True,byteorder='big')
#Scaling
humidity=(h1_us-h0_us)*(rawHum_s-h0t0_s)/(h1t0_s-h0t0_s)
humidity=humidity+h0_us
return round(humidity,2)Here are the readings that I got, when the heater on the HTS221 was turned off:


Here are the readings that I got, when the heater on the HTS221 was turned on:

I would be really great-full, if anyone can shed some light on the issue and let me know what's wrong.
