Skip to content

Commit 1de2e9a

Browse files
ndml28winkj
authored andcommitted
added support for sht4x
1 parent a8c826f commit 1de2e9a

File tree

3 files changed

+75
-16
lines changed

3 files changed

+75
-16
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Repository for Sensirion humidity and temperature sensor support on Arduino
88
- SHT3x-DIS (I2C)
99
- SHT3x-ARP (ratiometric analog voltage output)
1010
- SHT85
11+
- SHT4x
1112

1213
## Installation
1314

SHTSensor.cpp

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ bool SHTSensorDriver::readSample()
5151
// class SHTI2cSensor
5252
//
5353

54-
const uint8_t SHTI2cSensor::CMD_SIZE = 2;
5554
const uint8_t SHTI2cSensor::EXPECTED_DATA_SIZE = 6;
5655

5756
bool SHTI2cSensor::readFromI2c(uint8_t i2cAddress,
@@ -110,12 +109,13 @@ uint8_t SHTI2cSensor::crc8(const uint8_t *data, uint8_t len)
110109
bool SHTI2cSensor::readSample()
111110
{
112111
uint8_t data[EXPECTED_DATA_SIZE];
113-
uint8_t cmd[CMD_SIZE];
112+
uint8_t cmd[mCmd_Size];
114113

115114
cmd[0] = mI2cCommand >> 8;
115+
//is omitted for SHT4x Sensors
116116
cmd[1] = mI2cCommand & 0xff;
117117

118-
if (!readFromI2c(mI2cAddress, cmd, CMD_SIZE, data,
118+
if (!readFromI2c(mI2cAddress, cmd, mCmd_Size, data,
119119
EXPECTED_DATA_SIZE, mDuration)) {
120120
return false;
121121
}
@@ -133,12 +133,12 @@ bool SHTI2cSensor::readSample()
133133
mTemperature = mA + mB * (val / mC);
134134

135135
val = (data[3] << 8) + data[4];
136-
mHumidity = mX * (val / mY);
136+
mHumidity = mX + mY * (val / mZ);
137137

138-
return true;
138+
return true;
139+
139140
}
140141

141-
142142
//
143143
// class SHTC1Sensor
144144
//
@@ -148,7 +148,7 @@ class SHTC1Sensor : public SHTI2cSensor
148148
public:
149149
SHTC1Sensor()
150150
// clock stretching disabled, high precision, T first
151-
: SHTI2cSensor(0x70, 0x7866, 15, -45, 175, 65535, 100, 65535)
151+
: SHTI2cSensor(0x70, 0x7866, 15, -45, 175, 65535, 0, 100, 65535, 2)
152152
{
153153
}
154154
};
@@ -176,7 +176,7 @@ class SHT3xSensor : public SHTI2cSensor
176176
SHT3xSensor(uint8_t i2cAddress = SHT3X_I2C_ADDRESS_44)
177177
: SHTI2cSensor(i2cAddress, SHT3X_ACCURACY_HIGH,
178178
SHT3X_ACCURACY_HIGH_DURATION,
179-
-45, 175, 65535, 100, 65535)
179+
-45, 175, 65535, 0, 100, 65535, 2)
180180
{
181181
}
182182

@@ -203,6 +203,55 @@ class SHT3xSensor : public SHTI2cSensor
203203
};
204204

205205

