-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSparkFun_Qwiic_Humidity_AHT20.cpp
264 lines (215 loc) · 6.89 KB
/
SparkFun_Qwiic_Humidity_AHT20.cpp
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
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
/****************************************************************
* SparkFun_Qwiic_Humidity_AHT20.cpp
* SparkFun Qwiic Humidity AHT20 Library Source File
* Priyanka Makin @ SparkFun Electronics
* Original Creation Date: March 25, 2020
* https://github.com/sparkfun/SparkFun_Qwiic_Humidity_AHT20_Arduino_Library
*
* Pick up a board here: https://www.sparkfun.com/products/16618
*
* This file implements all the functions of the AHT20 class.
* Functions in this library can return the humidity and temperature
* measured by the sensor. All data is communicated over I2C bus.
*
* Development environment specifics:
* IDE: Arduino 1.8.9
* Hardware Platform: Arduino Uno
* Qwiic Humidity AHT20 Version: 1.0.0
*
* This code is lemonade-ware; if you see me (or any other SparkFun
* employee) at the local, and you've found our code helpful, please
* buy us a round!
*
* Distributed as-is; no warranty is given.
***************************************************************/
#include <SparkFun_Qwiic_Humidity_AHT20.h>
/*--------------------------- Device Status ------------------------------*/
bool AHT20::begin(TwoWire &wirePort)
{
_i2cPort = &wirePort; //Grab the port the user wants to communicate on
_deviceAddress = AHT20_DEFAULT_ADDRESS; //We had hoped the AHT20 would support two addresses but it doesn't seem to
if (isConnected() == false)
return false;
//Wait 40 ms after power-on before reading temp or humidity. Datasheet pg 8
delay(40);
//Check if the calibrated bit is set. If not, init the sensor.
if (isCalibrated() == false)
{
//Send 0xBE0800
initialize();
//Immediately trigger a measurement. Send 0xAC3300
triggerMeasurement();
delay(75); //Wait for measurement to complete
uint8_t counter = 0;
while (isBusy())
{
delay(1);
if (counter++ > 100)
return (false); //Give up after 100ms
}
//This calibration sequence is not completely proven. It's not clear how and when the cal bit clears
//This seems to work but it's not easily testable
if (isCalibrated() == false)
{
return (false);
}
}
//Check that the cal bit has been set
if (isCalibrated() == false)
return false;
//Mark all datums as fresh (not read before)
sensorQueried.temperature = true;
sensorQueried.humidity = true;
return true;
}
//Ping the AHT20's I2C address
//If we get a response, we are correctly communicating with the AHT20
bool AHT20::isConnected()
{
_i2cPort->beginTransmission(_deviceAddress);
if (_i2cPort->endTransmission() == 0)
return true;
//If IC failed to respond, give it 20ms more for Power On Startup
//Datasheet pg 7
delay(20);
_i2cPort->beginTransmission(_deviceAddress);
if (_i2cPort->endTransmission() == 0)
return true;
return false;
}
/*------------------------ Measurement Helpers ---------------------------*/
uint8_t AHT20::getStatus()
{
_i2cPort->requestFrom(_deviceAddress, (uint8_t)1);
if (_i2cPort->available())
return (_i2cPort->read());
return (0);
}
//Returns the state of the cal bit in the status byte
bool AHT20::isCalibrated()
{
return (getStatus() & (1 << 3));
}
//Returns the state of the busy bit in the status byte
bool AHT20::isBusy()
{
return (getStatus() & (1 << 7));
}
bool AHT20::initialize()
{
_i2cPort->beginTransmission(_deviceAddress);
_i2cPort->write(sfe_aht20_reg_initialize);
_i2cPort->write((uint8_t)0x08);
_i2cPort->write((uint8_t)0x00);
if (_i2cPort->endTransmission() == 0)
return true;
return false;
}
bool AHT20::triggerMeasurement()
{
_i2cPort->beginTransmission(_deviceAddress);
_i2cPort->write(sfe_aht20_reg_measure);
_i2cPort->write((uint8_t)0x33);
_i2cPort->write((uint8_t)0x00);
if (_i2cPort->endTransmission() == 0)
return true;
return false;
}
//Loads the
void AHT20::readData()
{
//Clear previous data
sensorData.temperature = 0;
sensorData.humidity = 0;
if (_i2cPort->requestFrom(_deviceAddress, (uint8_t)6) > 0)
{
_i2cPort->read(); // Read and discard state
uint32_t incoming = 0;
incoming |= (uint32_t)_i2cPort->read() << (8 * 2);
incoming |= (uint32_t)_i2cPort->read() << (8 * 1);
uint8_t midByte = _i2cPort->read();
incoming |= midByte;
sensorData.humidity = incoming >> 4;
sensorData.temperature = (uint32_t)midByte << (8 * 2);
sensorData.temperature |= (uint32_t)_i2cPort->read() << (8 * 1);
sensorData.temperature |= (uint32_t)_i2cPort->read() << (8 * 0);
//Need to get rid of data in bits > 20
sensorData.temperature = sensorData.temperature & ~(0xFFF00000);
//Mark data as fresh
sensorQueried.temperature = false;
sensorQueried.humidity = false;
}
}
//Triggers a measurement if one has not been previously started, then returns false
//If measurement has been started, checks to see if complete.
//If not complete, returns false
//If complete, readData(), mark measurement as not started, return true
bool AHT20::available()
{
if (measurementStarted == false)
{
triggerMeasurement();
measurementStarted = true;
return (false);
}
if (isBusy() == true)
{
return (false);
}
readData();
measurementStarted = false;
return (true);
}
bool AHT20::softReset()
{
_i2cPort->beginTransmission(_deviceAddress);
_i2cPort->write(sfe_aht20_reg_reset);
if (_i2cPort->endTransmission() == 0)
return true;
return false;
}
/*------------------------- Make Measurements ----------------------------*/
float AHT20::getTemperature()
{
if (sensorQueried.temperature == true)
{
//We've got old data so trigger new measurement
triggerMeasurement();
delay(75); //Wait for measurement to complete
uint8_t counter = 0;
while (isBusy())
{
delay(1);
if (counter++ > 100)
return (false); //Give up after 100ms
}
readData();
}
//From datasheet pg 8
float tempCelsius = ((float)sensorData.temperature / 1048576) * 200 - 50;
//Mark data as old
sensorQueried.temperature = true;
return tempCelsius;
}
float AHT20::getHumidity()
{
if (sensorQueried.humidity == true)
{
//We've got old data so trigger new measurement
triggerMeasurement();
delay(75); //Wait for measurement to complete
uint8_t counter = 0;
while (isBusy())
{
delay(1);
if (counter++ > 100)
return (false); //Give up after 100ms
}
readData();
}
//From datasheet pg 8
float relHumidity = ((float)sensorData.humidity / 1048576) * 100;
//Mark data as old
sensorQueried.humidity = true;
return relHumidity;
}