Files
2026-09-12 17:25:55 -04:00

37 lines
1015 B
Python

import time
class SHT41:
DEFAULT_ADDR = 0x44
CMD_MEASURE_HIGH_PRECISION = 0xFD
def __init__(self, i2c, addr=DEFAULT_ADDR):
self.i2c = i2c
self.addr = addr
# Soft reset
self.i2c.writeto(self.addr, b"\x94")
time.sleep(0.01)
def read_values(self):
# Trigger high precision measurement
self.i2c.writeto(self.addr, bytes([self.CMD_MEASURE_HIGH_PRECISION]))
# Conversion takes up to 8.2ms
time.sleep(0.01)
# 6 bytes: [Temp_MSB, Temp_LSB, CRC, Hum_MSB, Hum_LSB, CRC]
data = self.i2c.readfrom(self.addr, 6)
raw_t = (data[0] << 8) | data[1]
raw_h = (data[3] << 8) | data[4]
# Sensirion SHT4x conversion equations
temp = -45.0 + 175.0 * (raw_t / 65535.0)
hum = -6.0 + 125.0 * (raw_h / 65535.0)
hum = max(0.0, min(hum, 100.0))
return {
"temperature": round(temp, 2),
"humidity": round(hum, 2),
"pressure": None,
}