206+
//
207+
// class SHT4xSensor
208+
//
209+
210+
class SHT4xSensor : public SHTI2cSensor
211+
{
212+
private:
213+
static const uint16_t SHT4X_ACCURACY_HIGH = 0xFD00;
214+
static const uint16_t SHT4X_ACCURACY_MEDIUM = 0xF600;
215+
static const uint16_t SHT4X_ACCURACY_LOW = 0xE000;
216+
217+
static const uint8_t SHT4X_ACCURACY_HIGH_DURATION = 10;
218+
static const uint8_t SHT4X_ACCURACY_MEDIUM_DURATION = 4;
219+
static const uint8_t SHT4X_ACCURACY_LOW_DURATION = 2;
220+
221+
public:
222+
static const uint8_t SHT4X_I2C_ADDRESS_44 = 0x44;
223+
static const uint8_t SHT4X_I2C_ADDRESS_45 = 0x45;
224+
225+
SHT4xSensor(uint8_t i2cAddress = SHT4X_I2C_ADDRESS_44)
226+
: SHTI2cSensor(i2cAddress, SHT4X_ACCURACY_HIGH,
227+
SHT4X_ACCURACY_HIGH_DURATION,
228+
-45, 175, 65535, -6, 125, 65535, 1)
229+
{
230+
}
231+
232+
virtual bool setAccuracy(SHTSensor::SHTAccuracy newAccuracy)
233+
{
234+
switch (newAccuracy) {
235+
case SHTSensor::SHT_ACCURACY_HIGH:
236+
mI2cCommand = SHT4X_ACCURACY_HIGH;
237+
mDuration = SHT4X_ACCURACY_HIGH_DURATION;
238+
break;
239+
case SHTSensor::SHT_ACCURACY_MEDIUM:
240+
mI2cCommand = SHT4X_ACCURACY_MEDIUM;
241+
mDuration = SHT4X_ACCURACY_MEDIUM_DURATION;
242+
break;
243+
case SHTSensor::SHT_ACCURACY_LOW:
244+
mI2cCommand = SHT4X_ACCURACY_LOW;
245+
mDuration = SHT4X_ACCURACY_LOW_DURATION;
246+
break;
247+
default:
248+
return false;
249+
}
250+
return true;
251+
}
252+
};
253+
254+
206255
//
207256
// class SHT3xAnalogSensor
208257
//
@@ -227,7 +276,8 @@ float SHT3xAnalogSensor::readTemperature()
227276
const SHTSensor::SHTSensorType SHTSensor::AUTO_DETECT_SENSORS[] = {
228277
SHT3X,
229278
SHT3X_ALT,
230-
SHTC1
279+
SHTC1,
280+
SHT4X
231281
};
232282
const float SHTSensor::TEMPERATURE_INVALID = NAN;
233283
const float SHTSensor::HUMIDITY_INVALID = NAN;
@@ -252,14 +302,17 @@ bool SHTSensor::init()
252302
case SHTC1:
253303
mSensor = new SHTC1Sensor();
254304
break;
255-
305+
case SHT4X:
306+
mSensor = new SHT4xSensor();
307+
break;
256308
case AUTO_DETECT:
257309
{
258310
bool detected = false;
259311
for (unsigned int i = 0;
260312
i < sizeof(AUTO_DETECT_SENSORS) / sizeof(AUTO_DETECT_SENSORS[0]);
261313
++i) {
262314
mSensorType = AUTO_DETECT_SENSORS[i];
315+
delay(40);
263316
if (init()) {
264317
detected = true;
265318
break;

SHTSensor.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ class SHTSensor
5656
SHT3X_ALT,
5757
SHTC1,
5858
SHTW1,
59-
SHTW2
59+
SHTW2,
60+
SHT4X
6061
};
6162

6263
/**
@@ -146,10 +147,12 @@ class SHTSensor
146147
*/
147148
bool setAccuracy(SHTAccuracy newAccuracy);
148149

150+
SHTSensorType mSensorType;
151+
149152
private:
150153
void cleanup();
151154

152-
SHTSensorType mSensorType;
155+
153156
SHTSensorDriver *mSensor;
154157
float mTemperature;
155158
float mHumidity;
@@ -197,7 +200,7 @@ class SHTSensorDriver
197200
class SHTI2cSensor : public SHTSensorDriver {
198201
public:
199202
/** Size of i2c commands to send */
200-
static const uint8_t CMD_SIZE;
203+
201204

202205
/** Size of i2c replies to expect */
203206
static const uint8_t EXPECTED_DATA_SIZE;
@@ -210,14 +213,14 @@ class SHTI2cSensor : public SHTSensorDriver {
210213
* the formula: temperature = a + b * (rawTemperature / c)
211214
* and the values `x' and `y' to convert the fixed-point humidity value
212215
* received by the sensor to a floating point value using the formula:
213-
* humidity = x * (rawHumidity / y)
216+
* humidity = x + y * (rawHumidity / z)
214217
* duration is the duration in milliseconds of one measurement
215218
*/
216219
SHTI2cSensor(uint8_t i2cAddress, uint16_t i2cCommand, uint8_t duration,
217220
float a, float b, float c,
218-
float x, float y)
221+
float x, float y, float z, uint8_t cmd_Size)
219222
: mI2cAddress(i2cAddress), mI2cCommand(i2cCommand), mDuration(duration),
220-
mA(a), mB(b), mC(c), mX(x), mY(y)
223+
mA(a), mB(b), mC(c), mX(x), mY(y), mZ(z), mCmd_Size(cmd_Size)
221224
{
222225
}
223226

@@ -235,6 +238,8 @@ class SHTI2cSensor : public SHTSensorDriver {
235238
float mC;
236239
float mX;
237240
float mY;
241+
float mZ;
242+
uint8_t mCmd_Size;
238243

239244
private:
240245
static uint8_t crc8(const uint8_t *data, uint8_t len);

0 commit comments

Comments
 (0)