Skip to content

Commit 9d71491

Browse files
committed
- fixed issue #123
1 parent 3eaf92e commit 9d71491

File tree

6 files changed

+157
-13
lines changed

6 files changed

+157
-13
lines changed

libraries/SHT31/SHT31.cpp

Lines changed: 67 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
//
22
// FILE: SHT31.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.2
55
// DATE: 2019-02-08
66
// PURPOSE: Class for SHT31 I2C temperature humidity sensor
77
// https://www.adafruit.com/product/2857
88
//
99
// HISTORY:
1010
// 0.1.0 - 2019-02-08 initial version
11+
// 0.1.1 - 2019-02-18 add description readStatus(),
12+
// async interface
13+
// 0.1.2 - 2019-03-05 fix issue #123 - error in humidity
14+
// stable version
1115
//
1216
// Released to the public domain
1317
//
@@ -16,7 +20,8 @@
1620

1721
#define SHT31_READ_STATUS 0xF32D
1822
#define SHT31_CLEAR_STATUS 0x3041
19-
#define SHT31_RESET 0x30A2
23+
#define SHT31_SOFT_RESET 0x30A2
24+
#define SHT31_HARD_RESET 0x0006
2025
#define SHT31_MEASUREMENT_FAST 0x2416
2126
#define SHT31_MEASUREMENT_SLOW 0x2400
2227
#define SHT31_HEAT_ON 0x306D
@@ -54,15 +59,16 @@ bool SHT31::read(bool fast)
5459
writeCmd(SHT31_MEASUREMENT_SLOW);
5560
delay(15); // table 4 datasheet
5661
}
62+
63+
// TODO 5 read bytes would be sufficient when not fast / no CRC...
5764
readBytes(6, (uint8_t*) &buffer[0]);
5865

5966
if (!fast)
6067
{
61-
// TODO 5 read bytes would be sufficient when not fast...
6268
// TODO check CRC here
6369
// TODO rv = false;
6470
}
65-
float raw = (buffer[0] << 8) + buffer[1];
71+
uint16_t raw = (buffer[0] << 8) + buffer[1];
6672
temperature = raw * (175.0 / 65535) - 45;
6773
raw = (buffer[3] << 8) + buffer[4];
6874
humidity = raw * (100.0 / 65535);
@@ -76,16 +82,44 @@ uint16_t SHT31::readStatus()
7682
{
7783
uint32_t status = 0;
7884

79-
writeCmd(SHT31_READ_STATUS);
80-
readBytes(3, (uint8_t*) &status);
85+
writeCmd(SHT31_READ_STATUS); // page 13 datasheet
86+
readBytes(3, (uint8_t*) &status); // 16 bit status + CRC
87+
// TODO CRC check
8188
return status;
89+
90+
// bit - description
91+
// ==================
92+
// 15 Alert pending status
93+
// '0': no pending alerts
94+
// '1': at least one pending alert - default
95+
// 14 Reserved ‘0’
96+
// 13 Heater status
97+
// '0’ : Heater OFF - default
98+
// '1’ : Heater ON
99+
// 12 Reserved '0’
100+
// 11 Humidity tracking alert
101+
// '0’ : no alert - default
102+
// '1’ : alert
103+
// 10 Temp tracking alert
104+
// '0’ : no alert - default
105+
// '1’ : alert
106+
// 9:5 Reserved '00000’
107+
// 4 System reset detected
108+
// '0': no reset since last ‘clear status register’ command
109+
// '1': reset detected (hard or soft reset command or supply fail)
110+
// 3:2 Reserved ‘00’
111+
// 1 Command status
112+
// '0': last cmd executed successfully
113+
// '1': last cmd not processed. Invalid or failed checksum
114+
// 0 Write data checksum status
115+
// '0': checksum of last write correct
116+
// '1': checksum of last write transfer failed
82117
}
83118

84-
// hard reset 0x0006 on addr 0x00 see datasheet
85119
void SHT31::reset()
86120
{
87-
writeCmd(SHT31_RESET);
88-
delay(1); // table 4 datasheet
121+
writeCmd(SHT31_SOFT_RESET); // SHT31_HARD_RESET not implemented yet
122+
delay(1); // table 4 datasheet // 100ms for hardreset
89123
}
90124

91125
void SHT31::heatOn()
@@ -98,6 +132,30 @@ void SHT31::heatOff()
98132
writeCmd(SHT31_HEAT_OFF);
99133
}
100134

