Skip to content

Commit 02d63b3

Browse files
committed
0.2.0 SHT2x
1 parent 69ac9d5 commit 02d63b3

File tree

7 files changed

+261
-42
lines changed

7 files changed

+261
-42
lines changed

libraries/SHT2x/README.md

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ for platforms with multiple I2C buses. **begin()** calls **reset()** which can t
6464
- **bool read()** Reads both the temperature and humidity.
6565
Initial release has a blocking delay.
6666
- **bool isConnected()** check if sensor is reachable over I2C. Returns false if not connected.
67-
- **uint16_t getStatus()** returns a 2 bit status. See below.
67+
- **uint16_t getStatus()** returns a 2 bit status. See Status fields below.
6868
- **uint32_t lastRead()** in milliSeconds since start of program.
6969
- **bool reset()** resets the sensor, soft reset, no hard reset supported.
7070
- **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it.
@@ -76,6 +76,8 @@ Note that the temperature and humidity values are recalculated on every call to
7676
If you're worried about the extra cycles, you should make sure to cache these values or only request them
7777
after you've performed a new **read()**.
7878

79+
Note: The raw temperature and raw humidity are ideal to minimize storage or to minimize communication.
80+
7981

8082
### Error interface
8183

@@ -125,9 +127,11 @@ Returns false if fails, setting error to **SHT2x_ERR_HEATER_OFF**.
125127

126128
### Electronic ID
127129

128-
- **uint32_t getEIDA()** not tested yet.
129-
- **uint32_t getEIDB()** not tested yet.
130-
- **uint8_t getFirmwareVersion()** not tested yet.
130+
To be tested.
131+
132+
- **uint32_t getEIDA()** returns unique ID part A.
133+
- **uint32_t getEIDB()** returns unique ID part B.
134+
- **uint8_t getFirmwareVersion()** returns firmware version.
131135

132136

133137
### Status fields
@@ -142,29 +146,70 @@ From HTU20 datasheet
142146
| 11 | 3 | closed circuit |
143147

144148

