adafruit / Adafruit_CircuitPython_AHTx0

CircuitPython driver for the Adafruit AHT10 Humidity and Temperature Sensor
MIT License
15 stars 11 forks source link

Calibrate Command not valid for newer ATH20 #17

Closed DemiVis closed 10 months ago

DemiVis commented 1 year ago

Relevant line: https://github.com/adafruit/Adafruit_CircuitPython_AHTx0/blame/e7b71a57a769c57ce4efb95491dffc32e5940132/adafruit_ahtx0.py#L48

While trying to initialize a new AHT20 device ( shop link ) using a QT Py ESP32-S2 WiFi Dev Board with STEMMA QT ( shop link ) connected using a breadboard and wires (not STEMMA) between labelled QT Py SCL/SDA and AHT20 SCL/SDA, labeled GND pins connected, and AHT20 VIN tied to QT Py 3V I was getting an error using the above code library on intial call with the following traceback:

Traceback (most recent call last):
  File "code.py", line 46, in <module>
  File "adafruit_ahtx0.py", line 96, in __init__
  File "adafruit_ahtx0.py", line 114, in calibrate
OSError: [Errno 19] No such device

Upon further investigation using an analog discovery, I found the AHT20 was NACK-ing the first word of calibrate command (defined in this library as AHTX0_CMD_CALIBRATE). Initial soft reboot: Initial soft reboot followed by NACK of Calibrate command first word: NACK of Calibrate command

I tried various other things, including manually emulating the library step by step to find the issue, and continually got NACK on that first word. Digging into the AHT20 documentation, I found this document that shows a different calibrate word in section 5.4

  1. Wait 40ms after power-on. Before reading the temperature and humidity values, first check whether the calibration enable bit Bit [3] of the status word is 1 (you can get a byte of status word by sending 0x71). If not 1, need to send 0xbe command (for initialization), this command parameter has two bytes, the first byte is 0x08, the second byte is 0x00, and then wait for 10ms

I modified my manual recreation of this library to use 0xBE as the first word instead and it worked. So I downloaded this file directly, modified only line 48 (above) to read

AHTX0_CMD_CALIBRATE: int = const(0xBE)  # Calibration command

and the library works exactly as intended. Further calls to the returned object as shown in the guide work as expected for as long as I tested it.

I didn't want to submit a pull request as I'm not sure this update would be valid for all uses of the library, but wanted to report the issue I saw and subsequent solution.

berarduc commented 1 year ago

I got this error too. Then I went to the github repo:

github.com/adafruit/Adafruit_CircuitPython_AHTx0.

---> In commit fe1abce dated Aug 15 (2023), a change was made to the calibrate command. The comment associated with the change was:

"# Newer AHT20's may not succeed, so wrapping in try/except"

When I downloaded this code and used it, the part worked.

berarduc commented 1 year ago

BTW, new AHT20's from Adafruit have a RED led instead of GREEN led, which adds to the confusion :)

DemiVis commented 10 months ago

@berarduc I saw that try/except, but it still seemed to be failing for me. Maybe I'm misunderstanding something but I thought regardless it should've just pass if that write doesn't work, I still want my sensor to do the calibration if that's an option from the sensor manufacturer! :)

ladyada commented 10 months ago

thank you!!! <3

hopefully resolved by merging https://github.com/adafruit/Adafruit_CircuitPython_AHTx0/pull/19 mpy and pypi will take a day to update!