Skip to content

Commit 3838e1f

Browse files
committed
htu21d: I2C address is constructor arg (iss peterhinch#130)
1 parent e6f4a33 commit 3838e1f

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

v3/as_drivers/htu21d/htu21d_mc.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import asyncio
1212
from micropython import const
1313

14-
_ADDRESS = const(0x40) # HTU21D Address
1514
_PAUSE_MS = const(60) # HTU21D acquisition delay
1615
_READ_USER_REG = const(0xE7)
1716

@@ -25,10 +24,11 @@ class HTU21D:
2524
START_TEMP_MEASURE = b"\xF3" # Commands
2625
START_HUMD_MEASURE = b"\xF5"
2726

28-
def __init__(self, i2c, read_delay=10):
27+
def __init__(self, i2c, read_delay=10, address=0x40):
2928
self.i2c = i2c
30-
if _ADDRESS not in self.i2c.scan():
29+
if address not in self.i2c.scan():
3130
raise OSError("No HTU21D device found.")
31+
self.address = address
3232
self.temperature = None
3333
self.humidity = None
3434
asyncio.create_task(self._run(read_delay))
@@ -46,9 +46,9 @@ def __iter__(self): # Await 1st reading
4646
yield from asyncio.sleep(0)
4747

4848
async def _get_data(self, cmd, divisor=0x131 << 15, bit=1 << 23):
49-
self.i2c.writeto(_ADDRESS, cmd) # Start reading
49+
self.i2c.writeto(self.address, cmd) # Start reading
5050
await asyncio.sleep_ms(_PAUSE_MS) # Wait for device
51-
value = self.i2c.readfrom(_ADDRESS, 3) # Read result, check CRC8
51+
value = self.i2c.readfrom(self.address, 3) # Read result, check CRC8
5252
data, crc = ustruct.unpack(">HB", value)
5353
remainder = (data << 8) | crc
5454
while bit > 128:
@@ -61,4 +61,4 @@ async def _get_data(self, cmd, divisor=0x131 << 15, bit=1 << 23):
6161
return data & 0xFFFC # Clear the status bits
6262

6363
def user_register(self): # Read the user register byte (should be 2)
64-
return self.i2c.readfrom_mem(_ADDRESS, _READ_USER_REG, 1)[0]
64+
return self.i2c.readfrom_mem(self.address, _READ_USER_REG, 1)[0]

v3/docs/HTU21D.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ import as_drivers.htu21d.htu_test
5252
This provides a single class `HTU21D`.
5353

5454
Constructor.
55-
This takes two args, `i2c` (mandatory) and an optional `read_delay=10`. The
56-
former must be an initialised I2C bus instance. The `read_delay` (secs)
57-
determines how frequently the data values are updated.
55+
This takes the following args
56+
* `i2c` (mandatory) An initialised I2C bus instance.
57+
* `read_delay=10`. The frequency (secs) at which data values are updated.
58+
* `address=0x40` I2C address of the chip.
5859

5960
Public bound values
6061
1. `temperature` Latest value in Celcius.

0 commit comments

Comments
 (0)