AHT10 Temperature and Humidity Sensor
AHT10, the new generation of temperature and humidity sensors, sets a new standard in size and intelligence: it embeds a dual-row flat leadless SMD package suitable for reflow soldering, with a bottom surface of 4 x 5mm and a height of 1.6mm. The sensor outputs calibrated digital signals in standard I2C format. The AHT10 is equipped with a newly designed ASIC dedicated chip, an improved MEMS semiconductor capacitive humidity sensing element, and a standard on-chip temperature sensing element. Its performance has been greatly improved and even exceeds the reliability level of the previous generation of sensors. The new generation of temperature and humidity sensors has been improved to make its performance more stable in harsh environments. Each sensor is calibrated and tested, and the product batch number is printed on the product surface. Due to improvements and miniaturization of the sensor, its cost performance is higher, and ultimately all devices will benefit from the cutting-edge energy-saving operation mode. The application scope is mainly in HVAC, dehumidifiers, testing and inspection equipment, consumer goods, automobiles, automatic control, data loggers, weather stations, home appliances, humidity regulation, medical and other related temperature and humidity detection controls.
Module Source
Purchase link: https://item.taobao.com/item.htm?spm=a230r.1.14.28.5b982786BAvoXy&id=630894804483&ns=1&abbucket=19#detail Materials download link: https://pan.baidu.com/s/1xTX_QCmEmy8DWgxgtgXXFw Extraction code: pp3k
Specifications
Operating voltage: 1.8~3.6V Operating current: 0.25~23uA Humidity error: ±2%RH Temperature error: ±0.3℃ Output method: IIC Number of pins: 3 Pin
View Materials
The device address is 0x38, but the last bit is the read/write bit, so one bit needs to be left for the read/write bit. Therefore, it needs to be shifted left by one bit, that is, 0x38<<1 to get 0x70.
Acquisition steps: (Write=0, Read=1) Start signal -> Device address shifted left by 1 bit + Write -> Wait for sensor acknowledge -> Send trigger measurement command (0XAC) -> Wait for sensor acknowledge -> Send data bit 0X33 -> Wait for sensor acknowledge -> Send data bit 0x00 -> Wait for sensor acknowledge -> Stop signal (optional) -> Start signal -> Device address shifted left by 1 bit + Read -> Wait for sensor acknowledge -> Read 8-bit data (status word) -> Host sends acknowledge -> Read humidity high byte data -> Host sends acknowledge -> Read humidity low byte data -> Host sends acknowledge -> Read the last 4 bits of humidity data and the highest 4 bits of temperature data -> Host sends acknowledge -> Read 8 bits of temperature data -> Host sends acknowledge -> Read 8 bits of temperature data -> Host sends acknowledge -> Stop signal.
8-bit status word, the meaning of each bit.
Example: Status byte = 0x1C 0X1C = 0001 1100 bit7 = Device idle bit6~5= NOR mode bit4 = Reserved BIT3 = 1 Calibrated bit0~2= Reserved
Humidity conversion formula: where SRH is the integrated 20-bit humidity data read. RH(%)=(SRH/2 to the power of 20)*100 Temperature conversion formula: where ST is the integrated 20-bit temperature data read.
Porting Process
Pin Selection
Port to Project
Our goal is to port the example to the ESP32-S3 dev board. Complete driver code has been provided for you. Follow the steps below to complete the porting. For detailed instructions on creating folders and new .c and .h files, refer to section 1.4.2 in the [DHT11 Temperature and Humidity Sensor] chapter; we will not repeat it here. Just note that here we change the file names bsp_dht11.c and bsp_dht11.h to bsp_aht10.c and bsp_aht10.h, and the folder name to AHT10.
Write Code
In the file bsp_sht10.c, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-02 LCKFB-lp first version
*/
#include "bsp_aht10.h"
#include "stdio.h"
float Temperature = 0;
float Humidity = 0;
void delay_ms(unsigned int ms)
{
vTaskDelay(ms / portTICK_PERIOD_MS);
}
void delay_us(unsigned int us)
{
ets_delay_us(us);
}
/******************************************************************
* Function Name: AHT10_GPIO_Init
* Function Description: AHT10 pin initialization
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void AHT10_GPIO_Init(void)
{
gpio_config_t lll_config = {
.pin_bit_mask = (1ULL<<AHT10_SCL_PIN)|(1ULL<<AHT10_SDA_PIN), //Configure pins
.mode =GPIO_MODE_OUTPUT, //Output mode
.pull_up_en = GPIO_PULLUP_ENABLE, //Enable pull-up
.pull_down_en = GPIO_PULLDOWN_DISABLE, //Disable pull-down
.intr_type = GPIO_INTR_DISABLE //Disable pin interrupt
};
gpio_config(&lll_config);
}
/******************************************************************
* Function Name: IIC_Start
* Function Description: IIC start sequence
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Start(void)
{
SDA_OUT();
SDA(1);
delay_us(5);
SCL(1);
delay_us(5);
SDA(0);
delay_us(5);
SCL(0);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Stop
* Function Description: IIC stop signal
* Function Parameters: None
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Stop(void)
{
SDA_OUT();
SCL(0);
SDA(0);
SCL(1);
delay_us(5);
SDA(1);
delay_us(5);
}
/******************************************************************
* Function Name: IIC_Send_Ack
* Function Description: Host sends acknowledge or non-acknowledge signal
* Function Parameters: 0 send acknowledge 1 send non-acknowledge
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void IIC_Send_Ack(unsigned char ack)
{
SDA_OUT();
SCL(0);
SDA(0);
delay_us(5);
if(!ack) SDA(0);
else SDA(1);
SCL(1);
delay_us(5);
SCL(0);
SDA(1);
}
/******************************************************************
* Function Name: I2C_WaitAck
* Function Description: Wait for slave acknowledge
* Function Parameters: None
* Function Return: 0 acknowledged 1 timeout no acknowledge
* Author: LC
* Notes: None
******************************************************************/
unsigned char I2C_WaitAck(void)
{
char ack = 0;
unsigned char ack_flag = 10;
SCL(0);
SDA(1);
SDA_IN();
delay_us(5);
SCL(1);
delay_us(5);
while( (SDA_GET()==1) && ( ack_flag ) )
{
ack_flag--;
delay_us(5);
}
if( ack_flag <= 0 )
{
IIC_Stop();
return 1;
}
else
{
SCL(0);
SDA_OUT();
}
return ack;
}
/******************************************************************
* Function Name: Send_Byte
* Function Description: Write one byte
* Function Parameters: dat data to write
* Function Return: None
* Author: LC
* Notes: None
******************************************************************/
void Send_Byte(uint8_t dat)
{
int i = 0;
SDA_OUT();
SCL(0);//Pull clock low to start data transfer
for( i = 0; i < 8; i++ )
{
SDA( (dat & 0x80) >> 7 );
delay_us(1);
SCL(1);
delay_us(5);
SCL(0);
delay_us(5);
dat<<=1;
}
}
/******************************************************************
* Function Name: Read_Byte
* Function Description: IIC read sequence
* Function Parameters: None
* Function Return: Data read
* Author: LC
* Notes: None
******************************************************************/
unsigned char Read_Byte(void)
{
unsigned char i,receive=0;
SDA_IN();//Set SDA as input
for(i=0;i<8;i++ )
{
SCL(0);
delay_us(5);
SCL(1);
delay_us(5);
receive<<=1;
if( SDA_GET() )
{
receive|=1;
}
delay_us(5);
}
SCL(0);
return receive;
}
/**********************************************************
* Function Name: AHT10_Read
* Function: Read AHT10 temperature and humidity data
* Input Parameters: None
* Function Return: 0 read successfully
* Author: LC
* Notes: None
**********************************************************/
unsigned char AHT10_Read(void)
{
char timeout = 0;
unsigned char buff[6] = {0};
unsigned int dat = 0;
IIC_Start();//Start signal
Send_Byte(0X38<<1 | 0);//Device address + write command
I2C_WaitAck(); //Wait for sensor acknowledge
Send_Byte(0XAC);//Register address: trigger measurement command
I2C_WaitAck();//Wait for sensor acknowledge
Send_Byte(0X33);//Send data bit
I2C_WaitAck();//Wait for sensor acknowledge
Send_Byte(0X00);//Send data bit
I2C_WaitAck();//Wait for sensor acknowledge
IIC_Stop(); //Stop signal
do{
delay_ms(1);//Timeout judgment, wait for sensor to collect data
timeout++;
IIC_Start();//Resend start signal
Send_Byte(0X38<<1 | 1);//Device address + write command
}while( I2C_WaitAck() == 1 && timeout < 5 );
buff[0] = Read_Byte();//Read status word
IIC_Send_Ack(0);//Host sends acknowledge
buff[1] = Read_Byte();//Humidity high 8 bits data
IIC_Send_Ack(0);//Host sends acknowledge
buff[2] = Read_Byte();//Humidity low 8 bits data
IIC_Send_Ack(0);//Host sends acknowledge
buff[3] = Read_Byte();
IIC_Send_Ack(0);//Host sends acknowledge
buff[4] = Read_Byte();
IIC_Send_Ack(0);//Host sends acknowledge
buff[5] = Read_Byte();
IIC_Send_Ack(1);//Host sends acknowledge
IIC_Stop();//Stop signal
//High bit first
dat = (((buff[1]<<12) | (buff[2]<<4)) | (buff[3]>>4));
Humidity = dat / 1048576.0 * 100.0;
dat = 0;
dat = ((buff[3] &0x0F) << 16 ) | ( buff[4] << 8) | buff[5];
Temperature = (dat/1048576.0) * 200 - 50;
return 0;
}
/**********************************************************
* Function Name: Get_Temperature
* Function: Get the collected temperature data
* Input Parameters: None
* Function Return: Temperature data, unit ℃
* Author: LC
* Notes: Must collect data first, otherwise returns 0 or previous data
**********************************************************/
float Get_Temperature(void)
{
return Temperature;
}
/**********************************************************
* Function Name: Get_Humidity
* Function: Get the collected humidity data
* Input Parameters: None
* Function Return: Humidity data, unit %RH
* Author: LC
* Notes: Must collect data first, otherwise returns 0 or previous data
**********************************************************/
float Get_Humidity(void)
{
return Humidity;
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
In the file bsp_sht10.h, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-02 LCKFB-lp first version
*/
#ifndef _BSP_AHT10_H_
#define _BSP_AHT10_H_
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "freertos/queue.h"
#include <inttypes.h>
#include "sdkconfig.h"
#include "driver/gpio.h"
#include "esp_log.h"
#include "rom/ets_sys.h"
#include "esp_system.h"
#include "driver/gpio.h"
//Port porting
#define AHT10_SCL_PIN 1
#define AHT10_SDA_PIN 2
//Set SDA output mode
#define SDA_OUT() gpio_set_direction(AHT10_SDA_PIN,GPIO_MODE_OUTPUT)
//Set SDA input mode
#define SDA_IN() gpio_set_direction(AHT10_SDA_PIN,GPIO_MODE_INPUT)
//Get the level change of SDA pin
#define SDA_GET() gpio_get_level(AHT10_SDA_PIN)
//SDA and SCL output
#define SDA(x) gpio_set_level(AHT10_SDA_PIN, (x?1:0))
#define SCL(x) gpio_set_level(AHT10_SCL_PIN, (x?1:0))
void delay_us(unsigned int us);
void delay_ms(unsigned int ms);
void AHT10_GPIO_Init(void);
unsigned char AHT10_Read(void);
float Get_Temperature(void);
float Get_Humidity(void);
#endif2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Porting Verification
In the main function of your project, write the following code.
/*
* LCSC-Openkits (LCKFB) software and hardware materials and related expansion board software and hardware materials are all open source on the official website.
* Dev board official website: www.lckfb.com
* Technical support resides on the forum; any technical questions are welcome for exchange and learning at any time.
* LCKFB Forum: club.szlcsc.com
* Follow our Bilibili account: [LCSC-Openkits (LCKFB)] to keep up with our latest updates!
* We do not make money by selling boards; we take cultivating engineers as our mission.
* Change Logs:
* Date Author Notes
* 2024-01-02 LCKFB-lp first version
*/
#include <stdio.h>
#include "bsp_aht10.h"
void app_main(void)
{
AHT10_GPIO_Init();//AHT10 pin initialization
printf("start\r\n");
while(1)
{
//Collect temperature and humidity
AHT10_Read();
//Serial print temperature data
printf("temp = %.2f\r\n", Get_Temperature() );
//Serial print humidity data
printf("humi = %.2f\r\n", Get_Humidity() );
delay_ms(1000);
}
}2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Power-on effect:
Driver code:
File Download
📌 Materials Download Center (Click to Jump)
📌 In the Materials Download Center -> Module Porting Materials Download, inside the compressed package of this chapter.