Skip to content

Commit 83054c5

Browse files
committed
0.5.5 SHT2x
1 parent a91acf3 commit 83054c5

7 files changed

Lines changed: 187 additions & 41 deletions

File tree

libraries/SHT2x/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ 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.5] - 2026-01-30
10+
- update readme.md, Wifi section, see #42 (kudos pgtest)
11+
- add **SHT2x_AH_issue42.ino** test sketch
12+
- minor edits
13+
914
## {0.5.4] - 2026-01-20
1015
- fix #40, add getHumidityCompensated() for HTU21 device.
1116
- add example HTU21_getHumidityCompensated.ino

libraries/SHT2x/README.md

Lines changed: 94 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,33 @@ Arduino library for the SHT2x, HTU2x and Si70xx temperature and humidity sensors
1616

1717
## Description
1818

19-
This library is not tested extensively yet.
20-
It works for the Si7021 including the asynchronous interface.
21-
It should work for SHT20, SHT21 and SHT25 but these are not tested yet.
22-
The SHT2x family of sensors should work up to 400 KHz I2C.
19+
This library is to use the SHT20, SHT21 and SHT25 and compatible temperature
20+
and humidity sensors.
21+
The interface implements an asynchronous API to prevent blocking code, examples
22+
exist how this is used.
23+
For simplicity there is also a blocking read() call.
24+
25+
The library is verified for the SHT21, HTU21 and Si7021 including the asynchronous
26+
interface. It is expected to work for SHT20 and SHT25 as these are datasheet
27+
compatible but these are not tested yet.
2328

2429
Furthermore there are several other sensors that should be compatible
2530
(see table below), but have not been tested either.
2631

27-
Accuracy table
32+
Accuracy table (indicative)
2833

29-
| Sensor | Temperature | Humidity | Notes |
30-
|:---------:|:-----------:|:--------:|:--------|
31-
| SHT20 | ~0.3 | ±3.0 | |
32-
| SHT21 | ~0.3 | ±3.0 | |
33-
| SHT25 | ~0.3 | ±1.8 | |
34-
| HTU20 | | | to-do |
35-
| HTU21 | | | #40 |
36-
| Si7013 | | | to-do |
37-
| Si7020 | | | to-do |
38-
| Si7021 | ~0.3 | ~2.0 | |
39-
| GY21 | | | to-do |
34+
| Sensor | test | Temperature | Humidity | Notes |
35+
|:--------:|:----:|:-----------:|:--------:|:--------|
36+
| SHT20 | - | ±0.3 | ±3.0 |
37+
| SHT21 | Y | ±0.3 | ±3.0 |
38+
| SHT25 | - | ±0.3 | ±1.8 |
39+
| HTU20 | - | | |
40+
| HTU21 | Y | ±0.3 | ±2.0 | #40 #42
41+
| Si7013 | - | ±0.3 | ±3.0 |
42+
| Si7020 | - | ±0.3 | ±4.0 |
43+
| Si7021 | Y | ±0.3 | ±2.0 |
44+
| GY21 | - | | | often HTU21
45+
| HDC1080 | - | ±0.2 | ±2.0 | TI
4046

4147

4248
All sensors in this family of sensors have address 0x40 (64 decimal).
@@ -46,6 +52,30 @@ or one should switch sensors on/off like the select in SPI communication.
4652
Feedback as always is welcome.
4753

4854

55+
### 0.5.5 Wifi affecting accuracy
56+
57+
See https://github.com/RobTillaart/SHT2x/issues/42
58+
59+
User pgtest did a number of experiments after he saw an increased temperature
60+
and decreased humidity when the sensors he used were in close proximity (about 3 cm)
61+
of the ESP-C3 Wifi antenna. As far as understood the 2.4 GHz heats up the water
62+
molecules in the air which cause the absolute humidity to rise, and thus the relative
63+
humidity to go down. Furthermore it seems that water evaporates which causes the
64+
absolute humidity drop too, resulting in drop of relative humidity too.
65+
Possibly there are more ways the 2.4 GHz could affect the sensor, especially the amount
66+
of Wifi usage could be a factor.
67+
68+
Shortly discussed on the Arduino forum, there were additional cause e.g. RF effect on
69+
internal electronics and possible effects from a power dip when Wifi is switched on.
70+
71+
The library does not compensate for the found effect as there is no formula that calculates
72+
the delta for a given distance and/or Wifi usage. However one could use the multiMap()
73+
library to build (non-linear) corrections if needed.
74+
75+
Advice is to keep the sensor "as far" from heat sources including Wifi as possible.
76+
Also take care of good air circulation in/around the sensor.
77+
78+
4979
### 0.5.0 Breaking change
5080

