Skip to content

Commit 159bbd2

Browse files
committed
0.3.2 SHT31
1 parent 21edf8d commit 159bbd2

File tree

7 files changed

+110
-22
lines changed

7 files changed

+110
-22
lines changed

libraries/SHT31/README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ returns false if device address is incorrect or device cannot be reset.
4141
- **uint16_t readStatus()** details see datasheet and **Status fields** below
4242
- **uint32_t lastRead()** in milliSeconds since start of program.
4343
- **reset(bool hard = false)** resets the sensor, soft reset by default. Returns false if fails.
44-
- **getHumidity()** returns relative humidity in %
45-
- **getTemperature()** returns temperature in °C
44+
- **getHumidity()** computes the relative humidity in % based off the latest raw reading, and returns it
45+
- **getTemperature()** computes the temperature in °C based off the latest raw reading, and returns it
46+
- **getRawHumidity()** returns the raw two-byte representation of humidity directly from the sensor
47+
- **getRawTemperature()** returns the raw two-byte representation of temperature directly from the sensor
48+
49+
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.
4650

4751

4852
#### Error interface

libraries/SHT31/SHT31.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT31.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.3.1
4+
// VERSION: 0.3.2
55
// DATE: 2019-02-08
66
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
77
// https://www.adafruit.com/product/2857
@@ -25,6 +25,7 @@
2525
//
2626
// 0.3.0 2021-01-04 arduino-ci
2727
// 0.3.1 2021-05-27 arduino-lint fixes
28+
// 0.3.2 2021-08-05 expose raw data from sensor
2829

2930

3031
#include "SHT31.h"
@@ -46,12 +47,12 @@
4647

4748
SHT31::SHT31()
4849
{
49-
_addr = 0;
50-
_lastRead = 0;
51-
temperature = 0;
52-
humidity = 0;
53-
_heaterStart = 0;
54-
_error = SHT31_OK;
50+
_addr = 0;
51+
_lastRead = 0;
52+
rawTemperature = 0;
53+
rawHumidity = 0;
54+
_heaterStart = 0;
55+
_error = SHT31_OK;
5556
}
5657

5758

@@ -266,10 +267,8 @@ bool SHT31::readData(bool fast)
266267
}
267268
}
268269

269-
uint16_t raw = (buffer[0] << 8) + buffer[1];
270-
temperature = raw * (175.0 / 65535) - 45;
271-
raw = (buffer[3] << 8) + buffer[4];
272-
humidity = raw * (100.0 / 65535);
270+
rawTemperature = (buffer[0] << 8) + buffer[1];
271+
rawHumidity = (buffer[3] << 8) + buffer[4];
273272

274273
_lastRead = millis();
275274

libraries/SHT31/SHT31.h

Lines changed: 8 additions & 6 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.3.1
5+
// VERSION: 0.3.2
66
// DATE: 2019-02-08
77
// PURPOSE: Arduino library for the SHT31 temperature and humidity sensor
88
// https://www.adafruit.com/product/2857
@@ -14,7 +14,7 @@
1414
#include "Wire.h"
1515

1616

17-
#define SHT31_LIB_VERSION (F("0.3.1"))
17+
#define SHT31_LIB_VERSION (F("0.3.2"))
1818

1919

2020
// fields readStatus
@@ -71,8 +71,10 @@ class SHT31
7171
bool isHeaterOn(); // is the sensor still heating up?
7272
bool heatUp() { return isHeaterOn(); }; // will be obsolete in future
7373

74-
float getHumidity() { return humidity; };
75-
float getTemperature() { return temperature; };
74+
float getHumidity() { return rawHumidity * (100.0 / 65535); };
75+
float getTemperature() { return rawTemperature * (175.0 / 65535) - 45; };
76+
uint16_t getRawHumidity() {return rawHumidity ; };
77+
uint16_t getRawTemperature() {return rawTemperature; };
7678

