Skip to content

Commit 1c5c183

Browse files
committed
0.5.2 SHT2x
1 parent 584380b commit 1c5c183

File tree

8 files changed

+71
-48
lines changed

8 files changed

+71
-48
lines changed

libraries/SHT2x/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.5.2] - 2025-06-14
10+
- PR #34 - revert status checking
11+
- improve some error handling
12+
913
## [0.5.1] - 2025-05-27
1014
- PR #32 - update readme.md, comments (Kudos to morfeus02)
1115
- add 2 examples

libraries/SHT2x/README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Arduino library for the SHT2x, HTU2x and Si70xx temperature and humidity sensors
1717
## Description
1818

1919
This library is not tested extensively yet.
20-
It works for the Si7021 incl. the asynchronous interface.
20+
It works for the Si7021 including the asynchronous interface.
2121
It should work for SHT20, SHT21 and SHT25 but these are not tested yet.
2222
The SHT2x family of sensors should work up to 400 KHz I2C.
2323

@@ -102,8 +102,8 @@ These functions are used for synchronous (blocking) reads of temperature and hum
102102
- **bool read()**: Reads both temperature and humidity from the sensor. This is a blocking call; program execution will pause until the readings are complete.
103103
- **float getTemperature()**: Returns the temperature in degrees Celsius (°C) based on the latest raw data acquired by `read()`.
104104
- **float getHumidity()**: Returns the relative humidity in percent (%) based on the latest raw data acquired by `read()`.
105-
- **uint16_t getRawTemperature()**: Returns the raw, uncalibrated 16-bit integer value for temperature directly from the sensor, as acquired by `read()`.
106-
- **uint16_t getRawHumidity()**: Returns the raw, uncalibrated 16-bit integer value for humidity directly from the sensor, as acquired by `read()`.
105+
- **uint16_t getRawTemperature()**: Returns the raw, not calibrated 16-bit integer value for temperature directly from the sensor, as acquired by `read()`.
106+
- **uint16_t getRawHumidity()**: Returns the raw, not calibrated 16-bit integer value for humidity directly from the sensor, as acquired by `read()`.
107107

108108
**Note on `read()` and data retrieval:**
109109
The `getTemperature()` and `getHumidity()` functions recalculate values from raw data on every call. If performance is critical, cache these values in your code after a `read()` instead of calling them repeatedly. Raw values are useful for minimizing storage or communication overhead.
@@ -138,11 +138,12 @@ The asynchronous interface allows you to initiate a temperature or humidity read
138138
- **`uint32_t lastRequest()`**: Returns the timestamp (in milliseconds, based on `millis()`) of when the last asynchronous request (`requestTemperature()` or `requestHumidity()`) was made. This can be useful for implementing timeouts for asynchronous operations.
139139
- **`uint8_t getRequestType()`**: Returns the type of the currently pending or last completed asynchronous request. This helps in determining what kind of data was requested or is ready. The possible return values are:
140140

141-
| Value | Symbolic | Description |
142-
|:------:|:----------------------|:------------------------------|
141+
| Value | Symbolic | Description |
142+
|:------:|:------------------------|:--------------|
143143
| 0x00 | `SHT2x_REQ_NONE` | No request pending or last request completed/cleared. |
144144
| 0x01 | `SHT2x_REQ_TEMPERATURE` | A temperature request is currently pending or was the last one made. |
145145
| 0x02 | `SHT2x_REQ_HUMIDITY` | A humidity request is currently pending or was the last one made. |
146+
| 0xFF | `SHT2x_REQ_FAIL` | A humidity request is currently pending or was the last one made. |
146147

147148
**Example Snippet (Asynchronous Reading):**
148149

libraries/SHT2x/SHT2x.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT2x.cpp
3-
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
4-
// VERSION: 0.5.1
3+
// AUTHOR: Rob Tillaart, Viktor Balint, JensB, morfeus02
4+
// VERSION: 0.5.2
55
// DATE: 2023-11-25
66
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
77
// URL: https://github.com/RobTillaart/SHT2x
@@ -24,7 +24,7 @@
2424

2525

2626
#define SHT2x_USRREG_RESOLUTION 0x81
27-
#define SHT2x_USRREG_BATTERY 0x20
27+
#define SHT2x_USRREG_BATTERY 0x40 // sht21 datasheet
2828
#define SHT2x_USRREG_HEATER 0x04
2929

3030