5181
Version 0.5.0 introduced a breaking change.
@@ -57,10 +87,44 @@ before calling **begin()**.
5787

5888
### Related
5989

90+
- https://github.com/RobTillaart/SHT2x
91+
- https://github.com/RobTillaart/SHT2x_INT - integer math only to minimize footprint.
6092
- https://github.com/RobTillaart/SHT31
6193
- https://github.com/RobTillaart/SHT85
62-
- https://github.com/RobTillaart/tinySHT2x
63-
- https://github.com/RobTillaart/Temperature Temperature related formulas.
94+
- https://github.com/RobTillaart/tinySHT2x - specific for AVR tiny processors.
95+
- https://github.com/RobTillaart/Temperature - Temperature related formulas.
96+
- https://github.com/RobTillaart/MultiMap - non linear mapping
97+
98+
99+
### Special chars (win)
100+
101+
ALT241 = ± (plus-minus)
102+
ALT167 = º (degrees)
103+
104+
105+
## I2C
106+
107+
### Performance
108+
109+
The SHT2x family of sensors should work up to 400 KHz I2C.
110+
111+
112+
### I2C multiplexing
113+
114+
Sometimes you need to control more devices than possible with the default
115+
address range the device provides.
116+
This is possible with an I2C multiplexer e.g. TCA9548 which creates up
117+
to eight channels (think of it as I2C subnets) which can use the complete
118+
address range of the device.
119+
120+
Drawback of using a multiplexer is that it takes more administration in
121+
your code e.g. which device is on which channel.
122+
This will slow down the access, which must be taken into account when
123+
deciding which devices are on which channel.
124+
Also note that switching between channels will slow down other devices
125+
too if they are behind the multiplexer.
126+
127+
- https://github.com/RobTillaart/TCA9548
64128

65129

66130
## Interface
@@ -115,7 +179,7 @@ these bits.
115179
These functions are used for synchronous (blocking) reads of temperature and humidity.
116180

117181
- **bool read()**: Reads both temperature and humidity from the sensor.
118-
This is a blocking call; program execution will pause until the readings are complete.
182+
This is a blocking call, and program execution will pause until the readings are complete.
119183
- **float getTemperature()**: Returns the temperature in degrees Celsius (°C) based
120184
on the latest raw data acquired by `read()`.
121185
- **float getHumidity()**: Returns the relative humidity in percent (%) based on the
@@ -125,7 +189,7 @@ value for temperature directly from the sensor, as acquired by `read()`.
125189
- **uint16_t getRawHumidity()**: Returns the raw, not calibrated 16-bit integer
126190
value for humidity directly from the sensor, as acquired by `read()`.
127191

