Skip to content

Commit 7f787a2

Browse files
committed
Disable clock stretching
Since not all platforms support clock streching, use the measurement commands with disabled clock streching
1 parent 494fe51 commit 7f787a2

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

SHTSensor.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ bool SHTSensorDriver::readSample()
5151
// class SHTI2cSensor
5252
//
5353

54-
const uint8_t SHTI2cSensor::CMD_SIZE = 2;
55-
const uint8_t SHTI2cSensor::EXPECTED_DATA_SIZE = 6;
54+
const uint8_t SHTI2cSensor::CMD_SIZE = 2;
55+
const uint8_t SHTI2cSensor::EXPECTED_DATA_SIZE = 6;
5656

5757
bool SHTI2cSensor::readFromI2c(uint8_t i2cAddress,
5858
const uint8_t *i2cCommand,
5959
uint8_t commandLength, uint8_t *data,
60-
uint8_t dataLength)
60+
uint8_t dataLength,
61+
uint8_t duration)
6162
{
6263
Wire.beginTransmission(i2cAddress);
6364
for (int i = 0; i < commandLength; ++i) {
@@ -70,6 +71,8 @@ bool SHTI2cSensor::readFromI2c(uint8_t i2cAddress,
7071
return false;
7172
}
7273

74+
delay(duration);
75+
7376
Wire.requestFrom(i2cAddress, dataLength);
7477

7578
// check if the same number of bytes are received that are requested.
@@ -113,7 +116,7 @@ bool SHTI2cSensor::readSample()
113116
cmd[1] = mI2cCommand & 0xff;
114117

115118
if (!readFromI2c(mI2cAddress, cmd, CMD_SIZE, data,
116-
EXPECTED_DATA_SIZE)) {
119+
EXPECTED_DATA_SIZE, mDuration)) {
117120
return false;
118121
}
119122

@@ -144,8 +147,8 @@ class SHTC1Sensor : public SHTI2cSensor
144147
{
145148
public:
146149
SHTC1Sensor()
147-
// Using clock stretching, high precision, T first
148-
: SHTI2cSensor(0x70, 0x7ca2, -45, 175, 65535, 100, 65535)
150+
// clock stretching disabled, high precision, T first
151+
: SHTI2cSensor(0x70, 0x7866, 15, -45, 175, 65535, 100, 65535)
149152
{
150153
}
151154
};
@@ -158,16 +161,21 @@ class SHTC1Sensor : public SHTI2cSensor
158161
class SHT3xSensor : public SHTI2cSensor
159162
{
160163
private:
161-
static const uint16_t SHT3X_ACCURACY_HIGH = 0x2c06;
162-
static const uint16_t SHT3X_ACCURACY_MEDIUM = 0x2c0d;
163-
static const uint16_t SHT3X_ACCURACY_LOW = 0x2c10;
164+
static const uint16_t SHT3X_ACCURACY_HIGH = 0x2400;
165+
static const uint16_t SHT3X_ACCURACY_MEDIUM = 0x240b;
166+
static const uint16_t SHT3X_ACCURACY_LOW = 0x2416;
167+
168+
static const uint8_t SHT3X_ACCURACY_HIGH_DURATION = 15;
169+
static const uint8_t SHT3X_ACCURACY_MEDIUM_DURATION = 6;
170+
static const uint8_t SHT3X_ACCURACY_LOW_DURATION = 4;
164171

165172
public:
166173
static const uint8_t SHT3X_I2C_ADDRESS_44 = 0x44;
167174
static const uint8_t SHT3X_I2C_ADDRESS_45 = 0x45;
168175

169176
SHT3xSensor(uint8_t i2cAddress = SHT3X_I2C_ADDRESS_44)
170177
: SHTI2cSensor(i2cAddress, SHT3X_ACCURACY_HIGH,
178+
SHT3X_ACCURACY_HIGH_DURATION,
171179
-45, 175, 65535, 100, 65535)
172180
{
173181
}
@@ -177,12 +185,15 @@ class SHT3xSensor : public SHTI2cSensor
177185
switch (newAccuracy) {
178186
case SHTSensor::SHT_ACCURACY_HIGH:
179187
mI2cCommand = SHT3X_ACCURACY_HIGH;
188+
mDuration = SHT3X_ACCURACY_HIGH_DURATION;
180189
break;
181190
case SHTSensor::SHT_ACCURACY_MEDIUM:
182191
mI2cCommand = SHT3X_ACCURACY_MEDIUM;
192+
mDuration = SHT3X_ACCURACY_MEDIUM_DURATION;
183193
break;
184194
case SHTSensor::SHT_ACCURACY_LOW:
185195
mI2cCommand = SHT3X_ACCURACY_LOW;
196+
mDuration = SHT3X_ACCURACY_LOW_DURATION;
186197
break;
187198
default:
188199
return false;

SHTSensor.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ class SHTI2cSensor : public SHTSensorDriver {
202202
* and the values `x' and `y' to convert the fixed-point humidity value
203203
* received by the sensor to a floating point value using the formula:
204204
* humidity = x * (rawHumidity / y)
205+
* duration is the duration in milliseconds of one measurement
205206
*/
206-
SHTI2cSensor(uint8_t i2cAddress, uint16_t i2cCommand,
207+
SHTI2cSensor(uint8_t i2cAddress, uint16_t i2cCommand, uint8_t duration,
207208
float a, float b, float c,
208209
float x, float y)
209-
: mI2cAddress(i2cAddress), mI2cCommand(i2cCommand),
210+
: mI2cAddress(i2cAddress), mI2cCommand(i2cCommand), mDuration(duration),
210211
mA(a), mB(b), mC(c), mX(x), mY(y)
211212
{
212213
}
@@ -219,6 +220,7 @@ class SHTI2cSensor : public SHTSensorDriver {
219220

220221
uint8_t mI2cAddress;
221222
uint16_t mI2cCommand;
223+
uint8_t mDuration;
222224
float mA;
223225
float mB;
224226
float mC;
@@ -230,7 +232,7 @@ class SHTI2cSensor : public SHTSensorDriver {
230232
static bool readFromI2c(uint8_t i2cAddress,
231233
const uint8_t *i2cCommand,
232234
uint8_t commandLength, uint8_t *data,
233-
uint8_t dataLength);
235+
uint8_t dataLength, uint8_t duration);
234236
};
235237

236238
class SHT3xAnalogSensor

0 commit comments

Comments
 (0)