模块简介
产品概述
AHT20,新一代温湿度传感器在尺寸与智能方面建立了新的标准:它嵌入了适于回流焊的双列扁平无引脚SMD封装,底面 3 x 3mm ,高度1.0mm。传感器输出经过标定的数字信号,标准 I2 C 格式。
AHT20 配有一个全新设计的 ASIC专用芯片、一个经过改进的MEMS半导体电容式湿度传感元件和一个标准的片上温度传感元件,其性能已经大大提升甚至超出了前一代传感器的可靠性水平,新一代温湿度传感器,经过改进使其在恶劣环境下的性能更稳定。
应用场景
广泛应用于消费电子、医疗、汽车、工业、气象等领域,例如:暖通空调、除湿器、净化器和冰箱等家电产品、消费品、数据记录器、湿度调节、测试和检测设备及其他相关温湿度检测控制产品。
•完全标定
•数字输出,I²C接口
•优异的长期稳定性
•采用SMD封装适于回流焊
•响应迅速、抗干扰能力强
原理图
引脚分布
通信协议
1. 启动/停止时序
每个传输序列都以 Start状态作为开始并以 Stop 状态作为结束
Start时序-当 SCL 为高电平时,SDA 由高 电平转换为低电平
Stop时序- 当SCL 高电平时,SDA 线上从 低电平转换为高电平
2. 发送命令
在启动传输后,随后传输的I2C首字节包括7位 2 的I2C设备地址 0x38和一个SDA方向位 x(读R:‘1’,写W:‘0’)。在第8个SCL时钟下降沿之后,通过拉低SDA引脚 (ACK位),指示传感器数据接收正常。在发出初始化命令之后 (‘1011’1110')代表初始化,‘1010’1100’代表温湿度测量), MCU必须等待测量完成。
基本命令集如下表
从机返回状态位说明
3. 传感器读取流程
- 上电后要等待40ms,读取温湿度值之前,首先要看状态字的校准使能位Bit[3]是否为1(通过发送0x71可以获取一个字节的状态字),如果不为1,要发送0xBE命令(初始化),此命令参数有两个字节,第一个字节为0x08,第二个字节为0x00。( 校准状态检验只需要上电时检查,在正常采集过程无需操作。)
- 直接发送 0xAC命令(触发测量),此命令参数有两个字节,第一个字节为 0x33,第二个字节为0x00。
- 等待75ms待测量完成,忙状态Bit[7]为0,然后可以读取六个字节(发0X71即可以读取)。
- 计算温湿度值。
注:传感器在采集时需要时间,主机发出测量指令(0xAC) 后,延时75毫秒以上再读取转换后的数据并判断返回的状 态位是否正常。若状态比特位[Bit7]为0代表数据可正常 读取,为1时传感器为忙状态,主机需要等待数据处理完成。
信号转换
相对湿度RH都可以根据SDA输出的相对湿度信号SRH通过如下公式计算获得(结果以 %RH 表示):
温度T都可以通过将温度输出信号ST代入到下面的公式计算得到 (结果以温度 ℃ 表示):
了解了传感器通信协议就可以根据步骤编写代码采集数据了!话不多说,直接上代码!
参考代码
aht20_test.c
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#include "aht20.h"
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <stdio.h>
#include "cmsis_os2.h"
#include "ohos_run.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
void Aht20TestTask(void *arg)
{
(void)arg;
uint32_t retval = 0;
i2c_config_t i2c_initer = {
.clk_flags = 0, // 选择默认时钟源
.master.clk_speed = 50000, // 指定速率为100Kbit,最大可以为400Kbit
.mode = I2C_MODE_MASTER, // 主机模式
.scl_io_num = 22, // 指定SCL的GPIO口
.scl_pullup_en = true, // SCL接上拉电阻
.sda_io_num = 21, // 指定SDA的GPIO口
.sda_pullup_en = true, // SDA接上拉电阻
};
printf("sda_io_num is %d scl_io_num %d\r\n", i2c_initer.sda_io_num, i2c_initer.scl_io_num);
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
if (i2c_param_config(I2C_NUM_0, &i2c_initer) == ESP_OK)
{
printf("i2c parm config success\r\n");
}
else
{
printf("config fail\r\n");
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
if (i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0) == ESP_OK)
{
printf("i2c driver install success\r\n");
}
else
{
printf("driver fail\r\n");
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
retval = AHT20_Calibrate();
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
printf("AHT20_Calibrate: %d\r\n", retval);
while (1)
{
float temp = 0.0, humi = 0.0;
retval = AHT20_StartMeasure();
printf("AHT20_StartMeasure: %d\r\n", retval);
retval = AHT20_GetMeasureResult(&temp, &humi);
if (retval == 0)
{
printf("AHT20_GetMeasureResult: %d, temp = %.2f, humi = %.2f\r\n", retval, temp, humi);
}
sleep(1);
}
}
void Aht20Test(void)
{
osThreadAttr_t attr;
printf("Aht20Task\r\n");
attr.name = "Aht20Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 4096;
attr.priority = osPriorityNormal;
if (osThreadNew(Aht20TestTask, NULL, &attr) == NULL)
{
printf("[Aht20Test] Failed to create Aht20TestTask!\n");
}
}
OHOS_APP_RUN(Aht20Test);
aht20.c
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#include "aht20.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include "cmsis_os2.h"
#include "ohos_run.h"
#include "esp_system.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "driver/gpio.h"
#include "driver/i2c.h"
#define AHT20_I2C_IDX HI_I2C_IDX_0
#define AHT20_STARTUP_TIME 20 * 1000 // 上电启动时间
#define AHT20_CALIBRATION_TIME 40 * 1000 // 初始化(校准)时间
#define AHT20_MEASURE_TIME 75 * 1000 // 测量时间
#define AHT20_DEVICE_ADDR 0x38
#define AHT20_READ_ADDR ((0x38 << 1) | 0x1)
#define AHT20_WRITE_ADDR ((0x38 << 1) | 0x0)
#define AHT20_CMD_CALIBRATION 0xBE // 初始化(校准)命令
#define AHT20_CMD_CALIBRATION_ARG0 0x08
#define AHT20_CMD_CALIBRATION_ARG1 0x00
/**
* 传感器在采集时需要时间,主机发出测量指令(0xAC)后,延时75毫秒以上再读取转换后的数据并判断返回的状态位是否正常。
* 若状态比特位[Bit7]为0代表数据可正常读取,为1时传感器为忙状态,主机需要等待数据处理完成。
**/
#define AHT20_CMD_TRIGGER 0xAC // 触发测量命令
#define AHT20_CMD_TRIGGER_ARG0 0x33
#define AHT20_CMD_TRIGGER_ARG1 0x00
// 用于在无需关闭和再次打开电源的情况下,重新启动传感器系统,软复位所需时间不超过20 毫秒
#define AHT20_CMD_RESET 0xBA // 软复位命令
#define AHT20_CMD_STATUS 0x71 // 获取状态命令
/**
* STATUS 命令回复:
* 1. 初始化后触发测量之前,STATUS 只回复 1B 状态值;
* 2. 触发测量之后,STATUS 回复6B: 1B 状态值 + 2B 湿度 + 4b湿度 + 4b温度 + 2B 温度
* RH = Srh / 2^20 * 100%
* T = St / 2^20 * 200 - 50
**/
#define AHT20_STATUS_BUSY_SHIFT 7 // bit[7] Busy indication
#define AHT20_STATUS_BUSY_MASK (0x1 << AHT20_STATUS_BUSY_SHIFT)
#define AHT20_STATUS_BUSY(status) ((status & AHT20_STATUS_BUSY_MASK) >> AHT20_STATUS_BUSY_SHIFT)
#define AHT20_STATUS_MODE_SHIFT 5 // bit[6:5] Mode Status
#define AHT20_STATUS_MODE_MASK (0x3 << AHT20_STATUS_MODE_SHIFT)
#define AHT20_STATUS_MODE(status) ((status & AHT20_STATUS_MODE_MASK) >> AHT20_STATUS_MODE_SHIFT)
// bit[4] Reserved
#define AHT20_STATUS_CALI_SHIFT 3 // bit[3] CAL Enable
#define AHT20_STATUS_CALI_MASK (0x1 << AHT20_STATUS_CALI_SHIFT)
#define AHT20_STATUS_CALI(status) ((status & AHT20_STATUS_CALI_MASK) >> AHT20_STATUS_CALI_SHIFT)
// bit[2:0] Reserved
#define AHT20_STATUS_RESPONSE_MAX 6
#define AHT20_RESOLUTION (1 << 20) // 2^20
#define AHT20_MAX_RETRY 10
static uint32_t AHT20_Read(uint8_t *buffer, uint32_t buffLen)
{
int i;
uint8_t rdata;
i2c_cmd_handle_t cmd_handle = i2c_cmd_link_create();
i2c_master_start(cmd_handle);
i2c_master_write_byte(cmd_handle, AHT20_READ_ADDR, true);
for (i = 0; i < buffLen; i++)
{
i2c_master_read_byte(cmd_handle, &buffer[i], I2C_MASTER_ACK);
}
i2c_master_stop(cmd_handle);
esp_err_t error = i2c_master_cmd_begin(I2C_NUM_0, cmd_handle, 100);
i2c_cmd_link_delete(cmd_handle);
return error;
}
static uint32_t AHT20_Write(uint8_t *buffer, uint32_t buffLen)
{
int i;
i2c_cmd_handle_t cmd_handle = i2c_cmd_link_create();
i2c_master_start(cmd_handle);
i2c_master_write_byte(cmd_handle, AHT20_WRITE_ADDR, true);
for (i = 0; i < buffLen; i++)
{
i2c_master_write_byte(cmd_handle, buffer[i], true);
}
i2c_master_stop(cmd_handle);
esp_err_t error = i2c_master_cmd_begin(I2C_NUM_0, cmd_handle, 100);
i2c_cmd_link_delete(cmd_handle);
return error;
}
// 发送获取状态命令
static uint32_t AHT20_StatusCommand(void)
{
uint8_t statusCmd[] = {AHT20_CMD_STATUS};
return AHT20_Write(statusCmd, sizeof(statusCmd));
}
// 发送软复位命令
static uint32_t AHT20_ResetCommand(void)
{
uint8_t resetCmd[] = {AHT20_CMD_RESET};
return AHT20_Write(resetCmd, sizeof(resetCmd));
}
// 发送初始化校准命令
static uint32_t AHT20_CalibrateCommand(void)
{
uint8_t clibrateCmd[] = {AHT20_CMD_CALIBRATION, AHT20_CMD_CALIBRATION_ARG0, AHT20_CMD_CALIBRATION_ARG1};
return AHT20_Write(clibrateCmd, sizeof(clibrateCmd));
}
// 读取温湿度值之前, 首先要看状态字的校准使能位Bit[3]是否为 1(通过发送0x71可以获取一个字节的状态字),
// 如果不为1,要发送0xBE命令(初始化),此命令参数有两个字节, 第一个字节为0x08,第二个字节为0x00。
uint32_t AHT20_Calibrate(void)
{
uint32_t retval = 0;
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX];
memset(&buffer, 0x0, sizeof(buffer));
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
retval = AHT20_StatusCommand();
if (retval != 0)
{
return retval;
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
retval = AHT20_Read(buffer, sizeof(buffer));
if (retval != 0)
{
return retval;
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
if (AHT20_STATUS_BUSY(buffer[0]) || !AHT20_STATUS_CALI(buffer[0]))
{
retval = AHT20_ResetCommand();
if (retval != 0)
{
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
return retval;
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
usleep(AHT20_STARTUP_TIME);
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
retval = AHT20_CalibrateCommand();
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
usleep(AHT20_CALIBRATION_TIME);
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
return retval;
}
printf("Aht20Task %s %d\r\n", __FILE__, __LINE__);
return 0;
}
// 发送 触发测量 命令,开始测量
uint32_t AHT20_StartMeasure(void)
{
uint8_t triggerCmd[] = {AHT20_CMD_TRIGGER, AHT20_CMD_TRIGGER_ARG0, AHT20_CMD_TRIGGER_ARG1};
return AHT20_Write(triggerCmd, sizeof(triggerCmd));
}
void printfhex(char *buf, int len)
{
int i;
printf("printfhex : \r\n");
for (i = 0; i < len; i++)
{
printf("%x ", buf[i]);
}
printf("\r\n");
}
// 接收测量结果,拼接转换为标准值
uint32_t AHT20_GetMeasureResult(float *temp, float *humi)
{
uint32_t retval = 0, i = 0;
if (temp == NULL || humi == NULL)
{
return 1;
}
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX];
memset(&buffer, 0x0, sizeof(buffer));
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != 0)
{
return retval;
}
for (i = 0; AHT20_STATUS_BUSY(buffer[0]) && i < AHT20_MAX_RETRY; i++)
{
// printf("AHT20 device busy, retry %d/%d!\r\n", i, AHT20_MAX_RETRY);
usleep(AHT20_MEASURE_TIME);
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != 0)
{
return retval;
}
}
if (i >= AHT20_MAX_RETRY)
{
printf("AHT20 device always busy!\r\n");
return 0;
}
printfhex(buffer, AHT20_STATUS_RESPONSE_MAX);
uint32_t humiRaw = buffer[1];
humiRaw = (humiRaw << 8) | buffer[2];
humiRaw = (humiRaw << 4) | ((buffer[3] & 0xF0) >> 4);
*humi = humiRaw / (float)AHT20_RESOLUTION * 100;
uint32_t tempRaw = buffer[3] & 0x0F;
tempRaw = (tempRaw << 8) | buffer[4];
tempRaw = (tempRaw << 8) | buffer[5];
*temp = tempRaw / (float)AHT20_RESOLUTION * 200 - 50;
// printf("humi = %05X, %f, temp= %05X, %f\r\n", humiRaw, *humi, tempRaw, *temp);
return 0;
}
aht20.h
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
*
* limitations under the License.
*/
#ifndef AHT20_H
#define AHT20_H
#include <stdint.h>
uint32_t AHT20_Calibrate(void);
uint32_t AHT20_StartMeasure(void);
uint32_t AHT20_GetMeasureResult(float *temp, float *humi);
#endif // AHT20_H
BUILD.gn
# Copyright (c) 2022 Hunan OpenValley Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//kernel/liteos_m/liteos.gni")
module_name = get_path_info(rebase_path("."), "name")
kernel_module(module_name){
include_dirs = [
"//drivers/hdf_core/framework/include/platform/",
"//drivers/hdf_core/framework/include/utils/",
"//drivers/hdf_core/framework/support/platform/include/gpio",
"//drivers/hdf_core/adapter/khdf/liteos_m/osal/include/",
"//drivers/hdf_core/framework/include/core/",
"//drivers/hdf_core/framework/include/osal/",
"//device/soc/esp/esp32/components/driver/include",
"//device/soc/esp/esp32/components/esp_adc_cal/include",
"//device/soc/esp/esp32/components/driver/esp32/include",
]
sources = [
"aht20_test.c",
"aht20.c",
]
}
编译并烧录
在源码根目录下使用hb工具对写好的代码进行编译
选择mini级系统
同理 产品选择esp公司下的esp32
选择完毕后在源码根目录下执行hb build -f 进行编译
编译完成后会有如下界面,并且编译后的代码固件位于:out\esp32\esp32
实验现象
按下ESP32开发板上的EN键,即可观察到实验现象:
根据传感器读取到十六进制数据代入公式进行转换即可得到当前环境下的温湿度值!