forked from adafruit/Adafruit_HTU21DF_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Adafruit_HTU21DF.cpp
144 lines (116 loc) · 3.55 KB
/
Adafruit_HTU21DF.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
/***************************************************
This is a library for the HTU21DF Humidity & Temp Sensor
Designed specifically to work with the HTU21DF sensor from Adafruit
----> https://www.adafruit.com/products/1899
These displays use I2C to communicate, 2 pins are required to
interface
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Limor Fried/Ladyada for Adafruit Industries.
BSD license, all text above must be included in any redistribution
****************************************************/
#include "Adafruit_HTU21DF.h"
#if defined(__AVR__)
#include <util/delay.h>
#endif
/**
* Constructor for the HTU21DF driver.
*/
Adafruit_HTU21DF::Adafruit_HTU21DF()
{
/* Assign default values to internal tracking variables. */
_last_humidity = 0.0f;
_last_temp = 0.0f;
}
/**
* Initialises the I2C transport, and configures the IC for normal operation.
*
* @return true (1) if the device was successfully initialised, otherwise
* false (0).
*/
boolean Adafruit_HTU21DF::begin(void)
{
Wire.begin();
reset();
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READREG);
Wire.endTransmission();
Wire.requestFrom(HTU21DF_I2CADDR, 1);
return (Wire.read() == 0x2); // after reset should be 0x2
}
/**
* Sends a 'reset' request to the HTU21DF, followed by a 15ms delay.
*/
void Adafruit_HTU21DF::reset(void)
{
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_RESET);
Wire.endTransmission();
delay(15);
}
/**
* Performs a single temperature conversion in degrees Celsius.
*
* @return a single-precision (32-bit) float value indicating the measured
* temperature in degrees Celsius.
*/
float Adafruit_HTU21DF::readTemperature(void)
{
// OK lets ready!
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READTEMP);
Wire.endTransmission();
delay(50); // add delay between request and actual read!
uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);
/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
}
/* Read 16 bits of data, dropping the last two status bits. */
uint16_t t = Wire.read();
t <<= 8;
t |= Wire.read() & 0b11111100;
uint8_t crc = Wire.read();
(void)crc;
float temp = t;
temp *= 175.72f;
temp /= 65536.0f;
temp -= 46.85f;
/* Track the value internally in case we need to access it later. */
_last_temp = temp;
return temp;
}
/**
* Performs a single relative humidity conversion.
*
* @return A single-precision (32-bit) float value indicating the relative
* humidity in percent (0..100.0%).
*/
float Adafruit_HTU21DF::readHumidity(void) {
/* Prepare the I2C request. */
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READHUM);
Wire.endTransmission();
/* Wait a bit for the conversion to complete. */
delay(50);
/* Read the conversion results. */
uint8_t count = Wire.requestFrom(HTU21DF_I2CADDR, 3);
/* Make sure we got 3 bytes back. */
if (count != 3) {
return 0.0f;
}
/* Read 16 bits of data, dropping the last two status bits. */
uint16_t h = Wire.read();
h <<= 8;
h |= Wire.read() & 0b11111100;
uint8_t crc = Wire.read();
(void)crc;
float hum = h;
hum *= 125.0f;
hum /= 65536.0f;
hum -= 6.0f;
/* Track the value internally in case we need to access it later. */
_last_humidity = hum;
return hum;
}