I've finally got my AHT10 working

Just wanted to share how I finally got my AHT10 working. I was getting the dreaded Communication with AHT10 failed! error no matter how many tweaks I made to my .yaml file. This was the typical log output:

[C][i2c.arduino:053]: I2C Bus:
[C][i2c.arduino:054]: SDA Pin: GPIO21
[C][i2c.arduino:055]: SCL Pin: GPIO22
[C][i2c.arduino:056]: Frequency: 200000 Hz
[C][i2c.arduino:059]: Recovery: bus successfully recovered
[I][i2c.arduino:069]: Results from i2c bus scan:
[I][i2c.arduino:075]: Found i2c device at address 0x38
[C][aht10:135]: AHT10:
[C][aht10:136]: Address: 0x38
[E][aht10:138]: Communication with AHT10 failed!

I’ve checked the wiring endless times and then tried using Arduino IDE with the Adafruit AHTX0 library instead of ESPHome. And it worked! So I look at the Adafruit AHTX0 code and found this:

  cmd[0] = AHTX0_CMD_CALIBRATE;
  cmd[1] = 0x08;
  cmd[2] = 0x00;
  i2c_dev->write(cmd, 3); // may not 'succeed' on newer AHT20s

So the calibrate command is written to the bus without checking the success of the command. Then the code goes on to wait for the sensor initialization.

I’ve guessed that the error I was in this part of the ESPHome component code:

  switch (this->variant_) {
    case AHT10Variant::AHT20:
      ESP_LOGCONFIG(TAG, "Setting up AHT20");
      error_code = this->write(AHT20_INITIALIZE_CMD, sizeof(AHT20_INITIALIZE_CMD));
      break;
    case AHT10Variant::AHT10:
      ESP_LOGCONFIG(TAG, "Setting up AHT10");
      error_code = this->write(AHT10_INITIALIZE_CMD, sizeof(AHT10_INITIALIZE_CMD));
      break;
  }
  if (error_code != i2c::ERROR_OK) {
    ESP_LOGE(TAG, "Communication with AHT10 failed!");
    this->mark_failed();
    return;
  }

and we should simply write the initialization command and go on to check if the sensor is done with it with the busy waiting loop that is after this.

So I copied everything under components/aht10 to config/my_components, simply commented the error check like this

// if (error_code != i2c::ERROR_OK) {
//    ESP_LOGE(TAG, "Communication with AHT10 failed!");
//    this->mark_failed();
//    return;
//  }

added the external component to the yaml

external_components:
  - source: my_components

and voila ! It worked!!

[C][aht10:050]: Setting up AHT10
[D][sensor:094]: 'Temperature': Sending state 27.35252 °C with 2 decimals of accuracy
[D][sensor:094]: 'Humidity': Sending state 66.48884 % with 2 decimals of accuracy

It is humid and hot here :hot_face: !!

I think the ‘fix’ is safe, as the code checks that the component is going out of BUSY status and replying with data, so this check is not necessary and, as the Adafruit library documents, newer components seem to give an error at this stage of the initialization.

So if someone like me had this sensor in a drawer cause they cannot make it work, give it a shot with this change.

2 Likes

Thanks for this, exactly the fix I needed. Sensors worked fine with Arduino/Adafruit but not with esphome.

2 Likes

Hi, I am running into the same issue with AHT10-Sensor. Wiring ok, I2C-Bus ok. Logfile is showing “Communication with AHT10 failed!”. I was changing my yaml-File the following:
sensor:

  • platform: aht10
    variant: AHT20
    Although I have a AHT10 I used variant: AHT20 and at least I got it working with values. I am pretty sure the values are wrong but was just checking if everything else is setup ok and wiring to the sensor ok what seems to be the case. Now I tried to follow with the tweak of the component explained by arpena but unclear where to store the changed components file? He stored under folder config/ but no idea where this folder is and if that component file does have any extension or just my_components. Does anybody know how to get this working with the seperate my_cmoponents file ?

Hi, was now checking the espHOME documents over the weekend how this is working with the components and how to build my own components folder. I followed aprenas instruction and was able to get the same result with variant = AHT10. Seems that the values are the same as if setting the variant to AHT20. So the first setting is more easy. I would suggest to use the variant AHT20 with the AHT10 sensor. Thanks for all this information in this community !

1 Like