145-
## Operation
149+
### Resolution
150+
151+
**Warning experimental**
152+
- needs more testing as results are not in line with the datasheet.
153+
- only tested on a HTUxx sensor.
154+
- tested with **SHT2X_resolution.ino**
155+
156+
- **void setResolution(uint8_t res)** res = 0..3, other values return false.
157+
- **uint8_t getResolution()** returns resolution set 0..3.
158+
159+
160+
Datasheet SHT20 Table 8: (resolution)
161+
162+
| RES | Humidity | Temperature |
163+
|:-----:|:---------:|:-----------:|
164+
| 0 | 12 bit | 14 bit |
165+
| 1 | 08 bit | 12 bit |
166+
| 2 | 10 bit | 13 bit |
167+
| 3 | 11 bit | 11 bit |
168+
169+
Datasheet SHT20 Table 7: (timing) and results of real measurements.
170+
( https://github.com/RobTillaart/SHT2x/pull/11 )
171+
172+
| RES | HUM | TEMP | TOTAL | REAL |
173+
|:-----:|:-----:|:------:|:-------:|:------:|
174+
| 0 | 29 | 85 | 114 | 116 |
175+
| 1 | 4 | 22 | 26 | 113 |
176+
| 2 | 9 | 43 | 52 | 084 |
177+
| 3 | 15 | 11 | 26 | 102 |
178+
179+
Timing in milliseconds.
146180

147-
See examples
181+
182+
183+
### Battery
184+
185+
- **bool batteryOK()** returns true if VCC > 2.5 Volts.
148186

149187

150188
## Future
151189

152-
- test
190+
- test test test
153191
- get hardware
192+
- investigate resolution anomalies
154193
- improve documentation
155-
- add **getSerialNumber()**
156-
**getEIDA()** and **getEIDB()** covers this
194+
- fix TODO in code (.cpp and .h)
195+
- add examples
196+
- test resolutions
197+
- performance different resolutions
198+
- test battery
199+
- update unit tests
200+
201+
202+
#### 0.3.0
203+
204+
- add crc8 check (need sensor)
157205
- improve error handling (all code paths)
158206
- investigate blocking delay() in read
159-
- optimize... Q: need async interface?
160-
- add crc8 check
161-
- fix TODO in code (4 in CPP)
207+
- **ASYNC** NO HOLD call to read T or H
208+
- **void requestTemperature()** ==> **void readTemperature()**
209+
- **void requestHumidity()** ==> **void readHumidity()**
162210

211+
#### wont
163212

164-
### 0.2.0
213+
- add **getSerialNumber()**
214+
**getEIDA()** and **getEIDB()** covers this
165215

166-
- add **setResolution()** and **getResolution()**
167-
- write user register E6
168-
- read user register R7
169-
(note table 8 + FIgure 16 code)
170-
- add **getBatteryStatus()**

libraries/SHT2x/SHT2x.cpp

Lines changed: 100 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT2x.cpp
33
// AUTHOR: Rob Tillaart, Viktor Balint
4-
// VERSION: 0.1.4
4+
// VERSION: 0.2.0
55
// DATE: 2021-09-25
66
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
77
// URL: https://github.com/RobTillaart/SHT2x
@@ -17,6 +17,12 @@
1717
// 0.1.3 2021-12-28 update library.json, license, minor edits
1818
// 0.1.4 2022-06-21 Fix #9 ESP32 wire.begin()
1919
// Fix getEIDB() bug.
20+
//
21+
// 0.2.0 2022-07-10 Fix #11 RawTemp + rawHum (kudos to theandy94)
22+
// add experimental getResolution() + setResolution()
23+
// adjust read delay
24+
// add experimental batteryOK()
25+
// add CRC in read() - no fail.
2026

2127

2228
#include "SHT2x.h"
@@ -34,6 +40,11 @@
3440
#define SHT2x_ADDRESS 0x40
3541

3642

43+
#define SHT2x_USRREG_RESOLUTION 0x81
44+
#define SHT2x_USRREG_BATTERY 0x20
45+
#define SHT2x_USRREG_HEATER 0x04
46+
47+
3748
SHT2x::SHT2x()
3849
{
3950
_lastRead = 0;
@@ -45,6 +56,7 @@ SHT2x::SHT2x()
4556
_heaterOn = false;
4657
_error = SHT2x_OK;
4758
_status = 0;
59+
_resolution = 0;
4860
}
4961

5062

@@ -86,40 +98,56 @@ bool SHT2x::read()
8698

8799
// TEMPERATURE
88100
writeCmd(SHT2x_GET_TEMPERATURE_NO_HOLD);
89-
delay(70);
101+
// table 7
102+
if (_resolution == 3) delay(11); // 11 bit
103+
else if (_resolution == 1) delay(22); // 12 bit
104+
else if (_resolution == 2) delay(43); // 13 bit
105+
else delay(85); // 14 bit
106+
90107
if (readBytes(3, (uint8_t*) &buffer[0], 90) == false)
91108
{
92109
_error = SHT2x_ERR_READBYTES;
93110
return false;
94111
}
95-
// TODO CRC8 check
96-
// error = SHT2x_ERR_CRC_TEMP;
97-
_rawTemperature = buffer[0] << 8;
112+
if (crc8(buffer, 2) != buffer[2])
113+
{
114+
_error = SHT2x_ERR_CRC_TEMP;
115+
// return false; // do not fail yet
116+
}
117+
_rawTemperature = buffer[0] << 8;
98118
_rawTemperature += buffer[1];
99119
_rawTemperature &= 0xFFFC;
100120

101121
_status = buffer[1] & 0x0003;
102-
if (_status == 0xFF) // TODO != 0x01
122+
if (_status == 0xFF) // TODO != 0x01 (need HW to test)
103123
{
104124
_error = SHT2x_ERR_READBYTES;
105125
return false;
106126
}
107127

108128
// HUMIDITY
109129
writeCmd(SHT2x_GET_HUMIDITY_NO_HOLD);
110-
delay(30);
130+
// table 7
131+
if (_resolution == 1) delay(4); // 8 bit
132+
else if (_resolution == 2) delay(9); // 10 bit
133+
else if (_resolution == 3) delay(15); // 11 bit
134+
else delay(29); // 12 bit
135+
111136
if (readBytes(3, (uint8_t*) &buffer[0], 30) == false)
112137
{
113138
return false;
114139
}
115-
// TODO CRC8 check
116-
// _error = SHT2x_ERR_CRC_HUM;
117-
_rawHumidity = buffer[0] << 8;
140+
if (crc8(buffer, 2) != buffer[2])
141+
{
142+
_error = SHT2x_ERR_CRC_HUM;
143+
// return false; // do not fail yet
144+
}
145+
_rawHumidity = buffer[0] << 8;
118146
_rawHumidity += buffer[1];
119-
_rawHumidity &= 0xFFFC;
147+
_rawHumidity &= 0xFFFC; // TODO is this mask OK? as humidity is max 12 bit..
120148

121149
_status = buffer[1] & 0x0003;
122-
if (_status == 0xFF) // TODO != 0x02
150+
if (_status == 0xFF) // TODO != 0x02 (need HW to test)
123151
{
124152
_error = SHT2x_ERR_READBYTES;
125153
return false;
@@ -148,8 +176,7 @@ float SHT2x::getHumidity()
148176
bool SHT2x::reset()
149177
{
150178
bool b = writeCmd(SHT2x_SOFT_RESET);
151-
if (b == false) return false;
152-
return true;
179+
return b;
153180
}
154181

155182

@@ -184,7 +211,9 @@ bool SHT2x::heatOn()
184211
_error = SHT2x_ERR_READBYTES;
185212
return false;
186213
}
187-
userReg |= 0x04; // HEAT BIT ON
214+
215+
// HEAT BIT ON
216+
userReg |= SHT2x_USRREG_HEATER;
188217

189218
if (writeCmd(SHT2x_READ_USER_REGISTER, userReg) == false)
190219
{
@@ -206,7 +235,10 @@ bool SHT2x::heatOff()
206235
_error = SHT2x_ERR_READBYTES;
207236
return false;
208237
}
209-
userReg &= ~0x04; // HEAT BIT OFF
238+
239+
// HEAT BIT OFF
240+
userReg &= ~SHT2x_USRREG_HEATER;
241+
210242
if (writeCmd(SHT2x_READ_USER_REGISTER, userReg) == false)
211243
{
212244
_error = SHT2x_ERR_HEATER_OFF; // can be serious!
@@ -338,15 +370,65 @@ uint8_t SHT2x::getFirmwareVersion()
338370
}
339371

340372

373+
bool SHT2x::setResolution(uint8_t res)
374+
{
375+
if (res > 3) return false;
376+
377+
uint8_t userReg = 0x00;
378+
writeCmd(SHT2x_READ_USER_REGISTER);
379+
if (readBytes(1, (uint8_t *) &userReg, 5) == false)
380+
{
381+
_error = SHT2x_ERR_READBYTES;
382+
return false;
383+
}
384+
385+
// clear old resolution and set new
386+
userReg &= ~SHT2x_USRREG_RESOLUTION;
387+
// resolution is bit 7 and bit 0.
388+
userReg |= ((res & 0x02) << 6);
389+
userReg |= (res & 0x01);
390+
391+
if (writeCmd(SHT2x_READ_USER_REGISTER, userReg) == false)
392+
{
393+
_error = SHT2x_ERR_RESOLUTION;
394+
return false;
395+
}
396+
_resolution = res;
397+
return true;
398+
}
399+
400+
401+
uint8_t SHT2x::getResolution()
402+
{
403+
return _resolution;
404+
};
405+
406+
407+
bool SHT2x::batteryOK()
408+
{
409+
uint8_t userReg = 0x00;
410+
writeCmd(SHT2x_READ_USER_REGISTER);
411+
if (readBytes(1, (uint8_t *) &userReg, 5) == false)
412+
{
413+
_error = SHT2x_ERR_READBYTES;
414+
return false;
415+
}
416+
return (userReg & SHT2x_USRREG_BATTERY) == 0;
417+
}
418+
419+
420+
421+
341422
//////////////////////////////////////////////////////////
342423
//
343424
// PRIVATE
344425
//
345426
uint8_t SHT2x::crc8(const uint8_t *data, uint8_t len)
346427
{
347428
// CRC-8 formula from page 14 of SHT spec pdf
348-
const uint8_t POLY(0x31);
349-
uint8_t crc(0xFF);
429+
// Sensirion_Humidity_Sensors_SHT2x_CRC_Calculation.pdf
430+
const uint8_t POLY = 0x31;
431+
uint8_t crc = 0x00;
350432

351433
for (uint8_t j = len; j; --j)
352434
{

0 commit comments

Comments
 (0)