-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
SHT2x.h
280 lines (214 loc) · 5.77 KB
/
SHT2x.h
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#pragma once
//
// FILE: SHT2x.h
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
// VERSION: 0.5.0
// DATE: 2023-11-25
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
// URL: https://github.com/RobTillaart/SHT2x
//
#include "Arduino.h"
#include "Wire.h"
#define SHT2x_LIB_VERSION (F("0.5.0"))
// fields getStatus
#define SHT2x_STATUS_OPEN_CIRCUIT 0x00
#define SHT2x_STATUS_TEMPERATURE 0x01
#define SHT2x_STATUS_HUMIDITY 0x02
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
// error codes
// kept in sync with SHT31 library
#define SHT2x_OK 0x00
#define SHT2x_ERR_WRITECMD 0x81
#define SHT2x_ERR_READBYTES 0x82
#define SHT2x_ERR_HEATER_OFF 0x83
#define SHT2x_ERR_NOT_CONNECT 0x84
#define SHT2x_ERR_CRC_TEMP 0x85
#define SHT2x_ERR_CRC_HUM 0x86
#define SHT2x_ERR_CRC_STATUS 0x87 // not used
#define SHT2x_ERR_HEATER_COOLDOWN 0x88
#define SHT2x_ERR_HEATER_ON 0x89
// 0.2.0
#define SHT2x_ERR_RESOLUTION 0x8A
// Request types
#define SHT2x_REQ_NONE 0x00
#define SHT2x_REQ_TEMPERATURE 0x01
#define SHT2x_REQ_HUMIDITY 0x02
class SHT2x
{
public:
SHT2x(TwoWire *wire = &Wire);
bool begin();
// check sensor is reachable over I2C
bool isConnected();
/////////////////////////////////////////////////////////
//
// TEMPERATURE AND HUMIDTY
//
// read must be called before calling getTemperature / getHumidity
bool read();
float getTemperature();
float getHumidity();
uint16_t getRawTemperature();
uint16_t getRawHumidity();
// might take up to 15 milliseconds.
bool reset();
// from datasheet HTU20
//
// | bits | value | meaning |
// |:------:|:------:|:--------------------|
// | 00 | 0 | open circuit |
// | 01 | 1 | temperature reading |
// | 10 | 2 | humidity reading |
// | 11 | 3 | closed circuit |
//
uint8_t getStatus();
// lastRead is in milliSeconds since start
uint32_t lastRead();
/////////////////////////////////////////////////////////
//
// HEATER
//
// do not use heater for long periods,
// use it for max 3 minutes to heat up
// and let it cool down at least 3 minutes.
void setHeatTimeout(uint8_t seconds);
uint8_t getHeatTimeout();
bool heatOn();
bool heatOff();
bool isHeaterOn(); // is the sensor still heating up?
bool setHeaterLevel(uint8_t level); // level = 0..15
bool getHeaterLevel(uint8_t & level); // 0..15
// reading clears error flag
int getError();
/////////////////////////////////////////////////////////
//
// Electronic Identification Code
//
// Sensirion_Humidity_SHT2x_Electronic_Identification_Code_V1.1.pdf
// Electronic ID bytes
uint32_t getEIDA();
uint32_t getEIDB();
uint8_t getFirmwareVersion();
/////////////////////////////////////////////////////////
//
// RESOLUTION
//
// experimental 0.2.0 - needs testing.
// table 8 SHT20 datasheet
// table 7 shows different timing per resolution
// every level is roughly factor 2 in time.
// RES HUM TEMP
// 0 12 bit 14 bit
// 1 08 bit 12 bit
// 2 10 bit 13 bit
// 3 11 bit 11 bit
// 4..255 returns false
bool setResolution(uint8_t res = 0);
// returns RES set (cached value)
uint8_t getResolution();
/////////////////////////////////////////////////////////
//
// ASYNCHRONOUS INTERFACE (experimental)
//
bool requestTemperature();
bool requestHumidity();
bool reqTempReady();
bool reqHumReady();
bool requestReady();
bool readTemperature();
bool readHumidity();
uint32_t lastRequest();
uint8_t getRequestType();
/////////////////////////////////////////////////////////
//
// OTHER
//
bool batteryOK();
protected:
uint8_t crc8(const uint8_t *data, uint8_t len);
bool writeCmd(uint8_t cmd);
bool writeCmd(uint8_t cmd, uint8_t value);
bool readBytes(uint8_t n, uint8_t *val, uint8_t maxDuration);
/** can be called after requesting temperature or humidity */
bool readCachedTemperature();
TwoWire* _wire;
uint32_t _lastRead;
// for async interface
uint32_t _lastRequest;
// 0 = none 1 = temp 2 = hum
uint8_t _requestType;
uint8_t _heatTimeout; // seconds
uint32_t _heaterStart;
uint32_t _heaterStop;
bool _heaterOn;
uint16_t _rawHumidity;
uint16_t _rawTemperature;
uint8_t _status;
uint8_t _error;
uint8_t _resolution;
};
////////////////////////////////////////////////////////
//
// DERIVED SHT classes
//
class SHT20 : public SHT2x
{
public:
SHT20(TwoWire *wire = &Wire);
};
class SHT21 : public SHT2x
{
public:
SHT21(TwoWire *wire = &Wire);
};
class SHT25 : public SHT2x
{
public:
SHT25(TwoWire *wire = &Wire);
};
////////////////////////////////////////////////////////
//
// DERIVED HTU classes
//
class HTU20 : public SHT2x
{
public:
HTU20(TwoWire *wire = &Wire);
};
class HTU21 : public SHT2x
{
public:
HTU21(TwoWire *wire = &Wire);
};
////////////////////////////////////////////////////////
//
// DERIVED Si70xx classes
//
class Si7013 : public SHT2x
{
public:
Si7013(TwoWire *wire = &Wire);
using SHT2x::readCachedTemperature;
};
class Si7020 : public SHT2x
{
public:
Si7020(TwoWire *wire = &Wire);
using SHT2x::readCachedTemperature;
};
class Si7021 : public SHT2x
{
public:
Si7021(TwoWire *wire = &Wire);
using SHT2x::readCachedTemperature;
};
////////////////////////////////////////////////////////
//
// DERIVED GY21 classes
//
class GY21 : public SHT2x
{
public:
GY21(TwoWire *wire = &Wire);
};
// -- END OF FILE --