9
9
Python library for TE HTU31D temperature and humidity sensors
10
10
11
11
12
- * Author(s): ladyada
12
+ * Author(s): ladyada, Jose D. Montoya.
13
13
14
14
Implementation Notes
15
15
--------------------
16
16
17
17
**Hardware:**
18
18
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
+
20
22
21
23
**Software and Dependencies:**
22
24
23
25
* Adafruit CircuitPython firmware for the supported boards:
24
- https://github.com/adafruit/circuitpython/releases
26
+ https://circuitpython.org/downloads
25
27
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
27
30
28
31
"""
29
32
44
47
_HTU31D_CONVERSION = const (0x40 ) # Start a conversion
45
48
_HTU31D_READTEMPHUM = const (0x00 ) # Read the conversion values
46
49
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
+
47
53
48
54
class HTU31D :
49
55
"""
50
56
A driver for the HTU31D temperature and humidity sensor.
51
57
52
58
:param ~busio.I2C i2c_bus: The `busio.I2C` object to use. This is the only required parameter.
53
59
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
+
54
87
"""
55
88
56
89
def __init__ (self , i2c_bus ):
57
90
self .i2c_device = i2c_device .I2CDevice (i2c_bus , _HTU31D_DEFAULT_ADDR )
91
+ self ._conversion_command = _HTU31D_CONVERSION
58
92
self ._buffer = bytearray (6 )
59
93
self .reset ()
60
94
@@ -69,6 +103,7 @@ def serial_number(self):
69
103
70
104
def reset (self ):
71
105
"""Perform a soft reset of the sensor, resetting all settings to their power-on defaults"""
106
+ self ._conversion_command = _HTU31D_CONVERSION
72
107
self ._buffer [0 ] = _HTU31D_SOFTRESET
73
108
with self .i2c_device as i2c :
74
109
i2c .write (self ._buffer , end = 1 )
@@ -101,7 +136,7 @@ def relative_humidity(self):
101
136
102
137
@property
103
138
def temperature (self ):
104
- """The current temperature in degrees celsius """
139
+ """The current temperature in degrees Celsius """
105
140
return self .measurements [0 ]
106
141
107
142
@property
@@ -111,7 +146,7 @@ def measurements(self):
111
146
temperature = None
112
147
humidity = None
113
148
114
- self ._buffer [0 ] = _HTU31D_CONVERSION
149
+ self ._buffer [0 ] = self . _conversion_command
115
150
with self .i2c_device as i2c :
116
151
i2c .write (self ._buffer , end = 1 )
117
152
@@ -142,6 +177,56 @@ def measurements(self):
142
177
143
178
return (temperature , humidity )
144
179
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
+
145
230
@staticmethod
146
231
def _crc (value ):
147
232
polynom = 0x988000 # x^8 + x^5 + x^4 + 1
0 commit comments