Skip to content

Commit 94c28e2

Browse files
committed
0.5.2 SHT31
1 parent cb18543 commit 94c28e2

File tree

19 files changed

+238
-49
lines changed

19 files changed

+238
-49
lines changed

libraries/SHT31/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ 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-05-10
10+
- add getSerialNumber, kudos to skunktrading
11+
- add example SHT31_getSerialNumber.ino
12+
- changed defines for commands in static constexpr (.cpp)
13+
- update examples
14+
- update readme.md
15+
- update keywords.txt
16+
- minor edits
17+
918
## [0.5.1] - 2025-04-28
1019
- add **clearStatus()**, kudos to Elbandi
1120
- update readme.md

libraries/SHT31/README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,19 @@ you've performed a new reading.
148148
Be sure to clear the error flag by calling **getError()** before calling
149149
any command as the error flag could be from a previous command.
150150

151-
| Error | Symbolic | Description |
152-
|:-------:|:----------------------------|:-------------------------------|
153-
| 0x00 | SHT31_OK | no error |
154-
| 0x81 | SHT31_ERR_WRITECMD | I2C write failed |
155-
| 0x82 | SHT31_ERR_READBYTES | I2C read failed |
156-
| 0x83 | SHT31_ERR_HEATER_OFF | Could not switch off heater |
157-
| 0x84 | SHT31_ERR_NOT_CONNECT | Could not connect |
158-
| 0x85 | SHT31_ERR_CRC_TEMP | CRC error in temperature |
159-
| 0x86 | SHT31_ERR_CRC_HUM | CRC error in humidity |
160-
| 0x87 | SHT31_ERR_CRC_STATUS | CRC error in status field |
161-
| 0x88 | SHT31_ERR_HEATER_COOLDOWN | Heater need to cool down |
162-
| 0x89 | SHT31_ERR_HEATER_ON | Could not switch on heater |
151+
| Error | Symbolic | Description |
152+
|:-------:|:------------------------------|:-------------------------------|
153+
| 0x00 | SHT31_OK | no error |
154+
| 0x81 | SHT31_ERR_WRITECMD | I2C write failed |
155+
| 0x82 | SHT31_ERR_READBYTES | I2C read failed |
156+
| 0x83 | SHT31_ERR_HEATER_OFF | Could not switch off heater |
157+
| 0x84 | SHT31_ERR_NOT_CONNECT | Could not connect |
158+
| 0x85 | SHT31_ERR_CRC_TEMP | CRC error in temperature |
159+
| 0x86 | SHT31_ERR_CRC_HUM | CRC error in humidity |
160+
| 0x87 | SHT31_ERR_CRC_STATUS | CRC error in status field |
161+
| 0x88 | SHT31_ERR_HEATER_COOLDOWN | Heater need to cool down |
162+
| 0x89 | SHT31_ERR_HEATER_ON | Could not switch on heater |
163+
| 0x8A | SHT31_ERR_SERIAL_NUMBER_CRC | Could not switch on heater |
163164

164165

165166
### Heater interface
@@ -223,6 +224,12 @@ Returns false if reading fails or in case of a CRC failure.
223224
**bool clearStatus()** clears 15, 11, 10 and 4.
224225

225226

227+
### GetSerial
228+
229+
- **bool getSerialNumber(uint32_t &serial, bool fast = true)** fast == true, => no CRC check
230+
fast == false, => do CRC check.
231+
232+
226233
## Future
227234

228235
#### Must
@@ -240,6 +247,8 @@ Returns false if reading fails or in case of a CRC failure.
240247
#### Could
241248

242249
- move code from .h to .cpp
250+
- param fast in getSerialNumber => skipCRC?
251+
243252

244253
#### Wont
245254

libraries/SHT31/SHT31.cpp

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,26 @@
1212

1313

1414
// SUPPORTED COMMANDS - single shot mode only
15-
#define SHT31_READ_STATUS 0xF32D
16-
#define SHT31_CLEAR_STATUS 0x3041
15+
static constexpr uint16_t SHT31_READ_STATUS = 0xF32D;
16+
static constexpr uint16_t SHT31_CLEAR_STATUS = 0x3041;
1717

18-
#define SHT31_SOFT_RESET 0x30A2
19-
#define SHT31_HARD_RESET 0x0006
18+
static constexpr uint16_t SHT31_SOFT_RESET = 0x30A2;
19+
static constexpr uint16_t SHT31_HARD_RESET = 0x0006;
2020

