/
AHT10_Serial.ino
218 lines (172 loc) · 7.64 KB
/
AHT10_Serial.ino
1
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
/***************************************************************************************************/
/*
This is an Arduino example for Aosong ASAIR AHT10/AHT15/AHT20/AHT21/AHT25/AM2301B/AM2311B
Digital Humidity & Temperature Sensor
written by : enjoyneering
sourse code: https://github.com/enjoyneering/
Aosong ASAIR AHT1x/AHT2x features:
- AHT1x +1.8v..+3.6v, AHT2x +2.2v..+5.5v
- AHT1x 0.25uA..320uA, AHT2x 0.25uA..980uA
- temperature range -40C..+85C
- humidity range 0%..100%
- typical accuracy T +-0.3C, RH +-2%
- typical resolution T 0.01C, RH 0.024%
- normal operating range T -20C..+60C, RH 10%..80%
- maximum operating rage T -40C..+80C, RH 0%..100%
- response time 8..30sec*
- I2C bus speed 100KHz..400KHz, 10KHz recommended minimum
*measurement with high frequency leads to heating of the
sensor, interval must be > 1 second to keep self-heating below 0.1C
This device uses I2C bus to communicate, specials pins are required to interface
Board SDA SCL Level
Uno, Mini, Pro, ATmega168, ATmega328..... A4 A5 5v
Mega2560................................. 20 21 5v
Due, SAM3X8E............................. 20 21 3.3v
MKR Zero, XIAO SAMD21, SAMD21xx.......... PA08 PA09 3.3v
Leonardo, Micro, ATmega32U4.............. 2 3 5v
Digistump, Trinket, Gemma, ATtiny85...... PB0/D0 PB2/D2 3.3v/5v
Blue Pill*, STM32F103xxxx boards*........ PB7/PB9 PB6/PB8 3.3v/5v
ESP8266 ESP-01**......................... GPIO0 GPIO2 3.3v/5v
NodeMCU 1.0**, WeMos D1 Mini**........... GPIO4/D2 GPIO5/D1 3.3v/5v
ESP32***................................. GPIO21/D21 GPIO22/D22 3.3v
GPIO16/D16 GPIO17/D17 3.3v
*hardware I2C Wire mapped to Wire1 in stm32duino
see https://github.com/stm32duino/wiki/wiki/API#I2C
**most boards has 10K..12K pullup-up resistor
on GPIO0/D3, GPIO2/D4/LED & pullup-down on
GPIO15/D8 for flash & boot
***hardware I2C Wire mapped to TwoWire(0) aka GPIO21/GPIO22 in Arduino ESP32
Supported frameworks:
Arduino Core - https://github.com/arduino/Arduino/tree/master/hardware
ATtiny Core - https://github.com/SpenceKonde/ATTinyCore
ESP8266 Core - https://github.com/esp8266/Arduino
ESP32 Core - https://github.com/espressif/arduino-esp32
STM32 Core - https://github.com/stm32duino/Arduino_Core_STM32
SAMD Core - https://github.com/arduino/ArduinoCore-samd
GNU GPL license, all text above must be included in any redistribution,
see link for details - https://www.gnu.org/licenses/licenses.html
*/
/***************************************************************************************************/
#include <Wire.h>
#include <AHTxx.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
float ahtValue; //to store T/RH result
AHTxx aht10(AHTXX_ADDRESS_X38, AHT1x_SENSOR); //sensor address, sensor type
/**************************************************************************/
/*
setup()
Main setup
*/
/**************************************************************************/
void setup()
{
#if defined(ESP8266)
WiFi.persistent(false); //disable saving wifi config into SDK flash area
WiFi.forceSleepBegin(); //disable AP & station by calling "WiFi.mode(WIFI_OFF)" & put modem to sleep
#endif
Serial.begin(115200);
Serial.println();
while (aht10.begin() != true) //for ESP-01 use aht10.begin(0, 2);
{
Serial.println(F("AHT1x not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
delay(5000);
}
Serial.println(F("AHT10 OK"));
//Wire.setClock(400000); //experimental I2C speed! 400KHz, default 100KHz
}
/**************************************************************************/
/*
loop()
Main loop
*/
/**************************************************************************/
void loop()
{
/* DEMO - 1, every temperature or humidity call will read 6-bytes over I2C, total 12-bytes */
Serial.println();
Serial.println(F("DEMO 1: read 12-bytes"));
ahtValue = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
Serial.print(F("Temperature...: "));
if (ahtValue != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(ahtValue);
Serial.println(F(" +-0.3C"));
}
else
{
printStatus(); //print temperature command status
if (aht10.softReset() == true) Serial.println(F("reset success")); //as the last chance to make it alive
else Serial.println(F("reset failed"));
}
delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
ahtValue = aht10.readHumidity(); //read another 6-bytes via I2C, takes 80 milliseconds
Serial.print(F("Humidity......: "));
if (ahtValue != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(ahtValue);
Serial.println(F(" +-2%"));
}
else
{
printStatus(); //print humidity command status
}
delay(2000); //measurement with high frequency leads to heating of the sensor, see NOTE
/* DEMO - 2, temperature call will read 6-bytes via I2C, humidity will use same 6-bytes */
Serial.println();
Serial.println(F("DEMO 2: read 6-byte"));
ahtValue = aht10.readTemperature(); //read 6-bytes via I2C, takes 80 milliseconds
Serial.print(F("Temperature: "));
if (ahtValue != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(ahtValue);
Serial.println(F(" +-0.3C"));
}
else
{
printStatus(); //print temperature command status
}
ahtValue = aht10.readHumidity(AHTXX_USE_READ_DATA); //use 6-bytes from temperature reading, takes zero milliseconds!!!
Serial.print(F("Humidity...: "));
if (ahtValue != AHTXX_ERROR) //AHTXX_ERROR = 255, library returns 255 if error occurs
{
Serial.print(ahtValue);
Serial.println(F(" +-2%"));
}
else
{
printStatus(); //print temperature command status not humidity!!! RH measurement use same 6-bytes from T measurement
}
delay(10000); //recomended polling frequency 8sec..30sec
}
/**************************************************************************/
/*
printStatus()
Print last command status
*/
/**************************************************************************/
void printStatus()
{
switch (aht10.getStatus())
{
case AHTXX_NO_ERROR:
Serial.println(F("no error"));
break;
case AHTXX_BUSY_ERROR:
Serial.println(F("sensor busy, increase polling time"));
break;
case AHTXX_ACK_ERROR:
Serial.println(F("sensor didn't return ACK, not connected, broken, long wires (reduce speed), bus locked by slave (increase stretch limit)"));
break;
case AHTXX_DATA_ERROR:
Serial.println(F("received data smaller than expected, not connected, broken, long wires (reduce speed), bus locked by slave (increase stretch limit)"));
break;
case AHTXX_CRC8_ERROR:
Serial.println(F("computed CRC8 not match received CRC8, this feature supported only by AHT2x sensors"));
break;
default:
Serial.println(F("unknown status"));
break;
}
}