Skip to content

Adding temp rh resolution #16

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

Merged
merged 3 commits into from
May 15, 2021
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
5 changes: 2 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,10 @@ Usage Example

import time
import board
import busio
from adafruit_htu21d import HTU21D

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = HTU21D(i2c)


Expand Down
73 changes: 57 additions & 16 deletions adafruit_htu21d.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,17 @@

**Hardware:**

* Adafruit `HTU21D-F Temperature & Humidity Sensor Breakout Board
* `Adafruit HTU21D-F Temperature & Humidity Sensor Breakout Board
<https://www.adafruit.com/product/1899>`_ (Product ID: 1899)

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
https://circuitpython.org/downloads

* Adafruit's Bus Device library:
https://github.com/adafruit/Adafruit_CircuitPython_BusDevice

"""
try:
import struct
Expand All @@ -42,9 +43,12 @@
HUMIDITY = const(0xF5)
TEMPERATURE = const(0xF3)
_RESET = const(0xFE)
_WRITE_USER1 = const(0xE6)
_READ_USER1 = const(0xE7)
_USER1_VAL = const(0x3A)

_TEMP_RH_RES = (0, 1, 128, 129)


def _crc(data):
crc = 0
Expand All @@ -62,36 +66,32 @@ def _crc(data):
class HTU21D:
"""
A driver for the HTU21D-F temperature and humidity sensor.

:param i2c_bus: The `busio.I2C` object to use. This is the only required parameter.
:param i2c_bus: The I2C bus the device is connected to
:param int address: (optional) The I2C address of the device. Defaults to :const:`0x40`

**Quickstart: Importing and using the HTU21D-F**

**Quickstart: Importing and using the HTU21D temperature sensor**

Here is one way of importing the `HTU21D` class so you can use it with the name ``htu``.
Here is an example of using the :class:`HTU21D` class.
First you will need to import the libraries to use the sensor

.. code-block:: python

import busio
import board
import adafruit_htu21d
from adafruit_htu21d import HTU21D

Once this is done you can define your `busio.I2C` object and define your sensor object
Once this is done you can define your `board.I2C` object and define your sensor object

.. code-block:: python

i2c = busio.I2C(board.SCL, board.SDA)
htu = adafruit_htu21d.HTU21D(i2c)
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = HTU21D(i2c)

Now you have access to the temperature and humidity using
the :attr:`temperature` and :attr:`relative_humidity` attributes
Now you have access to the :attr:`temperature` and :attr:`relative_humidity` attributes

.. code-block:: python

temperature = htu.temperature
relative_humidity = htu.relative_humidity
temperature = sensor.temperature
relative_humidity = sensor.relative_humidity


"""
Expand All @@ -100,6 +100,7 @@ def __init__(self, i2c_bus, address=0x40):
self.i2c_device = I2CDevice(i2c_bus, address)
self._command(_RESET)
self._measurement = 0
self._buffer = bytearray(3)
time.sleep(0.01)

def _command(self, command):
Expand Down Expand Up @@ -157,3 +158,43 @@ def measurement(self, what):
elif self._measurement != what:
raise RuntimeError("other measurement in progress")
self._measurement = what

@property
def temp_rh_resolution(self):
"""The temperature and relative humidity resolution

Have one of the following values: [#f1]_

======= ============== ==============
value RH res % T res C
======= ============== ==============
0 0.04 (12bit) 0.01 (14bit)
1 0.7 (8bit) 0.04 (12bit)
2 0.17 (10bit) 0.02 (13bit)
3 0.08 (11bit) 0.08 (11bit)
======= ============== ==============


.. [#f1] HTU21D(F) RH/T Sensor IC Datasheet. TE connectivity. 2017. p13

"""

self._buffer[0] = _READ_USER1
with self.i2c_device as i2c:
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1)

return self._buffer[0]

@temp_rh_resolution.setter
def temp_rh_resolution(self, value):
self._buffer[0] = _READ_USER1
with self.i2c_device as i2c:
i2c.write_then_readinto(self._buffer, self._buffer, out_end=1)

register = (self._buffer[0] & 0xFE) | _TEMP_RH_RES[value]

self._buffer[0] = 0xE6
self._buffer[1] = register

with self.i2c_device as i2c:
i2c.write(self._buffer)
5 changes: 2 additions & 3 deletions examples/htu21d_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

import time
import board
import busio
from adafruit_htu21d import HTU21D

# Create library object using our Bus I2C port
i2c = busio.I2C(board.SCL, board.SDA)
# Create sensor object, communicating over the board's default I2C bus
i2c = board.I2C() # uses board.SCL and board.SDA
sensor = HTU21D(i2c)


Expand Down