Skip to content

Commit 3e4c618

Browse files
committed
feat(aht20): add demo showcasing updated driver features
Added a demo application to illustrate usage of the updated AHT20 driver (v1.1.0). Demo includes usage of new sensor read APIs. Also serves as a reference for integrating the i2c_bus component from esp-iot-solution. Changes to be committed: new file: aht20_demo/CMakeLists.txt new file: aht20_demo/README.md new file: aht20_demo/main/CMakeLists.txt new file: aht20_demo/main/aht20_demo.c new file: aht20_demo/main/idf_component.yml
1 parent 3719c16 commit 3e4c618

File tree

5 files changed

+96
-0
lines changed

5 files changed

+96
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following five lines of boilerplate have to be in your project's
2+
# CMakeLists in this exact order for cmake to work correctly
3+
cmake_minimum_required(VERSION 3.16)
4+
5+
set(COMPONENT main)
6+
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
7+
project(aht20_demo)

examples/sensors/aht20_demo/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This is a demo which uses the aht20 driver.
2+
3+
It simply reads the humiture from aht20 sensor and prints the output continuously on terminal.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
idf_component_register(SRCS "aht20_demo.c"
2+
INCLUDE_DIRS ".")
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
/*
7+
* @file: aht20_demo.c
8+
*
9+
* @brief: A simple demo to use the AHT20 driver
10+
*
11+
* @date: May 2, 2025
12+
*
13+
* @Author: Rohan Jeet <[email protected]>
14+
*
15+
*/
16+
#include <stdio.h>
17+
#include <stdbool.h>
18+
#include <unistd.h>
19+
#include "aht20.h"
20+
21+
//i2c configuration values
22+
#define I2C_MASTER_SCL_IO (22) // SCL pin
23+
#define I2C_MASTER_SDA_IO (21) // SDA pin
24+
#define I2C_MASTER_NUM I2C_NUM_0
25+
#define I2C_MASTER_FREQ_HZ (400000) // I2C frequency
26+
27+
i2c_bus_handle_t my_i2c_bus_handle;
28+
29+
void i2c_master_init(void)
30+
{
31+
const i2c_config_t conf = {
32+
.mode = I2C_MODE_MASTER,
33+
.sda_io_num = I2C_MASTER_SDA_IO,
34+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
35+
.scl_io_num = I2C_MASTER_SCL_IO,
36+
.scl_pullup_en = GPIO_PULLUP_ENABLE,
37+
.master.clk_speed = I2C_MASTER_FREQ_HZ,
38+
};
39+
printf("requesting i2c bus handle\n");
40+
my_i2c_bus_handle = i2c_bus_create(I2C_MASTER_NUM, &conf);
41+
printf("i2c bus handle acquired\n");
42+
43+
}
44+
45+
void read_aht20(void *my_aht20_handle)
46+
{
47+
48+
aht20_handle_t aht20_handle = (aht20_handle_t) my_aht20_handle; //retrieve the AHT20 device handle copy
49+
vTaskDelay(400 / portTICK_PERIOD_MS);
50+
while (1) {
51+
//read both humidity and temperature at once from device, using AHT20 device handle
52+
aht20_read_humiture(aht20_handle);
53+
//access the results stored in AHT20 device object, using the AHT20 device handle
54+
//other apis require user to explicitly pass variable address to hold data
55+
printf("tempertature = %.2fC humidity = %.3f \n", aht20_handle->humiture.temperature, aht20_handle->humiture.humidity);
56+
vTaskDelay(1000 / portTICK_PERIOD_MS);
57+
}
58+
}
59+
60+
void init_aht20()
61+
{
62+
//create a AHT20 device object and receive a device handle for it
63+
aht20_handle_t aht20_handle = aht20_create(my_i2c_bus_handle, AHT20_ADDRESS_LOW);
64+
65+
printf("initializing aht20\n");
66+
//use the previously created AHT20 device handle for initializing the associated device
67+
while (aht20_init(aht20_handle) != ESP_OK) { // wait until it is initialized
68+
vTaskDelay(100 / portTICK_PERIOD_MS);
69+
}
70+
printf("aht20 initialized\n");
71+
72+
//creating a task to read from AHT20, passing the AHT20 device handle copy
73+
xTaskCreate(read_aht20, "aht20_tester", 2500, aht20_handle, 5, NULL);
74+
}
75+
76+
void app_main(void)
77+
{
78+
i2c_master_init(); //initialize i2c master
79+
init_aht20(); // user defined function for aht20 initialization
80+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dependencies:
2+
aht20:
3+
version: "*"
4+
override_path: "../../../../components/sensors/humiture/aht20"

0 commit comments

Comments
 (0)