21-
#define SHT31_MEASUREMENT_FAST 0x2416 // page 10 datasheet
22-
#define SHT31_MEASUREMENT_SLOW 0x2400 // no clock stretching
21+
static constexpr uint16_t SHT31_MEASUREMENT_FAST = 0x2416; // page 10 datasheet
22+
static constexpr uint16_t SHT31_MEASUREMENT_SLOW = 0x2400; // no clock stretching
2323

24-
#define SHT31_HEAT_ON 0x306D
25-
#define SHT31_HEAT_OFF 0x3066
26-
#define SHT31_HEATER_TIMEOUT 180000UL // milliseconds
24+
static constexpr uint16_t SHT31_HEAT_ON = 0x306D;
25+
static constexpr uint16_t SHT31_HEAT_OFF = 0x3066;
26+
static constexpr uint32_t SHT31_HEATER_TIMEOUT = 180000UL; // milliseconds
27+
28+
static constexpr uint16_t SHT31_GET_SERIAL_NUMBER = 0x3682; // no clock stretching
2729

2830

2931
SHT31::SHT31(uint8_t address, TwoWire *wire)
3032
{
31-
_wire = wire;
3233
_address = address;
34+
_wire = wire;
3335
_lastRead = 0;
3436
_rawTemperature = 0;
3537
_rawHumidity = 0;
@@ -131,7 +133,6 @@ uint16_t SHT31::readStatus()
131133
_error = SHT31_ERR_CRC_STATUS;
132134
return 0xFFFF;
133135
}
134-
135136
return (uint16_t) (status[0] << 8) + status[1];
136137
}
137138

@@ -150,6 +151,7 @@ bool SHT31::clearStatus()
150151
return true;
151152
}
152153

154+
153155
bool SHT31::reset(bool hard)
154156
{
155157
bool b = writeCmd(hard ? SHT31_HARD_RESET : SHT31_SOFT_RESET);
@@ -224,7 +226,7 @@ bool SHT31::isHeaterOn()
224226

225227
/////////////////////////////////////////////////////////////////
226228
//
227-
// ASYNC
229+
// ASYNCHRONUOUS INTERFACE
228230
//
229231
bool SHT31::requestData()
230232
{
@@ -269,11 +271,14 @@ bool SHT31::readData(bool fast)
269271
_rawHumidity = (buffer[3] << 8) + buffer[4];
270272

271273
_lastRead = millis();
272-
273274
return true;
274275
}
275276

276277

278+
/////////////////////////////////////////////////////////////////
279+
//
280+
// MISC
281+
//
277282
int SHT31::getError()
278283
{
279284
int rv = _error;
@@ -282,6 +287,40 @@ int SHT31::getError()
282287
}
283288

284289

290+
/**
291+
* See https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
292+
*/
293+
bool SHT31::getSerialNumber(uint32_t &serial, bool fast) {
294+
if (writeCmd(SHT31_GET_SERIAL_NUMBER) == false) {
295+
return false;
296+
}
297+
delay(1);
298+
uint8_t buffer[6];
299+
if (readBytes(6, &buffer[0]) == false) {
300+
return false;
301+
}
302+
303+
if (!fast) {
304+
if (buffer[2] != crc8(buffer, 2)) {
305+
_error = SHT31_ERR_SERIAL_NUMBER_CRC;
306+
return false;
307+
}
308+
if (buffer[5] != crc8(buffer + 3, 2)) {
309+
_error = SHT31_ERR_SERIAL_NUMBER_CRC;
310+
return false;
311+
}
312+
}
313+
serial = buffer[0];
314+
serial <<= 8;
315+
serial += buffer[1];
316+
serial <<= 8;
317+
serial += buffer[3];
318+
serial <<= 8;
319+
serial += buffer[4];
320+
return true;
321+
}
322+
323+
285324
/////////////////////////////////////////////////////////////////
286325
//
287326
// PROTECTED
@@ -315,6 +354,7 @@ bool SHT31::writeCmd(uint16_t cmd)
315354
_error = SHT31_ERR_WRITECMD;
316355
return false;
317356
}
357+
_error = SHT31_OK;
318358
return true;
319359
}
320360