7779
// ASYNC INTERFACE
7880
bool requestData();
@@ -93,8 +95,8 @@ class SHT31
9395
uint32_t _lastRequest; // for async interface
9496
uint32_t _heaterStart;
9597

96-
float humidity;
97-
float temperature;
98+
uint16_t rawHumidity;
99+
uint16_t rawTemperature;
98100

99101
uint8_t _error;
100102
};
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// FILE: SHT31_async.ino
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.3.2
5+
// PURPOSE: demo async interface
6+
// URL: https://github.com/RobTillaart/SHT31
7+
8+
9+
#include "Wire.h"
10+
#include "SHT31.h"
11+
12+
#define SHT31_ADDRESS 0x44
13+
14+
uint32_t start;
15+
uint32_t stop;
16+
uint32_t cnt;
17+
18+
SHT31 sht;
19+
20+
21+
void setup()
22+
{
23+
Serial.begin(115200);
24+
Serial.println(__FILE__);
25+
Serial.print("SHT31_LIB_VERSION: \t");
26+
Serial.println(SHT31_LIB_VERSION);
27+
28+
Wire.begin();
29+
sht.begin(SHT31_ADDRESS);
30+
Wire.setClock(100000);
31+
32+
uint16_t stat = sht.readStatus();
33+
Serial.print(stat, HEX);
34+
Serial.println();
35+
36+
sht.requestData();
37+
cnt = 0;
38+
}
39+
40+
41+
void loop()
42+
{
43+
uint16_t rawTemperature;
44+
uint16_t rawHumidity;
45+
46+
if (sht.dataReady())
47+
{
48+
start = micros();
49+
bool success = sht.readData(); // default = true = fast
50+
stop = micros();
51+
sht.requestData(); // request for next sample
52+
53+
Serial.print("\t");
54+
Serial.print(stop - start);
55+
Serial.print("\t");
56+
if (success == false)
57+
{
58+
Serial.println("Failed read");
59+
}
60+
else
61+
{
62+
rawTemperature = sht.getRawTemperature();
63+
rawHumidity = sht.getRawHumidity();
64+
Serial.print(rawTemperature, HEX);
65+
Serial.print(" = ");
66+
Serial.print(rawTemperature * (175.0 / 65535) - 45, 1); // This formula comes from page 14 of the SHT31 datasheet
67+
Serial.print("°C\t");
68+
Serial.print(sht.getRawHumidity(), HEX);
69+
Serial.print(" = ");
70+
Serial.print(rawHumidity * (100.0 / 65535), 1); // This formula comes from page 14 of the SHT31 datasheet
71+
Serial.print("%\t");
72+
Serial.println(cnt);
73+
cnt = 0;
74+
}
75+
}
76+
cnt++; // simulate other activity
77+
}
78+
79+
// -- END OF FILE --

libraries/SHT31/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/SHT31"
1717
},
18-
"version": "0.3.1",
18+
"version": "0.3.2",
1919
"license": "MIT",
2020
"frameworks": "arduino",
2121
"platforms": "*"

libraries/SHT31/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SHT31
2-
version=0.3.1
2+
version=0.3.2
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=Arduino library for the SHT31 temperature and humidity sensor

libraries/SHT31/test/unit_test_001.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,14 @@ unittest(test_begin)
6161

6262
Serial.println(sht.getTemperature());
6363
Serial.println(sht.getHumidity());
64+
Serial.println(sht.getRawTemperature());
65+
Serial.println(sht.getRawHumidity());
6466

6567
// default value == 0
66-
assertEqual(0, sht.getTemperature());
68+
assertEqual(-45, sht.getTemperature());
6769
assertEqual(0, sht.getHumidity());
70+
assertEqual(0, sht.getRawTemperature());
71+
assertEqual(0, sht.getRawHumidity());
6872
}
6973

7074

0 commit comments

Comments
 (0)