@@ -97,7 +97,11 @@ bool SHT2x::read()
9797
//
9898
bool SHT2x::requestTemperature()
9999
{
100-
writeCmd(SHT2x_GET_TEMPERATURE_NO_HOLD);
100+
if (! writeCmd(SHT2x_GET_TEMPERATURE_NO_HOLD))
101+
{
102+
_requestType = SHT2x_REQ_FAIL;
103+
return false; // error is registered in writeCmd()
104+
}
101105
_lastRequest = millis();
102106
_requestType = SHT2x_REQ_TEMPERATURE;
103107
return true;
@@ -106,7 +110,11 @@ bool SHT2x::requestTemperature()
106110

107111
bool SHT2x::requestHumidity()
108112
{
109-
writeCmd(SHT2x_GET_HUMIDITY_NO_HOLD);
113+
if (! writeCmd(SHT2x_GET_HUMIDITY_NO_HOLD))
114+
{
115+
_requestType = SHT2x_REQ_FAIL;
116+
return false;
117+
}
110118
_lastRequest = millis();
111119
_requestType = SHT2x_REQ_HUMIDITY;
112120
return true;
@@ -170,19 +178,23 @@ bool SHT2x::readTemperature()
170178
}
171179
_rawTemperature = buffer[0] << 8;
172180
_rawTemperature += buffer[1];
173-
_rawTemperature &= 0xFFFC; // Clear status bits (last two bits)
181+
// Clear status bits (last two bits)
182+
_rawTemperature &= 0xFFFC;
174183

175-
// clear requestType, marking this async operation as complete
184+
// Clear requestType, marking this async operation as complete
176185
_requestType = SHT2x_REQ_NONE;
177186

178-
_status = buffer[1] & 0x03; // Extract status bits
187+
// _status = buffer[1] & 0x02; // Extract status bit (bit 0 not used)
179188
// After a temperature read, the status bits should indicate "temperature reading" (0x01).
180189
// If not, it implies a read error or unexpected sensor state.
181-
if (_status != SHT2x_STATUS_TEMPERATURE)
182-
{
183-
_error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
184-
return false;
185-
}
190+
//if (_status != SHT2x_STATUS_TEMPERATURE)
191+
//{
192+
// _error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
193+
// return false;
194+
//}
195+
196+
_error = SHT2x_OK;
197+
_lastRead = millis();
186198
return true;
187199
}
188200

@@ -209,14 +221,14 @@ bool SHT2x::readHumidity()
209221
// clear requestType, marking this async operation as complete
210222
_requestType = SHT2x_REQ_NONE;
211223

212-
_status = buffer[1] & 0x03; // Extract status bits
213-
// After a humidity read, the status bits should indicate "humidity reading" (0x02).
224+
// _status = buffer[1] & 0x02; // Extract status bit
225+
// After a humidity read, the status bit should indicate "humidity reading" (0x00).
214226
// If not, it implies a read error or unexpected sensor state.
215-
if (_status != SHT2x_STATUS_HUMIDITY)
216-
{
217-
_error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
218-
return false;
219-
}
227+
//if (_status != SHT2x_STATUS_HUMIDITY)
228+
//{
229+
// _error = SHT2x_ERR_READBYTES; // Or a more specific error e.g. SHT2x_ERR_UNEXPECTED_STATUS
230+
// return false;
231+
//}
220232

