Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing resolutions #11

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions adafruit_htu31d.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def measurements(self) -> Tuple[float, float]:
i2c.write(self._buffer, end=1)

# wait conversion time
time.sleep(0.02)
# Changed as reading temp and hum at OS3 is 20.32 ms
# See datasheet Table 5
time.sleep(0.03)

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

"""

return _HTU31D_HUMIDITY_RES[self._conversion_command >> 4 & 3]
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 3 & 3]

@humidity_resolution.setter
def humidity_resolution(
Expand All @@ -212,9 +214,9 @@ def humidity_resolution(
raise ValueError(
f"Humidity resolution must be one of: {_HTU31D_HUMIDITY_RES}"
)
register = self._conversion_command & 0xCF
register = self._conversion_command & 0xE7
hum_res = _HTU31D_HUMIDITY_RES.index(value)
self._conversion_command = register | hum_res << 4
self._conversion_command = register | hum_res << 3

@property
def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:
Expand All @@ -229,7 +231,7 @@ def temp_resolution(self) -> Literal["0.040", "0.025", "0.016", "0.012"]:

"""

return _HTU31D_TEMP_RES[self._conversion_command >> 2 & 3]
return _HTU31D_TEMP_RES[self._conversion_command >> 1 & 3]

@temp_resolution.setter
def temp_resolution(
Expand All @@ -239,9 +241,9 @@ def temp_resolution(
raise ValueError(
f"Temperature resolution must be one of: {_HTU31D_TEMP_RES}"
)
register = self._conversion_command & 0xF3
register = self._conversion_command & 0xF9
temp_res = _HTU31D_TEMP_RES.index(value)
self._conversion_command = register | temp_res << 2
self._conversion_command = register | temp_res << 1

@staticmethod
def _crc(value) -> int:
Expand Down
29 changes: 24 additions & 5 deletions examples/htu31d_setting_resolutions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#
# SPDX-License-Identifier: MIT

import time
import board
import adafruit_htu31d

Expand All @@ -15,16 +16,34 @@
print("Temperature Resolution: ", htu.temp_resolution)
print("Humidity Resolution: ", htu.humidity_resolution)


# Setting the temperature resolution.
# Possible values are "0.040", "0.025", "0.016" and "0.012"
htu.temp_resolution = "0.016"
htu.temp_resolution = "0.040"

# Setting the Relative Humidity resolution.
# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
htu.humidity_resolution = "0.007%"

htu.humidity_resolution = "0.020%"

# Printing the New Values
print("Temperature Resolution: ", htu.temp_resolution)
print("Humidity Resolution: ", htu.humidity_resolution)

hum_res = ["0.020%", "0.014%", "0.010%", "0.007%"]
temp_res = ["0.040", "0.025", "0.016", "0.012"]

while True:
for humidity_resolution in hum_res:
htu.humidity_resolution = humidity_resolution
print(f"Current Humidity Resolution: {humidity_resolution}")
for _ in range(2):
print(f"Humidity: {htu.relative_humidity:.2f}")
print(f"Temperature: {htu.temperature:.2f}")
print("")
time.sleep(0.5)
for temperature_resolution in temp_res:
htu.temp_resolution = temperature_resolution
print(f"Current Temperature Resolution: {temperature_resolution}")
for _ in range(2):
print(f"Humidity: {htu.relative_humidity:.2f}")
print(f"Temperature: {htu.temperature:.2f}")
print("")
time.sleep(0.5)