128-
**Note on `read()` and data retrieval:**
192+
**Note on `read()` and data retrieval**
129193
The `getTemperature()` and `getHumidity()` functions recalculate values from raw
130194
data on every call. If performance is critical, cache these values in your code after
131195
a `read()` instead of calling them repeatedly. Raw values are useful for minimizing
@@ -152,7 +216,7 @@ stalled while waiting for sensor data. This interface is experimental (since 0.2
152216
and may change in future versions.
153217
See discussion in [issue #16](https://github.com/RobTillaart/SHT2x/issues/16).
154218

155-
**General Workflow for Asynchronous Operations:**
219+
**General Workflow for Asynchronous Operations**
156220

157221
1. **Initiate a request:** Call `requestTemperature()` to start a temperature
158222
measurement or `requestHumidity()` to start a humidity measurement.
@@ -167,7 +231,7 @@ retrieve the humidity value. These functions then fetch the data from the sensor
167231
and update the internal raw values.
168232
You can then use `getTemperature()` or `getHumidity()` to get the processed values.
169233

170-
**Difference between synchronous `read()` and asynchronous operations:**
234+
**Difference between synchronous `read()` and asynchronous operations**
171235
- **Synchronous `read()`**: When you call `read()`, your program stops and waits
172236
for the sensor to complete both temperature and humidity measurements.
173237
This is simpler to use but can block other operations for the duration of the sensor's
@@ -178,7 +242,7 @@ retrieve the result.
178242
This avoids blocking but requires a slightly more complex program structure to
179243
periodically check the sensor's status.
180244

181-
**Asynchronous Function Descriptions:**
245+
**Asynchronous Function Descriptions**
182246

183247
- **`bool requestTemperature()`**: Initiates a request for a temperature reading from the sensor.
184248
If a previous asynchronous request (either temperature or humidity) was still pending,
@@ -220,7 +284,7 @@ The possible return values are:
220284
| 0x02 | `SHT2x_REQ_HUMIDITY` | A humidity request is currently pending or was the last one made. |
221285
| 0xFF | `SHT2x_REQ_FAIL` | A humidity request is currently pending or was the last one made. |
222286

223-
**Example Snippet (Asynchronous Reading):**
287+
**Example Snippet (Asynchronous Reading)**
224288

225289
```cpp
226290
#include "SHT2x.h"
@@ -292,7 +356,7 @@ void loop() {
292356
}
293357
```
294358

295-
**Waiting until the device is ready:**
359+
**Waiting until the device is ready**
296360

297361
These sensors typically need a bit of time to start up before they can accept commands.
298362
The SHT21, for instance, requires up to 15ms after it's been powered up.
@@ -339,7 +403,8 @@ specific sensor model; refer to the sensor datasheet.)
339403
### Electronic ID
340404

341405
Functions to retrieve the unique electronic identification code and firmware version from the sensor.
342-
(These functions need more testing and have primarily been tested with Si7021 sensors.)
406+
These functions need more testing and have primarily been tested with Si7021 sensors (low prio).
407+
They might not be supported on some "compatible" sensors.
343408

344409
- **uint32_t getEIDA()**: Returns the first part (A) of the electronic ID.
345410
- **uint32_t getEIDB()**: Returns the second part (B) of the electronic ID.
@@ -353,8 +418,8 @@ Allows adjustment of the measurement resolution for temperature and humidity.
353418
as initial results are not perfectly aligned with datasheet specifications.
354419
It has been primarily tested on HTUxx and Si7021 sensors using the `SHT2X_resolution.ino` example.
355420

356-
- **bool setResolution(uint8_t res)**: Sets the measurement resolution.
357-
`res` can be 0, 1, 2, or 3. Other values will result in the function returning `false`.
421+
- **bool setResolution(uint8_t resolution)**: Sets the measurement resolution.
422+
`resolution` can be 0, 1, 2, or 3. Other values will result in the function returning `false`.
358423
- **uint8_t getResolution()**: Returns the currently set resolution (cached value).
359424

360425
Datasheet SHT20 Table 8: Resolution settings
@@ -366,6 +431,7 @@ Datasheet SHT20 Table 8: Resolution settings
366431
| 2 | 10 bit | 13 bit |
367432
| 3 | 11 bit | 11 bit |
368433

434+
369435
Datasheet SHT20 Table 7: Measurement timing (milliseconds) vs. observed real-world measurements.
370436
(See details at https://github.com/RobTillaart/SHT2x/pull/11)
371437

libraries/SHT2x/SHT2x.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT2x.cpp
33
// AUTHOR: Rob Tillaart, Viktor Balint, JensB, morfeus02
4-
// VERSION: 0.5.4
4+
// VERSION: 0.5.5
55
// DATE: 2023-11-25
66
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
77
// URL: https://github.com/RobTillaart/SHT2x
@@ -530,9 +530,9 @@ uint8_t SHT2x::getFirmwareVersion()
530530
//
531531
// RESOLUTION
532532
//
533-
bool SHT2x::setResolution(uint8_t res)
533+
bool SHT2x::setResolution(uint8_t resolution)
534534
{
535-
if (res > 3) return false;
535+
if (resolution > 3) return false;
536536

537537
uint8_t userReg = 0x00;
538538
writeCmd(SHT2x_READ_USER_REGISTER);
@@ -545,15 +545,15 @@ bool SHT2x::setResolution(uint8_t res)
545545
// clear old resolution and set new
546546
userReg &= ~SHT2x_USRREG_RESOLUTION;
547547
// resolution is bit 7 and bit 0.
548-
userReg |= ((res & 0x02) << 6);
549-
userReg |= (res & 0x01);
548+
userReg |= ((resolution & 0x02) << 6);
549+
userReg |= (resolution & 0x01);
550550

551551
if (writeCmd(SHT2x_WRITE_USER_REGISTER, userReg) == false)
552552
{
553553
_error = SHT2x_ERR_RESOLUTION;
554554
return false;
555555
}
556-
_resolution = res;
556+
_resolution = resolution;
557557
return true;
558558
}
559559

libraries/SHT2x/SHT2x.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: SHT2x.h
44
// AUTHOR: Rob Tillaart, Viktor Balint, JensB, morfeus02
5-
// VERSION: 0.5.4
5+
// VERSION: 0.5.5
66
// DATE: 2023-11-25
77
// PURPOSE: Arduino library for the SHT2x temperature and humidity sensor
88
// URL: https://github.com/RobTillaart/SHT2x
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define SHT2x_LIB_VERSION (F("0.5.4"))
16+
#define SHT2x_LIB_VERSION (F("0.5.5"))
1717

1818

1919
// fields getStatus
@@ -122,8 +122,9 @@ class SHT2x
122122
// 1 | 08 bit | 12 bit
123123
// 2 | 10 bit | 13 bit
124124
// 3 | 11 bit | 11 bit
125-
bool setResolution(uint8_t res = 0); // Sets resolution (res = 0..3). Returns false for invalid values.
126-
uint8_t getResolution(); // Returns the currently configured resolution (cached value).
125+
bool setResolution(uint8_t resolution = 0); // Sets resolution (res = 0..3).
126+
// Returns false for invalid values.
127+
uint8_t getResolution(); // Returns the currently configured resolution (cached value).
127128

128129

129130
// Asynchronous Interface (experimental)
@@ -134,7 +135,8 @@ class SHT2x
134135

135136

136137
// Check if data is ready after an asynchronous request.
137-
// The time required for measurement depends on the configured resolution (see datasheet or implementation of reqHumReady/reqTempReady in .cpp).
138+
// The time required for measurement depends on the configured resolution
139+
// (see datasheet or implementation of reqHumReady/reqTempReady in SHT2x.cpp).
138140
bool reqTempReady(); // Returns true if temperature data is ready to be read.
139141
bool reqHumReady(); // Returns true if humidity data is ready to be read.
140142
bool requestReady(); // Returns true if either temperature or humidity data is ready.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// FILE: SHT2x_AH_issue42.ino
2+
// AUTHOR: Rob Tillaart
3+
// PURPOSE: test for issue 42 - effect of Wifi on RH and T
4+
// URL: https://github.com/RobTillaart/SHT2x/issues/42
5+
// URL: https://github.com/RobTillaart/Temperature
6+
//
7+
//
8+
9+
10+
#include "Arduino.h"
11+
12+
13+
// from Temperature Library
14+
float absoluteHumidity(float Celsius, float relHumidity)
15+
{
16+
float TC = Celsius;
17+
float AH = (2.1674 * 6.112) * relHumidity;
18+
AH *= exp((17.67 * TC) / (243.5 + TC));
19+
AH /= (273.15 + TC);
20+
return AH;
21+
}
22+
23+
24+
void test(float Celsius, float relHumidity)
25+
{
26+
Serial.print(Celsius);
27+
Serial.print("\t");
28+
Serial.print(relHumidity);
29+
Serial.print("\t");
30+
Serial.println(absoluteHumidity(Celsius, relHumidity));
31+
}
32+
33+
34+
void setup()
35+
{
36+
Serial.begin(115200);
37+
while (!Serial);
38+
Serial.println(__FILE__);
39+
40+
// Test 1-2
41+
Serial.println("03 cm, Temp = 23.6, Hum = 34.4");
42+
Serial.println(absoluteHumidity(23.6, 34.4));
43+
Serial.println("15 cm, Temp = 23.4, Hum = 32.2");
44+
Serial.println(absoluteHumidity(23.4, 32.2));
45+
Serial.println();
46+
47+
// Test 3-4
48+
Serial.println("03 cm, Temp = 25.6, Hum = 24.0");
49+
Serial.println(absoluteHumidity(25.6, 24.0));
50+
Serial.println("15 cm, Temp = 22.6, Hum = 28.3");
51+
Serial.println(absoluteHumidity(22.6, 28.3));
52+
Serial.println();
53+
54+
Serial.println("\n\nStart");
55+
test(23.3, 30.3);
56+
test(23.3, 30.3);
57+
test(23.3, 30.2);
58+
test(23.3, 30.0);
59+
test(23.3, 29.8);
60+
Serial.println("10 min");
61+
test(26.4, 21.9);
62+
Serial.println("20 min");
63+
test(28.4, 18.6);
64+
Serial.println("30 min");
65+
test(29.1, 17.4);
66+
}
67+
68+
void loop()
69+
{
70+
}
71+
72+
73+
// -- END OF FILE --

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.4",
27+
"version": "0.5.5",
2828
"license": "MIT",
2929
"frameworks": "*",
3030
"platforms": "*",

0 commit comments

Comments
 (0)