Skip to content

Commit 99d8a35

Browse files
authored
add getSerialNumber (#46)
- add getSerialNumber, kudos to skunktrading - add example SHT31_getSerialNumber.ino - changed defines for commands in static constexpr (.cpp) - update examples - update readme.md - update keywords.txt - minor edits * fix build * fix build
1 parent 93c3863 commit 99d8a35

File tree

19 files changed

+214
-53
lines changed

19 files changed

+214
-53
lines changed

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

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

SHT31.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +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

28-
static constexpr uint16_t SHT31_GET_SERIAL_NUMBER = 0x3682; // no clock stretching
2930

3031
SHT31::SHT31(uint8_t address, TwoWire *wire)
3132
{
32-
_wire = wire;
3333
_address = address;
34+
_wire = wire;
3435
_lastRead = 0;
3536
_rawTemperature = 0;
3637
_rawHumidity = 0;
@@ -132,7 +133,6 @@ uint16_t SHT31::readStatus()
132133
_error = SHT31_ERR_CRC_STATUS;
133134
return 0xFFFF;
134135
}
135-
136136
return (uint16_t) (status[0] << 8) + status[1];
137137
}
138138

@@ -151,6 +151,7 @@ bool SHT31::clearStatus()
151151
return true;
152152
}
153153

154+
154155
bool SHT31::reset(bool hard)
155156
{
156157
bool b = writeCmd(hard ? SHT31_HARD_RESET : SHT31_SOFT_RESET);
@@ -225,7 +226,7 @@ bool SHT31::isHeaterOn()
225226

226227
/////////////////////////////////////////////////////////////////
227228
//
228-
// ASYNC
229+
// ASYNCHRONUOUS INTERFACE
229230
//
230231
bool SHT31::requestData()
231232
{
@@ -270,18 +271,22 @@ bool SHT31::readData(bool fast)
270271
_rawHumidity = (buffer[3] << 8) + buffer[4];
271272

272273
_lastRead = millis();
273-
274274
return true;
275275
}
276276

277277

278+
/////////////////////////////////////////////////////////////////
279+
//
280+
// MISC
281+
//
278282
int SHT31::getError()
279283
{
280284
int rv = _error;
281285
_error = SHT31_OK;
282286
return rv;
283287
}
284288

289+
285290
/**
286291
* See https://sensirion.com/media/documents/E5762713/63D103C2/Sensirion_electronic_identification_code_SHT3x.pdf
287292
*/
@@ -305,10 +310,17 @@ bool SHT31::getSerialNumber(uint32_t &serial, bool fast) {
305310
return false;
306311
}
307312
}
308-
serial = (buffer[0] << 24) | (buffer[1] << 16) | (buffer[3] << 8) | buffer[4];
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];
309320
return true;
310321
}
311322

323+
312324
/////////////////////////////////////////////////////////////////
313325
//
314326
// PROTECTED
@@ -342,6 +354,7 @@ bool SHT31::writeCmd(uint16_t cmd)
342354
_error = SHT31_ERR_WRITECMD;
343355
return false;
344356
}
357+
_error = SHT31_OK;
345358
return true;
346359
}
347360

@@ -355,6 +368,7 @@ bool SHT31::readBytes(uint8_t n, uint8_t *val)
355368
{
356369
val[i] = _wire->read();
357370
}
371+
_error = SHT31_OK;
358372
return true;
359373
}
360374
_error = SHT31_ERR_READBYTES;

SHT31.h

Lines changed: 8 additions & 4 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,7 +39,8 @@
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
42+
#define SHT31_ERR_SERIAL_NUMBER_CRC 0x8A
43+
4344

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

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

96100
protected:
97101
uint8_t _address;

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);

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
}

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);

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);
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
//
2+
// FILE: SHT31_getSerialNumber.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo Serial Number
5+
// URL: https://github.com/RobTillaart/SHT31
6+
7+
8+
#include "Wire.h"
9+
#include "SHT31.h"
10+
11+
#define SHT31_ADDRESS 0x44
12+
13+
uint32_t start;
14+
uint32_t stop;
15+
16+
SHT31 sht(SHT31_ADDRESS); // uses explicit address
17+
18+
19+
void setup()
20+
{
21+
// while(!Serial); // uncomment if needed
22+
Serial.begin(115200);
23+
Serial.println();
24+
Serial.println(__FILE__);
25+
Serial.print("SHT31_LIB_VERSION: \t");
26+
Serial.println(SHT31_LIB_VERSION);
27+
Serial.println();
28+
29+
Wire.begin();
30+
Wire.setClock(100000);
31+
sht.begin();
32+
33+
uint16_t stat = sht.readStatus();
34+
Serial.print("STATUS:\t");
35+
Serial.print(stat, HEX);
36+
Serial.println();
37+
38+
delay(100);
39+
40+
uint32_t sn = 0;
41+
bool b;
42+
43+
start = micros();
44+
b = sht.getSerialNumber(sn, true);
45+
stop = micros();
46+
Serial.println("FAST:\ttrue");
47+
Serial.print("TIME:\t");
48+
Serial.println(stop - start);
49+
if (b)
50+
{
51+
Serial.print("SN:\t");
52+
Serial.println(sn);
53+
}
54+
else
55+
{
56+
Serial.print("ERROR:\t");
57+
Serial.println(sht.getError(), HEX);
58+
}
59+
delay(100);
60+
61+
start = micros();
62+
b = sht.getSerialNumber(sn, false);
63+
stop = micros();
64+
Serial.println("FAST:\tfalse");
65+
Serial.print("TIME:\t");
66+
Serial.println(stop - start);
67+
if (b)
68+
{
69+
Serial.print("SN:\t");
70+
Serial.println(sn);
71+
}
72+
else
73+
{
74+
Serial.print("ERROR:\t");
75+
Serial.println(sht.getError(), HEX);
76+
}
77+
78+
Serial.println("\ndone...");
79+
}
80+
81+
82+
void loop()
83+
{
84+
85+
}
86+
87+
88+
// -- END OF FILE --

0 commit comments

Comments
 (0)