logo elektroda
logo elektroda
X
logo elektroda

AHT10/AHT2X/AHT30 I2C Temperature & Humidity Sensor Driver in OpenBeken presentation

divadiow  Cool? (+5)
📢 Listen (AI):
Images of AHT10, AHT20, AHT21, and AHT30 sensors with visible pin labels.
I noticed this pull request (PR) to add support for AHT10/AHT2x/AHT30 temperature and humidity sensors. We've seen the AHT30 in use in the TH01 device, but it is controlled by a TuyaMCU so does not need a driver for direct control by OpenBeken (OBK) in that instance. Same situation for the S09 with an AHT20.

As the Github user NonPlayerCharacter posted in the pull request, this driver will be needed for devices where the user has decided to remove the TuyaMCU, and so control the sensor directly with OBK, or where a device does not come with an MCU at all and the Beken chip interfaces directly.

Code: Text
Log in, to see the code

The sensors are made by Guangzhou Aosong Electronic Co. Ltd under the ASAIR brand name.

The product pages for each:
AHT10
AHT15
AHT20
AHT21
AHT25
AHT30

A comparison of the main specs:
Code: Text
Log in, to see the code


The English datasheets for each are attached to this thread for reference.

Onto some testing. I am using a NodeMCU dev board with a transplanted BK7231N based CB3S module. To save space, the AHTXX driver is disabled by default, ie it is not included in the general release builds. The binaries specific to the PR need to be flashed to make use of the driver. To download the special binaries navigate to https://github.com/openshwprojects/OpenBK7231T_App/actions/runs/8769670483, login to Github, scroll to the bottom of the Summary page and download the zip file under the Artifacts section

Screenshot of a Github page showing artifacts available for download.

If flashing a BK7231N device that's already running OBK you'll need to use the OTA update method to flash the OpenBK7231N_1187_merge_4738dd2d9ba2.rbl file in the downloaded zip. If flashing by UART using Easy UART Flasher then use OpenBK7231N_QIO_1187_merge_4738dd2d9ba2.bin

I used OTA method from the web application

Screenshot of the OTA tab for the BK7231N chipset.

After either method your build details on the OBK main page will be
Screenshot of the device information page on NodeMCU with BK7231N chipset.

and you'll see AHT2X driver options in the drop-downs on the module page
Pin configuration for AHT2X sensors in the user interface.

The AHT chips use the i2c protocol to communicate. The i2c (Inter-Integrated Circuit) protocol is a widely used communication method between components on a single integrated circuit, commonly used in microcontrollers and other digital electronics. It was developed by Philips Semiconductors (now NXP Semiconductors) in the early 1980s. i2c is particularly notable for its simplicity and effectiveness in managing communication between multiple integrated circuits over short distances using a minimum number of pins.

You'll notice the AHT (and other i2c drivers in OBK) require an SDA and an SCL pin configuring. This is because i2c only requires two wires to transmit data between devices:
SDA (Serial Data Line): This line is used for transmitting data between the master and slave devices.
SCL (Serial Clock Line): This line is used by the master device to synchronize data transmission across the i2c bus.

Additionally:
-Each device on the i2c bus has a unique address which is used by the master device to communicate with a specific slave device.
-The i2c protocol operates on a master-slave principle, where the master device initiates and controls the data transfer, and the slave devices respond to the master's requests.
-Although it is a master-slave protocol, i2c supports multiple masters on the same bus, allowing more than one master to control the bus without interference through a built-in arbitration process.
-All i2c transactions are synchronized with the clock pulses on the SCL line, making it a synchronous serial communication protocol.

With that in mind, it looks like I will need to use P20 (SCL1) for AHT2X_SCL and P21 (SDA1) for AHT2X_SDA with the CB3S module I have for testing
Pin diagram of the CB3S module with function labels.

First, here are the AHT sensors I have to play with. They're all from Ali Express. AHT10, AHT20, AHT21 and AHT30. This means I will not be able to test AHT15 or AHT25.

Images of AHT10, AHT20, AHT21, and AHT30 sensors with visible pin labels.

Starting with the AHT10, I connect SDA to P21 (which maps to S3 on NodeMCU) and SCL to P20 (which maps to SK on NodeMCU). I am powering the AHTs with an external 3.3v so a common ground is established between the PSU, the sensor and the NodeMCU.

AHT10 sensor module connected to NodeMCU board. NodeMCU development board connected with a sensor and power supply.

To confirm the sensor is detected we can use OBKs Softi2c feature to detect the address of the device. Set SoftSDA and SoftSCL pins as appropriate. In my case:
Screenshot of SoftSCL and SoftSDA settings on ports P20 and P21.

in the web application log tab we check the i2c driver is started by entering command startdriver i2c then we can scan for i2c devices with command scani2c soft

Screenshot of OBK configuration logs with i2c sensor detection

We see in the log it detects and returns the correct address for the AHT10 - 0x38 (in decimal = 56). We should be able to swap to the AHT2X driver now. (note SCK = SCL). All the AHTXX tested will return 0x38 as their address.

Screenshot of AHT2X pin configuration on NodeMCU.

You'll need to save changes after swapping to reveal the second channel box for the SDA assignment. Ensure both channels differ from any other in use. After a reboot the AHT2X driver starts automatically and the temperature and humidity data is displayed as expected

