Skip to content

Commit f054543

Browse files
committed
0.1.4 SHT85
1 parent 8bfec2f commit f054543

File tree

6 files changed

+120
-77
lines changed

6 files changed

+120
-77
lines changed

libraries/SHT85/README.md

Lines changed: 70 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11

22
[![Arduino CI](https://github.com/robtillaart/SHT85/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
3-
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT85.svg?maxAge=3600)](https://github.com/RobTillaart/SHT85/releases)
3+
[![JSON check](https://github.com/RobTillaart/SHT85/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/SHT85/actions/workflows/jsoncheck.yml)
4+
[![Arduino-lint](https://github.com/RobTillaart/SHT85/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/SHT85/actions/workflows/arduino-lint.yml)
45
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/SHT85/blob/master/LICENSE)
6+
[![GitHub release](https://img.shields.io/github/release/RobTillaart/SHT85.svg?maxAge=3600)](https://github.com/RobTillaart/SHT85/releases)
57

68

79
# SHT85
@@ -60,25 +62,28 @@ this is not verified yet.
6062
Need to investigate if the interface is identical?
6163
If so the libraries might be merged.
6264

65+
An elaborated library for the SHT31 sensor can be found here
66+
https://github.com/hawesg/SHT31D_Particle_Photon_ClosedCube
67+
6368

6469
## Interface
6570

6671
#### Base interface
6772

68-
- **SHT85()** constructor.
73+
- **SHT85()** constructor.
6974
- **begin(address, dataPin, clockPin)** begin function for ESP8266 & ESP32; **WARNING: not verified yet**
7075
returns false if device address is incorrect or device cannot be reset.
71-
- **begin(address, TwoWire \*wire = &Wire)** for platforms with multiple I2C busses.
72-
- **read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
76+
- **bool begin(address, TwoWire \*wire = &Wire)** for platforms with multiple I2C busses.
77+
- **bool read(bool fast = true)** blocks 4 (fast) or 15 (slow) milliseconds + actual read + math.
7378
Does read both the temperature and humidity.
74-
- **isConnected()** check sensor is reachable over I2C. Returns false if not connected.
75-
- **uint16_t readStatus()** details see datasheet and **Status fields** below
79+
- **bool isConnected()** check sensor is reachable over I2C. Returns false if not connected.
80+
- **uint16_t readStatus()** details see datasheet and **Status fields** below.
7681
- **uint32_t lastRead()** in milliSeconds since start of program.
77-
- **reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if fails.
78-
- **getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it
79-
- **getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it
80-
- **getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor
81-
- **getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor
82+
- **bool reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if fails.
83+
- **float getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it.
84+
- **float getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it.
85+
- **uint16_t getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor.
86+
- **uint16_t getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor.
8287

8388
Note that the temperature and humidity values are recalculated on every call to getHumidity() and getTemperature(). If you're worried about the extra cycles, you should make sure to cache these values or only request them after you've performed a new reading.
8489

@@ -88,67 +93,77 @@ Note that the temperature and humidity values are recalculated on every call to
8893
- **getError()** returns last set error flag and clear it.
8994
Be sure to clear the error flag by calling **getError()** before calling any command as the error flag could be from a previous command.
9095

91-
| Error | Symbolic | Description |
92-
|:----:|:----|:----|
93-
| 0x00 | SHT_OK | no error |
94-
| 0x81 | SHT_ERR_WRITECMD | I2C write failed |
95-
| 0x82 | SHT_ERR_READBYTES | I2C read failed |
96-
| 0x83 | SHT_ERR_HEATER_OFF | Could not switch off heater |
97-
| 0x84 | SHT_ERR_NOT_CONNECT | Could not connect |
98-
| 0x85 | SHT_ERR_CRC_TEMP | CRC error in temperature |
99-
| 0x86 | SHT_ERR_CRC_HUM | CRC error in humidity |
100-
| 0x87 | SHT_ERR_CRC_STATUS | CRC error in statusfield |
96+
| Error | Symbolic | Description
97+
|:-----:|:--------------------------|:----------------------------|
98+
| 0x00 | SHT_OK | no error |
99+
| 0x81 | SHT_ERR_WRITECMD | I2C write failed |
100+
| 0x82 | SHT_ERR_READBYTES | I2C read failed |
101+
| 0x83 | SHT_ERR_HEATER_OFF | Could not switch off heater |
102+
| 0x84 | SHT_ERR_NOT_CONNECT | Could not connect |
103+
| 0x85 | SHT_ERR_CRC_TEMP | CRC error in temperature |
104+
| 0x86 | SHT_ERR_CRC_HUM | CRC error in humidity |
105+
| 0x87 | SHT_ERR_CRC_STATUS | CRC error in statusfield |
106+
| 0x88 | SHT_ERR_HEATER_COOLDOWN | Heater need to cool down |
107+
| 0x88 | SHT_ERR_HEATER_ON | Could not switch on heater |
101108

102109

103110
#### Heater interface
104111

105-
Use the heater for max **180** seconds, and let it cool down an equal period of time.
112+
**WARNING:** Do not use heater for long periods.
113+
114+
Use the heater for max **180** seconds, and let it cool down **180** seconds = 3 minutes.
115+
Version 0.3.3 and up guards the cool down time by preventing switching the heater on
116+
within **180** seconds of the last switch off. Note: this guarding is not reboot persistent.
106117

107-
**WARNING:** Do not use heater for long periods.
118+
**WARNING:** The user is responsible to switch the heater off manually!
108119

109-
**WARNING:** The user is responsible to switch the heater off manually!
110120
The class does **NOT** do this automatically.
121+
Switch off the heater by directly calling **heatOff()** or indirectly by calling **isHeaterOn()**.
111122

112-
- **setHeatTimeout(uint8_t seconds)** Set the time out of the heat cycle.
123+
- **void setHeatTimeout(uint8_t seconds)** Set the time out of the heat cycle.
113124
This value is truncated to max 180 seconds.
114-
- **heatOn()** switches heat cycle on. Returns false if fails.
115-
- **heatOff()** switches heat cycle off. Returns false if fails.
116-
- **isHeaterOn()** is the sensor still in heating cycle? replaces **heatUp()**.
117-
Will switch heat off if max heating time has passed.
118-
- **heatUp()** will be obsolete in the future.
125+
- **uint8_t getHeatTimeout
126+
- **bool heatOn()** switches heat cycle on if not already on.
127+
Returns false if fails, setting error to **SHT_ERR_HEATER_COOLDOWN**
128+
or to **SHT_ERR_HEATER_ON**.
129+
- **bool heatOff()** switches heat cycle off.
130+
Returns false if fails, setting error to **SHT_ERR_HEATER_OFF**.
131+
- **bool isHeaterOn()** is the sensor still in heating cycle? replaces **heatUp()**.
132+
Will switch the heater off if max heating time has passed.
119133

120134

121135
#### Async interface
122136

123137
See async example for usage
124138

125-
- **requestData()** requests a new measurement. Returns false if this fails.
126-
- **dataReady()** checks if enough time has passed to read the data. (15 milliseconds)
127-
- **readData(bool fast = true)** fast skips CRC check. Returns false if reading fails or in case of a CRC fail.
139+
- **bool requestData()** requests a new measurement. Returns false if this fails.
140+
- **bool dataReady()** checks if enough time has passed to read the data. (15 milliseconds)
141+
- **bool readData(bool fast = true)** fast = true skips the CRC check.
142+
Returns false if reading fails or in case of a CRC failure.
128143

129144

130145
## Status fields
131146

132-
| BIT | Description | values |
133-
|:----:|:----|:----|
134-
| 15 | Alert pending status | '0' : no pending alerts|
135-
| | | '1' : at least one pending alert - default |
136-
| 14 | Reserved | '0' |
137-
| 13 | Heater status | '0’ : Heater OFF - default |
138-
| | | '1’ : Heater ON |
139-
| 12 | Reserved | '0' |
140-
| 11 | Humidity tracking alert | '0’ : no alert - default |
141-
| | | '1’ : alert |
142-
| 10 | Temperature tracking alert | '0’ : no alert - default |
143-
| | | '1’ : alert |
144-
| 9-5 | Reserved | '00000' |
145-
| 4 | System reset detected | '0' : no reset since last ‘clear status register’ command |
146-
| | | '1' : reset detected (hard or soft reset command or supply fail) - default |
147-
| 3-2 | Reserved | '00' |
148-
| 1 | Command status | '0' : last command executed successfully |
149-
| | | '1' : last command not processed. Invalid or failed checksum |
150-
| 0 | Write data checksum status | '0' : checksum of last write correct |
151-
| | | '1' : checksum of last write transfer failed |
147+
| BIT | Description | value | notes |
148+
|:-----|:---------------------------|:--------|:------|
149+
| 15 | Alert pending status | 0 | no pending alerts
150+
| | | 1 | at least one pending alert - default
151+
| 14 | Reserved | 0 |
152+
| 13 | Heater status | 0 | Heater OFF - default
153+
| | | 1 | Heater ON
154+
| 12 | Reserved | 0 |
155+
| 11 | Humidity tracking alert | 0 | no alert - default
156+
| | | 1 | alert
157+
| 10 | Temperature tracking alert | 0 | no alert - default
158+
| | | 1 | alert
159+
| 9-5 | Reserved | 00000 |
160+
| 4 | System reset detected | 0 | no reset since last ‘clear status register’ command
161+
| | | 1 | reset detected (hard or soft reset command or supply fail) - default
162+
| 3-2 | Reserved | 00 |
163+
| 1 | Command status | 0 | last command executed successfully
164+
| | | 1 | last command not processed. Invalid or failed checksum
165+
| 0 | Write data checksum status | 0 | checksum of last write correct
166+
| | | 1 | checksum of last write transfer failed
152167

153168

154169
## Future
@@ -158,6 +173,8 @@ See async example for usage
158173
- SHT_BASE class ?
159174
- investigate command ART (auto sampling at 4 Hz)
160175
- investigate command BREAK (stop auto sampling)
176+
- direct Fahrenheit formula ?
177+
- improve error handling / status. (all code paths)
161178

162179

163180
## Operation

libraries/SHT85/SHT85.cpp

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT85.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.3
4+
// VERSION: 0.1.4
55
// DATE: 2021-02-10
66
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
77
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@@ -13,6 +13,8 @@
1313
// 0.1.1 2021-03-13 initial release
1414
// 0.1.2 2021-05-27 fix Arduino-lint
1515
// 0.1.3 2021-08-06 expose raw data from sensor
16+
// 0.1.4 2021-08-24 prevent heater to switch on too fast.
17+
// update readme
1618

1719

1820
#include "SHT85.h"
@@ -30,15 +32,18 @@
3032

3133
#define SHT_HEAT_ON 0x306D
3234
#define SHT_HEAT_OFF 0x3066
33-
35+
#define SHT_HEATER_TIMEOUT 180000UL // milliseconds
3436

3537
SHT85::SHT85()
3638
{
37-
_addr = 0;
39+
_address = 0;
3840
_lastRead = 0;
3941
_rawTemperature = 0;
4042
_rawHumidity = 0;
43+
_heatTimeout = 0;
4144
_heaterStart = 0;
45+
_heaterStop = 0;
46+
_heaterOn = false;
4247
_error = SHT_OK;
4348
}
4449

@@ -50,7 +55,7 @@ bool SHT85::begin(const uint8_t address, const uint8_t dataPin, const uint8_t cl
5055
{
5156
return false;
5257
}
53-
_addr = address;
58+
_address = address;
5459

5560
_wire = &Wire;
5661
if ((dataPin < 255) && (clockPin < 255))
@@ -70,8 +75,8 @@ bool SHT85::begin(const uint8_t address, TwoWire *wire)
7075
{
7176
return false;
7277
}
73-
_addr = address;
74-
_wire = wire;
78+
_address = address;
79+
_wire = wire;
7580
_wire->begin();
7681
return reset();
7782
}
@@ -90,7 +95,7 @@ bool SHT85::read(bool fast)
9095

9196
bool SHT85::isConnected()
9297
{
93-
_wire->beginTransmission(_addr);
98+
_wire->beginTransmission(_address);
9499
int rv = _wire->endTransmission();
95100
if (rv != 0) _error = SHT_ERR_NOT_CONNECT;
96101
return (rv == 0);
@@ -165,46 +170,56 @@ bool SHT85::reset(bool hard)
165170

166171
void SHT85::setHeatTimeout(uint8_t seconds)
167172
{
168-
_heatTimeOut = seconds;
169-
if (_heatTimeOut > 180) _heatTimeOut = 180;
173+
_heatTimeout = seconds;
174+
if (_heatTimeout > 180) _heatTimeout = 180;
170175
}
171176

172177

173178
bool SHT85::heatOn()
174179
{
180+
if (isHeaterOn()) return true;
181+
if ((_heaterStop > 0) && (millis() - _heaterStop < SHT_HEATER_TIMEOUT))
182+
{
183+
_error = SHT_ERR_HEATER_COOLDOWN;
184+
return false;
185+
}
175186
if (writeCmd(SHT_HEAT_ON) == false)
176187
{
188+
_error = SHT_ERR_HEATER_ON;
177189
return false;
178190
}
179191
_heaterStart = millis();
192+
_heaterOn = true;
180193
return true;
181194
}
182195

183196

184197
bool SHT85::heatOff()
185198
{
199+
// always switch off the heater - ignore _heaterOn flag.
186200
if (writeCmd(SHT_HEAT_OFF) == false)
187201
{
188202
_error = SHT_ERR_HEATER_OFF; // can be serious!
189203
return false;
190204
}
191-
_heaterStart = 0;
205+
_heaterStop = millis();
206+
_heaterOn = false;
192207
return true;
193208
}
194209

195210

196211
bool SHT85::isHeaterOn()
197212
{
198-
if (_heaterStart == 0)
213+
if (_heaterOn == false)
199214
{
200215
return false;
201216
}
202217
// did not exceed time out
203-
if (millis() - _heaterStart < (_heatTimeOut * 1000UL))
218+
if (millis() - _heaterStart < (_heatTimeout * 1000UL))
204219
{
205220
return true;
206221
}
207-
heatOff(); // should this be done here?
222+
heatOff();
208223
return false;
209224
}
210225

@@ -288,7 +303,7 @@ uint8_t SHT85::crc8(const uint8_t *data, uint8_t len)
288303

289304
bool SHT85::writeCmd(uint16_t cmd)
290305
{
291-
_wire->beginTransmission(_addr);
306+
_wire->beginTransmission(_address);
292307
_wire->write(cmd >> 8 );
293308
_wire->write(cmd & 0xFF);
294309
if (_wire->endTransmission() != 0)
@@ -302,7 +317,7 @@ bool SHT85::writeCmd(uint16_t cmd)
302317

303318
bool SHT85::readBytes(uint8_t n, uint8_t *val)
304319
{
305-
int rv = _wire->requestFrom(_addr, (uint8_t) n);
320+
int rv = _wire->requestFrom(_address, (uint8_t) n);
306321
if (rv == n)
307322
{
308323
for (uint8_t i = 0; i < n; i++)

libraries/SHT85/SHT85.h

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: SHT85.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.1.3
5+
// VERSION: 0.1.4
66
// DATE: 2021-02-10
77
// PURPOSE: Arduino library for the SHT85 temperature and humidity sensor
88
// https://nl.rs-online.com/web/p/temperature-humidity-sensor-ics/1826530
@@ -25,7 +25,7 @@
2525
#include "Wire.h"
2626

2727

28-
#define SHT85_LIB_VERSION (F("0.1.3"))
28+
#define SHT85_LIB_VERSION (F("0.1.4"))
2929

3030

3131
// fields readStatus
@@ -46,6 +46,8 @@
4646
#define SHT_ERR_CRC_TEMP 0x85
4747
#define SHT_ERR_CRC_HUM 0x86
4848
#define SHT_ERR_CRC_STATUS 0x87
49+
#define SHT_ERR_HEATER_COOLDOWN 0x88
50+
#define SHT_ERR_HEATER_ON 0x89
4951

5052

5153
class SHT85
@@ -74,8 +76,10 @@ class SHT85
7476

7577
// do not use heater for long periods,
7678
// use it for max 3 minutes to heat up
77-
// and let it cool down an equal period.
79+
// and let it cool down at least 3 minutes.
7880
void setHeatTimeout(uint8_t seconds);
81+
uint8_t getHeatTimeout() { return _heatTimeout; };
82+
;
7983
bool heatOn();
8084
bool heatOff();
8185
bool isHeaterOn(); // is the sensor still heating up?
@@ -100,11 +104,13 @@ class SHT85
100104
bool readBytes(uint8_t n, uint8_t *val);
101105
TwoWire* _wire;
102106

103-
uint8_t _addr;
104-
uint8_t _heatTimeOut; // seconds
107+
uint8_t _address;
108+
uint8_t _heatTimeout; // seconds
105109
uint32_t _lastRead;
106110
uint32_t _lastRequest; // for async interface
107111
uint32_t _heaterStart;
112+
uint32_t _heaterStop;
113+
bool _heaterOn;
108114

109115
uint16_t _rawHumidity;
110116
uint16_t _rawTemperature;

0 commit comments

Comments
 (0)