|
1 | | -[](https://components.espressif.com/components/espressif/aht20) |
| 1 | +# aht20 |
| 2 | +I2C driver for Aosong AHT20 humidity and temperature sensor using esp-idf. |
| 3 | +Tested with AHT20 using ESP32 and ESP32-S3 devkits. |
2 | 4 |
|
3 | | -# Component: AHT20 |
4 | | -I2C driver and definition of AHT20 humidity and temperature sensor. |
| 5 | +# Features |
5 | 6 |
|
6 | | -Components compatible with AHT30 and AHT21 (AHT21 is deprecated). |
| 7 | + Temperature and humidity measurement |
7 | 8 |
|
8 | | -See [AHT20 datasheet](http://www.aosong.com/en/products-32.html), [AHT30 datasheet](http://www.aosong.com/en/products-131.html). |
| 9 | + Thread-safe via esp-i2c-driver |
9 | 10 |
|
| 11 | + CRC checksum verification (optional via menuconfig) |
10 | 12 |
|
11 | | -## Usage |
| 13 | + Configurable I2C clock speed (pre-compilation, not runtime,via menuconfig)) |
12 | 14 |
|
13 | | -### Initialization |
14 | | -> Note: Note: You need to initialize the I2C bus first. |
| 15 | + Unit tested |
| 16 | + |
| 17 | + |
| 18 | + ┌───────────────────┐ |
| 19 | + │ Application │ |
| 20 | + └────────┬──────────┘ |
| 21 | + │ |
| 22 | + ▼ |
| 23 | + ┌───────────────────┐ |
| 24 | + │ AHT20 Driver │ |
| 25 | + │ (this component) │ |
| 26 | + └────────┬──────────┘ |
| 27 | + │ |
| 28 | + ▼ |
| 29 | + ┌───────────────────┐ |
| 30 | + │ i2c_bus component │ |
| 31 | + └────────┬──────────┘ |
| 32 | + │ |
| 33 | + ▼ |
| 34 | + ┌───────────────────┐ |
| 35 | + │ I2C Bus │ |
| 36 | + └────────┬──────────┘ |
| 37 | + │ |
| 38 | + ▼ |
| 39 | + ┌────────────────────────────┐ |
| 40 | + │ AHT20 Temperature/Humidity │ |
| 41 | + │ Sensor │ |
| 42 | + └────────────────────────────┘ |
| 43 | + |
| 44 | + |
| 45 | + |
| 46 | +# How To Use |
| 47 | + |
| 48 | +This driver includes a demo example project. |
| 49 | + |
| 50 | + Follow the example to learn how to initialize the driver and read the sensor data. |
| 51 | + |
| 52 | + All public APIs are documented in aht20.h. |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | +However, following are the general guiedlines. |
15 | 57 | ```c |
16 | | - aht20_i2c_config_t i2c_conf = { |
17 | | - .i2c_port = I2C_MASTER_NUM, |
18 | | - .i2c_addr = AHT20_ADDRRES_0, |
19 | | - }; |
20 | | - aht20_new_sensor(&i2c_conf, &handle); |
| 58 | + //create a AHT20 device object and receive a device handle for it |
| 59 | + // my_i2c_bus_handle is a preintialized i2c_bus_handle_t object |
| 60 | + aht20_handle_t aht20_handle = aht20_create( my_i2c_bus_handle, AHT20_ADDRESS_LOW ); //addresses are in aht.h |
| 61 | + |
| 62 | + //use the previously created AHT20 device handle for initializing the associated device |
| 63 | + aht20_init(aht20_handle); |
| 64 | + |
| 65 | + //read both humidity and temperature at once from device, using AHT20 device handle |
| 66 | + aht20_read_humiture(aht20_handle); //Other public APIs are documented in AHT20.h. |
| 67 | + |
| 68 | + //access the results stored in AHT20 device object, using the AHT20 device handle |
| 69 | + //other apis require user to explicitly pass variable address to hold data |
| 70 | + printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity); |
| 71 | + |
| 72 | + //to get reaw values create a object of following data type |
| 73 | + aht20_raw_reading_t raw_value; |
| 74 | + aht20_read_raw( aht20_handle, &raw_value); |
| 75 | + printf("tempertature = %uC humidity = %u \n", raw_value.temperature, raw_value.humidity); |
21 | 76 | ``` |
22 | 77 |
|
23 | | -### Read data |
24 | | -> The user can periodically call the aht20_read_temp_hum API to retrieve real-time data. |
25 | | -```c |
26 | | - uint32_t temp_raw, hum_raw; |
27 | | - float temp, hum; |
28 | 78 |
|
29 | | - aht20_read_temp_hum(aht20, &temp_raw, &temp, &hum_raw, &hum); |
30 | | - ESP_LOGI(TAG, "Humidity : %2.2f %%", hum); |
31 | | - ESP_LOGI(TAG, "Temperature : %2.2f degC", temp); |
32 | | -``` |
| 79 | +# How to Configure CRC and I2C clock speed |
| 80 | +Additionally, select in menuconfig under Component Config → AHT20,; to use CRC(default is not used) |
| 81 | +or change the clock speed of device (default is 100KHz). |
| 82 | +
|
| 83 | +Note : It is recommended to use clock speeds in upper ranges of 100kHz to 200kHz. |
| 84 | +Higher clock speeds may cause occasional data inconsistencies depending on your board layout and wiring. |
| 85 | +
|
| 86 | + |
| 87 | +
|
| 88 | +or |
| 89 | +In sdkconfig under Component Config → AHT20, |
| 90 | + |
0 commit comments