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

Retry Calibration with AHT20 command on failure #19

Merged
merged 2 commits into from
Dec 18, 2023
Merged
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
24 changes: 19 additions & 5 deletions adafruit_ahtx0.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
__repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git"

AHTX0_I2CADDR_DEFAULT: int = const(0x38) # Default I2C address
AHTX0_CMD_CALIBRATE: int = const(0xE1) # Calibration command
AHT10_CMD_CALIBRATE: int = const(0xE1) # Calibration command for AHT10 sensor
AHT20_CMD_CALIBRATE: int = const(0xBE) # Calibration command for AHT20 sensor
AHTX0_CMD_TRIGGER: int = const(0xAC) # Trigger reading command
AHTX0_CMD_SOFTRESET: int = const(0xBA) # Soft reset command
AHTX0_STATUS_BUSY: int = const(0x80) # Status bit for busy
Expand Down Expand Up @@ -107,15 +108,28 @@ def reset(self) -> None:

def calibrate(self) -> bool:
"""Ask the sensor to self-calibrate. Returns True on success, False otherwise"""
# Newer AHT20's may not succeed, so wrapping in try/except
self._buf[0] = AHTX0_CMD_CALIBRATE
self._buf[0] = AHT10_CMD_CALIBRATE
self._buf[1] = 0x08
self._buf[2] = 0x00
calibration_failed = False
with self.i2c_device as i2c:
try:
# Newer AHT20's may not succeed with old command, so wrapping in try/except
i2c.write(self._buf, start=0, end=3)
except Exception: # pylint: disable=broad-except
pass
except (RuntimeError, OSError):
calibration_failed = True

if calibration_failed:
# try another calibration command for newer AHT20's
# print("Calibration failed, trying AH20 command")
time.sleep(0.01)
self._buf[0] = AHT20_CMD_CALIBRATE
with self.i2c_device as i2c:
try:
i2c.write(self._buf, start=0, end=3)
except (RuntimeError, OSError):
pass

while self.status & AHTX0_STATUS_BUSY:
time.sleep(0.01)
if not self.status & AHTX0_STATUS_CALIBRATED:
Expand Down