Skip to content

Commit 74eb4d0

Browse files
authored
Merge pull request #11 from jposada202020/fixing_resolution_setting
fixing resolutions
2 parents 78fde91 + a6757b6 commit 74eb4d0

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

adafruit_htu31d.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ def measurements(self) -> Tuple[float, float]:
163163
i2c.write(self._buffer, end=1)
164164

165165
# wait conversion time
166-
time.sleep(0.02)
166+
# Changed as reading temp and hum at OS3 is 20.32 ms
167+
# See datasheet Table 5
168+
time.sleep(0.03)
167169

168170
self._buffer[0] = _HTU31D_READTEMPHUM
169171
with self.i2c_device as i2c:
@@ -202,7 +204,7 @@ def humidity_resolution(self) -> Literal["0.020%", "0.014%", "0.010%", "0.007%"]
202204
203205
"""
204206

205-
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 4 & 3]
207+
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 3 & 3]
206208

207209
@humidity_resolution.setter
208210
def humidity_resolution(
@@ -212,9 +214,9 @@ def humidity_resolution(
212214
raise ValueError(
213215
f"Humidity resolution must be one of: {_HTU31D_HUMIDITY_RES}"
214216
)
215-
register = self._conversion_command & 0xCF
217+
register = self._conversion_command & 0xE7
216218
hum_res = _HTU31D_HUMIDITY_RES.index(value)
217-
self._conversion_command = register | hum_res << 4
219+
self._conversion_command = register | hum_res << 3
218220

219221
@property
220222
def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
@@ -229,7 +231,7 @@ def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
229231
230232
"""
231233

232-
return _HTU31D_TEMP_RES[self._conversion_command >> 2 & 3]
234+
return _HTU31D_TEMP_RES[self._conversion_command >> 1 & 3]
233235

234236
@temp_resolution.setter
235237
def temp_resolution(
@@ -239,9 +241,9 @@ def temp_resolution(
239241
raise ValueError(
240242
f"Temperature resolution must be one of: {_HTU31D_TEMP_RES}"
241243
)
242-
register = self._conversion_command & 0xF3
244+
register = self._conversion_command & 0xF9
243245
temp_res = _HTU31D_TEMP_RES.index(value)
244-
self._conversion_command = register | temp_res << 2
246+
self._conversion_command = register | temp_res << 1
245247

246248
@staticmethod
247249
def _crc(value) -> int:

examples/htu31d_setting_resolutions.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#
33
# SPDX-License-Identifier: MIT
44

5+
import time
56
import board
67
import adafruit_htu31d
78

@@ -15,16 +16,34 @@
1516
print("Temperature Resolution: ", htu.temp_resolution)
1617
print("Humidity Resolution: ", htu.humidity_resolution)
1718

18-
1919
# Setting the temperature resolution.
2020
# Possible values are "0.040", "0.025", "0.016" and "0.012"
21-
htu.temp_resolution = "0.016"
21+
htu.temp_resolution = "0.040"
2222

2323
# Setting the Relative Humidity resolution.
2424
# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
25-
htu.humidity_resolution = "0.007%"
26-
25+
htu.humidity_resolution = "0.020%"
2726

28-
# Printing the New Values
2927
print("Temperature Resolution: ", htu.temp_resolution)
3028
print("Humidity Resolution: ", htu.humidity_resolution)
29+
30+
hum_res = ["0.020%", "0.014%", "0.010%", "0.007%"]
31+
temp_res = ["0.040", "0.025", "0.016", "0.012"]
32+
33+
while True:
34+
for humidity_resolution in hum_res:
35+
htu.humidity_resolution = humidity_resolution
36+
print(f"Current Humidity Resolution: {humidity_resolution}")
37+
for _ in range(2):
38+
print(f"Humidity: {htu.relative_humidity:.2f}")
39+
print(f"Temperature: {htu.temperature:.2f}")
40+
print("")
41+
time.sleep(0.5)
42+
for temperature_resolution in temp_res:
43+
htu.temp_resolution = temperature_resolution
44+
print(f"Current Temperature Resolution: {temperature_resolution}")
45+
for _ in range(2):
46+
print(f"Humidity: {htu.relative_humidity:.2f}")
47+
print(f"Temperature: {htu.temperature:.2f}")
48+
print("")
49+
time.sleep(0.5)

0 commit comments

Comments
 (0)