NodeMCU module configuration homepage with temperature and humidity data from AHT2X sensor

Zipping through the remaining sensors:

AHT20
Temperature and humidity readings from the AHT2X sensor in user interface.

AHT21
Screenshot showing temperature and humidity readings from AHT2X sensor.

AHT30
Screenshot showing temperature and humidity readings from the AHT2X sensor.

Apart from the AHT21 humidity, they all seem consistent. Perhaps the AHT21 will sort itself out if left untouched powered for a while. I also do not have any professional equipment to compare measurements against so not sure how accurate these readings are.

Sticking with the AHT30 I'll demonstrate MQTT/HomeAssistant configuration and discovery. From the Config->Configure MQTT menu I'll set some test topic names, my HA server IP and the username/password credentials specific to my setup and click submit.

MQTT configuration for AHT30 on the OpenBeken platform.

Then under Config->Home Assistant Configuration->Start Home Assistant Discovery

MQTT discovery in OpenBeken with Home Assistant discovery enabled

A few seconds later the device tile should appear in HA complete with device name and the sensor readings

Humidity and temperature readings on a NodeMCU CB3S device.

About Author
divadiow
divadiow wrote 3190 posts with rating 564 , helped 283 times. Live in city Bristol. Been with us since 2023 year.

Comments

p.kaczmarek2 07 May 2024 21:54

While all of these modules seem very cool, they miss one important feature - alert signal. Alert signal could be used to wake OBK when temperature reaches given threshold, thus help us save the battery. I... [Read more]

divadiow 07 May 2024 23:51

That'd be cool. Also, I wondered about this: https://github.com/openshwprojects/OpenBK7231T_App/pull/1187#issuecomment-2095717188 How does a unified driver cater for the differences between the sensors... [Read more]

p.kaczmarek2 08 May 2024 20:05

If I understand correctly, this is a question for the PR contributor, not for myself. Still, we can just check out the code... Are you saying that I2C scanner does not pick up the address? You... [Read more]

divadiow 08 May 2024 20:53

I mean OBK doesn't scan for any i2c address then match driver for device automatically. my device i2c is detected fine. yes that's cool. just linking this thread to that comment really. [Read more]

p.kaczmarek2 08 May 2024 21:10

The code you are referring to has I2C address hardcoded. It should be possible to adjust it to be taken from a variable or to rewrite the code so it can support multiple sensors if necessary. http... [Read more]

insmod 09 May 2024 01:13

I hardcoded the address, as from what i have seen, only AHT10 has the ability to change its address. And it still comes with 0x38 as default, and very few will have the tools | ability to change it. It... [Read more]

divadiow 24 May 2024 09:07

how easy would adding the alert feature be @insmod ? ;) [Read more]

insmod 24 May 2024 10:07

Impossible, i believe. The sensors lack the alert pin. Where CHT83XX have it, AHT is NC. [Read more]

divadiow 24 May 2024 10:09

ah, fair enough! [Read more]

divadiow 03 Jan 2025 20:48

in the case of builds where including the AHT2X driver does not lead to a drop-down entry in the module config page (eg BK-T https://github.com/openshwprojects/OpenBK7231T_App/pull/1490), the driver can... [Read more]

maxim2002 03 Feb 2025 04:46

I've tried this approach on one of aliexpress tuya generic th sensors. https://obrazki.elektroda.pl/9315335600_1738554233_thumb.jpg Did a build with ath2x and some mods to output the data from... [Read more]

p.kaczmarek2 03 Feb 2025 10:43

Which sensor do you have there, exactly? I am asking because I have a dev board here, with AHT20 from Aliexpress: https://obrazki.elektroda.pl/2863851200_1738575760_thumb.jpg https://obrazki.elektroda.pl/4177936600_1738575760_thumb.jpg... [Read more]

maxim2002 03 Feb 2025 11:23

Board pix + log attached in previous post. It just sends back 18 38 20 00 00 00 all the time. Odd thing is that init and rest of stuff up to "Unrealistic humidity, will not update values" you have in drv_aht2x.c... [Read more]

taggbricka 13 Jun 2025 22:48

Is it possible to find a working OTA bin file for BK7231N and AHT30? The link in the original post seems to be broken. [Read more]

divadiow 13 Jun 2025 23:27

it seems it's enabled in the general release https://github.com/openshwprojects/OpenBK7231T_App/edit/main/src/obk_config.h#L219 https://github.com/openshwprojects/OpenBK7231T_App/releases [Read more]

taggbricka 14 Jun 2025 21:06

Will DRIVER_AHT2X work also for AHT30? I have got some AHT30 breakout boards an am considering buying some CB3S boards if they will work together. Tasmota presently has no support for AHT30. [Read more]

insmod 15 Jun 2025 05:29

Tasmota should work with AHT30. There is no difference between aht20/21/25 and aht30 code-wise. And with some tuning, like i've done on AHT2X driver in OBK, all AHT chips will work with the same driver... [Read more]

taggbricka 15 Jun 2025 14:13

Great! I am not so familiar with openbeken. There was no AHT2x_SDA pin alternative present, so could not try that. startdriver AHT2x with SoftSDA on P7 and SoftSDA on P6 gave device timeout. But startdriver... [Read more]

%}