Skip to content

Commit e11145d

Browse files
authored
Update SHT21.cpp
1 parent 5d33aca commit e11145d

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

SHT21.cpp

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
// Licence : Open-source !
88
//==============================================================================
99
//==============================================================================
10-
// Use with any SHT21 breakout. Check ours:
11-
// https://e-radionica.com/en/sht21-humidity-and-temperature-sensor.html
10+
// Use with any SHT21 breakout. Check ours:
11+
// https://e-radionica.com/en/sht21-humidity-and-temperature-sensor.html
1212
// If any questions,
1313
// just contact [email protected]
14-
//==============================================================================
15-
14+
//==============================================================================
15+
1616
#include <Arduino.h>
1717
#include <Wire.h>
1818
#include <SHT21.h>
@@ -23,15 +23,15 @@
2323
float SHT21::getHumidity(void) {
2424
uint16_t result; // return variable
2525

26-
result = readSensor_hm(TRIGGER_RH_MEASUREMENT_HM);
26+
result = readSensor_hm(TRIGGER_RH_MEASUREMENT_NHM);
2727

2828
return CalcRH(result);
2929
}
3030

3131
float SHT21::getTemperature(void) {
3232
uint16_t result; // return variable
3333

34-
result = readSensor_hm(TRIGGER_T_MEASUREMENT_HM);
34+
result = readSensor_hm(TRIGGER_T_MEASUREMENT_NHM);
3535

3636
return CalcT(result);
3737
}
@@ -93,14 +93,22 @@ uint16_t SHT21::readSensor_hm(uint8_t command) {
9393
uint8_t checksum;
9494
uint8_t data[2];
9595
uint16_t result;
96-
96+
uint8_t n = 0;
97+
uint8_t d;
98+
99+
if(command == TRIGGER_RH_MEASUREMENT_HM || command == TRIGGER_RH_MEASUREMENT_NHM) d = 30;
100+
if(command == TRIGGER_T_MEASUREMENT_HM || command == TRIGGER_T_MEASUREMENT_NHM) d = 85;
101+
97102
Wire.beginTransmission(I2C_ADD);
98103
Wire.write(command);
99104
Wire.endTransmission();
100-
105+
delay(d);
101106
Wire.requestFrom(I2C_ADD,3);
107+
102108
while(Wire.available() < 3) {
103-
delayMicroseconds(85);
109+
delay(10);
110+
n++;
111+
if(n>10) return 0;
104112
}
105113

106114
data[0] = Wire.read(); // read data (MSB)
@@ -147,5 +155,3 @@ uint8_t SHT21::CRC_Checksum(uint8_t data[], uint8_t no_of_bytes, uint8_t checksu
147155
if (crc != checksum) return 1;
148156
else return 0;
149157
}
150-
151-

0 commit comments

Comments
 (0)