135+
void SHT31::requestData()
136+
{
137+
writeCmd(SHT31_MEASUREMENT_SLOW);
138+
_lastRequest = millis();
139+
}
140+
141+
bool SHT31::dataReady()
142+
{
143+
return ((millis() - _lastRequest) > 15);
144+
}
145+
146+
void SHT31::readData()
147+
{
148+
uint8_t buffer[6];
149+
readBytes(6, (uint8_t*) &buffer[0]);
150+
151+
float raw = (buffer[0] << 8) + buffer[1];
152+
temperature = raw * (175.0 / 65535) - 45;
153+
raw = (buffer[3] << 8) + buffer[4];
154+
humidity = raw * (100.0 / 65535);
155+
156+
_lastRead = millis();
157+
}
158+
101159
//////////////////////////////////////////////////////////
102160

103161
void SHT31::writeCmd(uint16_t cmd)

libraries/SHT31/SHT31.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: SHT31.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.2
55
// DATE: 2019-02-08
66
// PURPOSE: Class for SHT31 I2C temperature humidity sensor
77
// https://www.adafruit.com/product/2857
@@ -16,7 +16,7 @@
1616
#include "Arduino.h"
1717
#include "Wire.h"
1818

19-
#define SHT31_LIB_VERSION "0.1.0"
19+
#define SHT31_LIB_VERSION "0.1.2"
2020

2121
class SHT31
2222
{
@@ -33,18 +33,28 @@ class SHT31
3333
uint32_t lastRead() { return _lastRead; };
3434

3535
void reset();
36+
37+
// do not use heater for long periods,
38+
// use it for max 3 minutes to heat up
39+
// and let it cool down an equal period.
3640
void heatOn();
3741
void heatOff();
3842

3943
float humidity;
4044
float temperature;
4145

46+
// ASYNC INTERFACE
47+
void requestData();
48+
bool dataReady();
49+
void readData();
50+
4251
private:
4352
void writeCmd(uint16_t cmd);
4453
void readBytes(uint8_t n, uint8_t *val);
4554

4655
uint8_t _addr;
4756
uint32_t _lastRead;
57+
uint32_t _lastRequest; // for async interface
4858
};
4959

5060
#endif
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//
2+
// FILE: SHT31_I2Cspeed
3+
// AUTHOR: Rob Tillaart
4+
// VERSION: 0.0.1
5+
// PURPOSE: demo
6+
//
7+
// HISTORY:
8+
9+
#include "Wire.h"
10+
#include "SHT31.h"
11+
12+
uint32_t start;
13+
uint32_t stop;
14+
15+
SHT31 sht;
16+
17+
void setup()
18+
{
19+
Serial.begin(115200);
20+
Serial.println(__FILE__);
21+
Serial.print("SHT31_LIB_VERSION: \t");
22+
Serial.println(SHT31_LIB_VERSION);
23+
24+
Wire.begin();
25+
sht.begin(0x44);
26+
Wire.setClock(100000);
27+
uint16_t stat = sht.readStatus();
28+
Serial.print(stat, HEX);
29+
}
30+
31+
void loop()
32+
{
33+
for (uint32_t I2Cfreq = 100000; I2Cfreq < 900000; I2Cfreq += 50000)
34+
{
35+
Serial.print(I2Cfreq/1000);
36+
Wire.setClock(I2Cfreq);
37+
test();
38+
}
39+
}
40+
41+
42+
void test()
43+
{
44+
start = micros();
45+
sht.read(true); // default = true/fast slow = false
46+
stop = micros();
47+
Serial.print("\t");
48+
Serial.print(stop - start);
49+
Serial.print("\t");
50+
Serial.print(sht.temperature, 1);
51+
Serial.print("\t");
52+
Serial.println(sht.humidity, 1);
53+
delay(100);
54+
}

libraries/SHT31/keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map For AnalogKeypad
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
SHT31 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
#######################################
16+
# Instances (KEYWORD2)
17+
#######################################
18+
19+
#######################################
20+
# Constants (LITERAL1)
21+
#######################################
22+

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/Arduino.git"
1717
},
18-
"version":"0.1.0",
18+
"version":"0.1.2",
1919
"frameworks": "arduino",
2020
"platforms": "*",
2121
"export": {

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.1.0
2+
version=0.1.2
33
author=Rob Tillaart <rob.tillaart@gmail.com>
44
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
55
sentence=Library for SHT31 Temperature Humidity

0 commit comments

Comments
 (0)