Skip to content

Commit d61e3d2

Browse files
Merge pull request #5 from jposada202020/adding_temp_rh_resolution_properties
Adding temp rh resolution properties
2 parents 9557981 + 102339e commit d61e3d2

File tree

6 files changed

+132
-11
lines changed

6 files changed

+132
-11
lines changed

README.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,10 @@ Usage Example
6262
.. code-block:: python
6363
6464
import time
65-
import busio
6665
import board
6766
import adafruit_htu31d
6867
69-
i2c = busio.I2C(board.SCL, board.SDA)
68+
i2c = board.I2C() # uses board.SCL and board.SDA
7069
htu = adafruit_htu31d.HTU31D(i2c)
7170
print("Found HTU31D with serial number", hex(htu.serial_number))
7271

adafruit_htu31d.py

Lines changed: 91 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@
99
Python library for TE HTU31D temperature and humidity sensors
1010
1111
12-
* Author(s): ladyada
12+
* Author(s): ladyada, Jose D. Montoya.
1313
1414
Implementation Notes
1515
--------------------
1616
1717
**Hardware:**
1818
19-
* Adafruit HTU31D breakout: https://www.adafruit.com/product/4832
19+
* `Adafruit HTU31 Temperature & Humidity Sensor Breakout Board
20+
<https://www.adafruit.com/product/4832>`_ (Product ID: 4832)
21+
2022
2123
**Software and Dependencies:**
2224
2325
* Adafruit CircuitPython firmware for the supported boards:
24-
https://github.com/adafruit/circuitpython/releases
26+
https://circuitpython.org/downloads
2527
26-
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
28+
* Adafruit's Bus Device library:
29+
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2730
2831
"""
2932

@@ -44,17 +47,48 @@
4447
_HTU31D_CONVERSION = const(0x40) # Start a conversion
4548
_HTU31D_READTEMPHUM = const(0x00) # Read the conversion values
4649

50+
_HTU31D_HUMIDITY_RES = ("0.020%", "0.014%", "0.010%", "0.007%")
51+
_HTU31D_TEMP_RES = ("0.040", "0.025", "0.016", "0.012")
52+
4753

4854
class HTU31D:
4955
"""
5056
A driver for the HTU31D temperature and humidity sensor.
5157
5258
:param ~busio.I2C i2c_bus: The `busio.I2C` object to use. This is the only required parameter.
5359
60+
**Quickstart: Importing and using the device**
61+
62+
Here is an example of using the :class:`HTU31D` class.
63+
First you will need to import the libraries to use the sensor
64+
65+
.. code-block:: python
66+
67+
import board
68+
import adafruit_htu31d
69+
70+
Once this is done you can define your `board.I2C` object and define your sensor object
71+
72+
.. code-block:: python
73+
74+
i2c = board.I2C() # uses board.SCL and board.SDA
75+
htu = adafruit_htu31d.HTU31D(i2c)
76+
77+
78+
Now you have access to the :attr:`temperature` and :attr:`relative_humidity`
79+
attributes
80+
81+
.. code-block:: python
82+
83+
temperature = htu.temperature
84+
relative_humidity = htu.relative_humidity
85+
86+
5487
"""
5588

5689
def __init__(self, i2c_bus):
5790
self.i2c_device = i2c_device.I2CDevice(i2c_bus, _HTU31D_DEFAULT_ADDR)
91+
self._conversion_command = _HTU31D_CONVERSION
5892
self._buffer = bytearray(6)
5993
self.reset()
6094

@@ -69,6 +103,7 @@ def serial_number(self):
69103

70104
def reset(self):
71105
"""Perform a soft reset of the sensor, resetting all settings to their power-on defaults"""
106+
self._conversion_command = _HTU31D_CONVERSION
72107
self._buffer[0] = _HTU31D_SOFTRESET
73108
with self.i2c_device as i2c:
74109
i2c.write(self._buffer, end=1)
@@ -101,7 +136,7 @@ def relative_humidity(self):
101136

102137
@property
103138
def temperature(self):
104-
"""The current temperature in degrees celsius"""
139+
"""The current temperature in degrees Celsius"""
105140
return self.measurements[0]
106141

107142
@property
@@ -111,7 +146,7 @@ def measurements(self):
111146
temperature = None
112147
humidity = None
113148

114-
self._buffer[0] = _HTU31D_CONVERSION
149+
self._buffer[0] = self._conversion_command
115150
with self.i2c_device as i2c:
116151
i2c.write(self._buffer, end=1)
117152

@@ -142,6 +177,56 @@ def measurements(self):
142177

143178
return (temperature, humidity)
144179

180+
@property
181+
def humidity_resolution(self):
182+
"""The current relative humidity resolution in % rH.
183+
184+
Possibles values:
185+
186+
* "0.020%"
187+
* "0.014%"
188+
* "0.010%"
189+
* "0.007%"
190+
191+
"""
192+
193+
return _HTU31D_HUMIDITY_RES[self._conversion_command >> 4 & 3]
194+
195+
@humidity_resolution.setter
196+
def humidity_resolution(self, value):
197+
if value not in _HTU31D_HUMIDITY_RES:
198+
raise ValueError(
199+
"Humidity resolution must be one of: {}".format(_HTU31D_HUMIDITY_RES)
200+
)
201+
register = self._conversion_command & 0xCF
202+
hum_res = _HTU31D_HUMIDITY_RES.index(value)
203+
self._conversion_command = register | hum_res << 4
204+
205+
@property
206+
def temp_resolution(self):
207+
"""The current temperature resolution in Celsius.
208+
209+
Possibles values:
210+
211+
* "0.040"
212+
* "0.025"
213+
* "0.016"
214+
* "0.012"
215+
216+
"""
217+
218+
return _HTU31D_TEMP_RES[self._conversion_command >> 2 & 3]
219+
220+
@temp_resolution.setter
221+
def temp_resolution(self, value):
222+
if value not in _HTU31D_TEMP_RES:
223+
raise ValueError(
224+
"Temperature resolution must be one of: {}".format(_HTU31D_TEMP_RES)
225+
)
226+
register = self._conversion_command & 0xF3
227+
temp_res = _HTU31D_TEMP_RES.index(value)
228+
self._conversion_command = register | temp_res << 2
229+
145230
@staticmethod
146231
def _crc(value):
147232
polynom = 0x988000 # x^8 + x^5 + x^4 + 1

docs/examples.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,12 @@ Ensure your device works with this simple test.
66
.. literalinclude:: ../examples/htu31d_simpletest.py
77
:caption: examples/htu31d_simpletest.py
88
:linenos:
9+
10+
Setting Resolution Example
11+
--------------------------
12+
13+
Show how to setup Relative Humidity and Temperature Sensor Resolutions
14+
15+
.. literalinclude:: ../examples/htu31d_setting_resolutions.py
16+
:caption: examples/htu31d_setting_resolutions.py
17+
:linenos:

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Table of Contents
2626
.. toctree::
2727
:caption: Related Products
2828

29-
Adafruit HTU31D breakout <https://www.adafruit.com/product/4832>
29+
Adafruit HTU31 Temperature & Humidity Sensor Breakout Board <https://www.adafruit.com/product/4832>
3030

3131
.. toctree::
3232
:caption: Other Links
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Jose David M.
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import board
6+
import adafruit_htu31d
7+
8+
9+
# import htu31d_setting_resolutions
10+
i2c = board.I2C()
11+
htu = adafruit_htu31d.HTU31D(i2c)
12+
13+
14+
print("Temperature Resolution: ", htu.temp_resolution)
15+
print("Humidity Resolution: ", htu.humidity_resolution)
16+
17+
18+
# Setting the temperature resolution.
19+
# Possible values are "0.040", "0.025", "0.016" and "0.012"
20+
htu.temp_resolution = "0.016"
21+
22+
# Setting the Relative Humidity resolution.
23+
# Possible values are "0.020%", "0.014%", "0.010%" and "0.007%"
24+
htu.humidity_resolution = "0.007%"
25+
26+
27+
# Printing the New Values
28+
print("Temperature Resolution: ", htu.temp_resolution)
29+
print("Humidity Resolution: ", htu.humidity_resolution)

examples/htu31d_simpletest.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
# SPDX-License-Identifier: MIT
44

55
import time
6-
import busio
76
import board
87
import adafruit_htu31d
98

10-
i2c = busio.I2C(board.SCL, board.SDA)
9+
i2c = board.I2C() # uses board.SCL and board.SDA
1110
htu = adafruit_htu31d.HTU31D(i2c)
1211
print("Found HTU31D with serial number", hex(htu.serial_number))
1312

0 commit comments

Comments
 (0)