221233
_error = SHT2x_OK; // Mark as OK if all checks passed for this specific read
222234
_lastRead = millis(); // Record time of successful synchronous style read completion
@@ -229,12 +241,16 @@ bool SHT2x::readCachedTemperature()
229241
if (_error == SHT2x_OK)
230242
{
231243
writeCmd(SHT2x_GET_TEMPERATURE_FOR_HUMIDITY);
232-
uint8_t buffer[2];
233-
if (readBytes(2, (uint8_t*) &buffer[0], 10) == false)
244+
uint8_t buffer[3];
245+
if (readBytes(3, (uint8_t*) &buffer[0], 10) == false)
234246
{
235247
_error = SHT2x_ERR_READBYTES;
236248
return false;
237249
}
250+
if (crc8(buffer, 2) != buffer[2])
251+
{
252+
_error = SHT2x_ERR_CRC_TEMP;
253+
}
238254
_rawTemperature = buffer[0] << 8;
239255
_rawTemperature += buffer[1];
240256
_rawTemperature &= 0xFFFC;

libraries/SHT2x/SHT2x.h

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
//
33
// FILE: SHT2x.h
4-
// AUTHOR: Rob Tillaart, Viktor Balint, JensB
4+
// AUTHOR: Rob Tillaart, Viktor Balint, JensB, morfeus02
55
// VERSION: 0.5.1
66
// DATE: 2023-11-25
77
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
@@ -18,31 +18,32 @@
1818

1919
// fields getStatus
2020
#define SHT2x_STATUS_OPEN_CIRCUIT 0x00
21-
#define SHT2x_STATUS_TEMPERATURE 0x01
22-
#define SHT2x_STATUS_HUMIDITY 0x02
21+
#define SHT2x_STATUS_TEMPERATURE 0x00 // only bit 1
22+
#define SHT2x_STATUS_HUMIDITY 0x02 // only bit 1
2323
#define SHT2x_STATUS_CLOSED_CIRCUIT 0x03
2424

2525

2626
// Error codes
2727
// Error codes are kept somewhat in sync with SHT31 library for consistency where applicable.
28-
#define SHT2x_OK 0x00 // Default success value, no error.
29-
#define SHT2x_ERR_WRITECMD 0x81 // Error during I2C write command.
30-
#define SHT2x_ERR_READBYTES 0x82 // Error during I2C read bytes.
31-
#define SHT2x_ERR_HEATER_OFF 0x83 // Failed to switch off the internal heater.
32-
#define SHT2x_ERR_NOT_CONNECT 0x84 // Sensor not connected or does not acknowledge on the I2C bus.
33-
#define SHT2x_ERR_CRC_TEMP 0x85 // CRC check failed for the temperature reading.
34-
#define SHT2x_ERR_CRC_HUM 0x86 // CRC check failed for the humidity reading.
35-
#define SHT2x_ERR_CRC_STATUS 0x87 // CRC check failed for the status register (currently not actively used for status validation within library).
36-
#define SHT2x_ERR_HEATER_COOLDOWN 0x88 // Heater is in its mandatory cool-down period and cannot be re-enabled yet.
37-
#define SHT2x_ERR_HEATER_ON 0x89 // Failed to switch on the internal heater.
38-
#define SHT2x_ERR_RESOLUTION 0x8A // Invalid resolution parameter provided (since 0.2.0).
28+
#define SHT2x_OK 0x00 // Default success value, no error.
29+
#define SHT2x_ERR_WRITECMD 0x81 // Error during I2C write command.
30+
#define SHT2x_ERR_READBYTES 0x82 // Error during I2C read bytes.
31+
#define SHT2x_ERR_HEATER_OFF 0x83 // Failed to switch off the internal heater.
32+
#define SHT2x_ERR_NOT_CONNECT 0x84 // Sensor not connected or does not acknowledge on the I2C bus.
33+
#define SHT2x_ERR_CRC_TEMP 0x85 // CRC check failed for the temperature reading.
34+
#define SHT2x_ERR_CRC_HUM 0x86 // CRC check failed for the humidity reading.
35+
#define SHT2x_ERR_CRC_STATUS 0x87 // CRC check failed for the status register (currently not actively used for status validation within library).
36+
#define SHT2x_ERR_HEATER_COOLDOWN 0x88 // Heater is in its mandatory cool-down period and cannot be re-enabled yet.
37+
#define SHT2x_ERR_HEATER_ON 0x89 // Failed to switch on the internal heater.
38+
#define SHT2x_ERR_RESOLUTION 0x8A // Invalid resolution parameter provided (since 0.2.0).
3939
// Consider adding SHT2x_ERR_UNEXPECTED_STATUS for status check failures in readTemperature/readHumidity.
4040
// Currently, these failures reuse SHT2x_ERR_READBYTES.
4141

4242
// Asynchronous request types
43-
#define SHT2x_REQ_NONE 0x00 // No active asynchronous request.
44-
#define SHT2x_REQ_TEMPERATURE 0x01 // Asynchronous temperature request is currently active.
45-
#define SHT2x_REQ_HUMIDITY 0x02 // Asynchronous humidity request is currently active.
43+
#define SHT2x_REQ_NONE 0x00 // No active asynchronous request.
44+
#define SHT2x_REQ_TEMPERATURE 0x01 // Asynchronous temperature request is currently active.
45+
#define SHT2x_REQ_HUMIDITY 0x02 // Asynchronous humidity request is currently active.
46+
#define SHT2x_REQ_FAIL 0xFF // Asynchronous humidity request is currently active.
4647

4748

4849
class SHT2x
@@ -62,7 +63,7 @@ class SHT2x
6263

6364
// Getters for sensor values
6465
// These functions return values based on the last successful read() or asynchronous read operation.
65-
float getTemperature(); // Returns the temperature in degrees Celsius (°C).
66+
float getTemperature(); // Returns the temperature in degrees Celsius (°C).
6667
float getHumidity(); // Returns the relative humidity in percent (%).
6768
uint16_t getRawTemperature(); // Returns the raw 16-bit sensor data for temperature.
6869
uint16_t getRawHumidity(); // Returns the raw 16-bit sensor data for humidity.

libraries/SHT2x/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,5 @@ SHT2x_STATUS_CLOSED_CIRCUIT LITERAL1
7979
SHT2x_REQ_NONE LITERAL1
8080
SHT2x_REQ_TEMPERATURE LITERAL1
8181
SHT2x_REQ_HUMIDITY LITERAL1
82+
SHT2x_REQ_FAIL LITERAL1
8283

libraries/SHT2x/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"type": "git",
2525
"url": "https://github.com/RobTillaart/SHT2x.git"
2626
},
27-
"version": "0.5.1",
27+
"version": "0.5.2",
2828
"license": "MIT",
2929
"frameworks": "*",
3030
"platforms": "*",

libraries/SHT2x/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SHT2x
2-
version=0.5.1
2+
version=0.5.2
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for the I2C SHT20 SHT21 SHT25 series temperature and humidity sensor.

libraries/SHT2x/test/unit_test_001.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ unittest(test_constants_1)
6262
{
6363
fprintf(stderr, "fields getStatus\n");
6464
assertEqual(SHT2x_STATUS_OPEN_CIRCUIT , 0x00);
65-
assertEqual(SHT2x_STATUS_TEMPERATURE , 0x01);
65+
assertEqual(SHT2x_STATUS_TEMPERATURE , 0x00);
6666
assertEqual(SHT2x_STATUS_HUMIDITY , 0x02);
6767
assertEqual(SHT2x_STATUS_CLOSED_CIRCUIT, 0x03);
6868
}

0 commit comments

Comments
 (0)