forked from adafruit/Adafruit_HTU21DF_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_HTU21DF.cpp
140 lines (106 loc) · 3.38 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
/***************************************************
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
****************************************************/
/***************************************************
Adapted by Michael Siegel @pomplesiegel on 9/15/2015 for
use on Particle (Spark) Core and Photon
****************************************************/
#include "Adafruit_HTU21DF.h"
//empty constructor
Adafruit_HTU21DF::Adafruit_HTU21DF()
{
}
//sends initialization signal to HTU21D-F IC
//Returns back true if it receives the expected response from the IC
//Wire.begin() must be called before this
bool Adafruit_HTU21DF::reset()
{
//Send reset signal to IC
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_RESET);
Wire.endTransmission();
//Delay to allow reset to digest
delay(15);
//Read from IC's register to see current state
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READREG);
Wire.endTransmission();
Wire.requestFrom(HTU21DF_I2CADDR, 1);
//Store the IC's register state
uint8_t returnValue = Wire.read();
if(returnValue != 0x2)
{
// after reset should be 0x2
Serial.println("HTU21DF: reset() return wrong: " + String(returnValue, HEX) );
return false;
}
return true; //otherwise, we're ok
}
//Reads temperature from the IC
float Adafruit_HTU21DF::readTemperature()
{
//Begin transmission to IC
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READTEMP);
Wire.endTransmission();
// delay(50); // possible delay
Wire.requestFrom(HTU21DF_I2CADDR, 3);
//clear our counter
numCommAttempts = 1;
//to see if we're waiting.
while ( !Wire.available() && numCommAttempts < 5 )
{
delay(10);
Serial.println("Temp still not available...");
numCommAttempts++; //increment counter
}
uint16_t t = Wire.read();
t <<= 8;
t |= Wire.read();
//Unused CRC
Wire.read();
//assign to float for manipulation
temperature = t;
// (t * 175.72 / 65536) - 46.85
temperature = (temperature * 0.00268127441406) - 46.85;
return temperature;
}
//Reads humidity from the IC
float Adafruit_HTU21DF::readHumidity()
{
//Begin transmission to IC
Wire.beginTransmission(HTU21DF_I2CADDR);
Wire.write(HTU21DF_READHUM);
Wire.endTransmission();
// delay(50); // possible delay
Wire.requestFrom(HTU21DF_I2CADDR, 3);
//clear our counter
numCommAttempts = 1;
//to see if we're waiting.
while ( !Wire.available() && numCommAttempts < 5 )
{
delay(10);
Serial.println("Humidity still not available...");
numCommAttempts++; //increment counter
}
uint16_t h = Wire.read();
h <<= 8;
h |= Wire.read();
//Unused CRC
Wire.read();
//assign to float for manipulation
humidity = h;
// (h * 125 / 65536) - 6
humidity = (humidity * 0.00190734863281) - 6;
return humidity;
}
/*********************************************************************/