|
45 | 45 | __repo__: str = "https://github.com/adafruit/Adafruit_CircuitPython_AHTx0.git" |
46 | 46 |
|
47 | 47 | AHTX0_I2CADDR_DEFAULT: int = const(0x38) # Default I2C address |
48 | | -AHTX0_CMD_CALIBRATE: int = const(0xE1) # Calibration command |
| 48 | +AHT10_CMD_CALIBRATE: int = const(0xE1) # Calibration command for AHT10 sensor |
| 49 | +AHT20_CMD_CALIBRATE: int = const(0xBE) # Calibration command for AHT20 sensor |
49 | 50 | AHTX0_CMD_TRIGGER: int = const(0xAC) # Trigger reading command |
50 | 51 | AHTX0_CMD_SOFTRESET: int = const(0xBA) # Soft reset command |
51 | 52 | AHTX0_STATUS_BUSY: int = const(0x80) # Status bit for busy |
@@ -107,15 +108,28 @@ def reset(self) -> None: |
107 | 108 |
|
108 | 109 | def calibrate(self) -> bool: |
109 | 110 | """Ask the sensor to self-calibrate. Returns True on success, False otherwise""" |
110 | | - # Newer AHT20's may not succeed, so wrapping in try/except |
111 | | - self._buf[0] = AHTX0_CMD_CALIBRATE |
| 111 | + self._buf[0] = AHT10_CMD_CALIBRATE |
112 | 112 | self._buf[1] = 0x08 |
113 | 113 | self._buf[2] = 0x00 |
| 114 | + calibration_failed = False |
114 | 115 | with self.i2c_device as i2c: |
115 | 116 | try: |
| 117 | + # Newer AHT20's may not succeed with old command, so wrapping in try/except |
116 | 118 | i2c.write(self._buf, start=0, end=3) |
117 | | - except Exception: # pylint: disable=broad-except |
118 | | - pass |
| 119 | + except (RuntimeError, OSError): |
| 120 | + calibration_failed = True |
| 121 | + |
| 122 | + if calibration_failed: |
| 123 | + # try another calibration command for newer AHT20's |
| 124 | + # print("Calibration failed, trying AH20 command") |
| 125 | + time.sleep(0.01) |
| 126 | + self._buf[0] = AHT20_CMD_CALIBRATE |
| 127 | + with self.i2c_device as i2c: |
| 128 | + try: |
| 129 | + i2c.write(self._buf, start=0, end=3) |
| 130 | + except (RuntimeError, OSError): |
| 131 | + pass |
| 132 | + |
119 | 133 | while self.status & AHTX0_STATUS_BUSY: |
120 | 134 | time.sleep(0.01) |
121 | 135 | if not self.status & AHTX0_STATUS_CALIBRATED: |
|
0 commit comments