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 !!
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.