@@ -328,6 +368,7 @@ bool SHT31::readBytes(uint8_t n, uint8_t *val)
328368
{
329369
val[i] = _wire->read();
330370
}
371+
_error = SHT31_OK;
331372
return true;
332373
}
333374
_error = SHT31_ERR_READBYTES;

libraries/SHT31/SHT31.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: SHT31.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.5.1
5+
// VERSION: 0.5.2
66
// DATE: 2019-02-08
77
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
88
// https://www.adafruit.com/product/2857
@@ -13,7 +13,7 @@
1313
#include "Wire.h"
1414

1515

16-
#define SHT31_LIB_VERSION (F("0.5.1"))
16+
#define SHT31_LIB_VERSION (F("0.5.2"))
1717

1818
#ifndef SHT_DEFAULT_ADDRESS
1919
#define SHT_DEFAULT_ADDRESS 0x44
@@ -39,6 +39,7 @@
3939
#define SHT31_ERR_CRC_STATUS 0x87
4040
#define SHT31_ERR_HEATER_COOLDOWN 0x88
4141
#define SHT31_ERR_HEATER_ON 0x89
42+
#define SHT31_ERR_SERIAL_NUMBER_CRC 0x8A
4243

4344

4445
class SHT31
@@ -90,7 +91,11 @@ class SHT31
9091
bool dataReady();
9192
bool readData(bool fast = true);
9293

94+
// MISC
9395
int getError(); // clears error flag
96+
// fast == true, => skips CRC check
97+
bool getSerialNumber(uint32_t &serial, bool fast = true);
98+
9499

95100
protected:
96101
uint8_t _address;

libraries/SHT31/examples/SHT31_I2Cspeed/SHT31_I2Cspeed.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ SHT31 sht(SHT31_ADDRESS);
1818

1919
void setup()
2020
{
21+
// while(!Serial); // uncomment if needed
2122
Serial.begin(115200);
23+
Serial.println();
2224
Serial.println(__FILE__);
2325
Serial.print("SHT31_LIB_VERSION: \t");
2426
Serial.println(SHT31_LIB_VERSION);
27+
Serial.println();
2528

2629
Wire.begin();
2730
Wire.setClock(100000);

libraries/SHT31/examples/SHT31_async/SHT31_async.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@ SHT31 sht; // use default address and Wire
1919

2020
void setup()
2121
{
22+
// while(!Serial); // uncomment if needed
2223
Serial.begin(115200);
24+
Serial.println();
2325
Serial.println(__FILE__);
2426
Serial.print("SHT31_LIB_VERSION: \t");
2527
Serial.println(SHT31_LIB_VERSION);
28+
Serial.println();
2629

2730
Wire.begin();
2831
Wire.setClock(100000);
@@ -31,7 +34,7 @@ void setup()
3134
uint16_t stat = sht.readStatus();
3235
Serial.print(stat, HEX);
3336
Serial.println();
34-
37+
3538
sht.requestData();
3639
cnt = 0;
3740
}

libraries/SHT31/examples/SHT31_demo/SHT31_demo.ino

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ SHT31 sht;
1818

1919
void setup()
2020
{
21+
// while(!Serial); // uncomment if needed
2122
Serial.begin(115200);
23+
Serial.println();
2224
Serial.println(__FILE__);
2325
Serial.print("SHT31_LIB_VERSION: \t");
2426
Serial.println(SHT31_LIB_VERSION);
27+
Serial.println();
2528

2629
Wire.begin();
2730
Wire.setClock(100000);

libraries/SHT31/examples/SHT31_demo_plotter/SHT31_demo_plotter.ino

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT31_demo_plotter.ino
33
// AUTHOR: Rob Tillaart
4-
// PURPOSE: demo
4+
// PURPOSE: demo for plotter
55
// URL: https://github.com/RobTillaart/SHT31
66

77

@@ -18,11 +18,13 @@ SHT31 sht;
1818

1919
void setup()
2020
{
21+
// while(!Serial); // uncomment if needed
2122
Serial.begin(115200);
23+
// Serial.println();
2224
// Serial.println(__FILE__);
2325
// Serial.print("SHT31_LIB_VERSION: \t");
2426
// Serial.println(SHT31_LIB_VERSION);
25-
27+
// Serial.println();
2628

2729
Wire.begin();
2830
Wire.setClock(100000);

0 commit comments

Comments
 (0)