Sensirion / arduino-sht

Repository for Sensirion humidity and temperature sensor support on Arduino
BSD 3-Clause "New" or "Revised" License
68 stars 43 forks source link

First Reading "High" with autodetect example using SHT4x #27

Closed floatAsNeeded closed 10 months ago

floatAsNeeded commented 1 year ago

Hi!

I just wanted to note an issue when using the example sht-autodetect with the SHT40. I'm not sure what the cause of the issue is, but when I use the SHT40 the first reading is not a proper value and gradually cools down while reading. It looks like the heater goes on for a short period or something like that. This issue doesn't happen when using the SHT3x series. This is easily fixed by just using this line instead:

SHTSensor sht(SHTSensor::SHT4X);

Here is an example of what the issue looks like in the Serial:

15:46:14.342 -> init(): success
15:46:14.389 -> SHT:
15:46:14.389 ->   RH: 70.45
15:46:14.389 ->   T:  29.50
15:46:15.360 -> SHT:
15:46:15.360 ->   RH: 67.22
15:46:15.405 ->   T:  22.48
15:46:16.376 -> SHT:
15:46:16.376 ->   RH: 66.68
15:46:16.421 ->   T:  22.06
15:46:17.392 -> SHT:
15:46:17.392 ->   RH: 67.05
15:46:17.438 ->   T:  21.95
15:46:18.410 -> SHT:
15:46:18.410 ->   RH: 67.57
15:46:18.410 ->   T:  21.93
15:46:19.431 -> SHT:
15:46:19.431 ->   RH: 67.94
15:46:19.431 ->   T:  21.87
15:46:20.479 -> SHT:
15:46:20.479 ->   RH: 68.36
15:46:20.479 ->   T:  21.85
15:46:21.487 -> SHT:
15:46:21.487 ->   RH: 68.65
15:46:21.487 ->   T:  21.83
15:46:22.472 -> SHT:
15:46:22.472 ->   RH: 68.90
15:46:22.519 ->   T:  21.81
15:46:23.501 -> SHT:
15:46:23.501 ->   RH: 69.06
15:46:23.501 ->   T:  21.77

Any Ideas on how to possibly fix that?

Just a note: I'm using an Atmega328P on breadboard with Arduino IDE 1.8.19

winkj commented 1 year ago

Thanks for the report. I was using an ESP32 with an Adafruit SHT40 board, and could reproduce this successfully. An interesting case for sure!

Any Ideas on how to possibly fix that?

Your workaround - to initialize SHTSensor as an SHT4X, rather than using the automatic detection - seems to work fine.

Other than that, my guess is that since the SHT3x is on the same I2C address, attempting to communicate with a SHT3x causes the SHT4x to do something unexpected. This would also explain why the user who contributed the SHT4x support had to add an extra delay here to make it work (since the SHT4x was in an strange state): https://github.com/Sensirion/arduino-sht/blob/master/SHTSensor.cpp#L317

To verify this, I switched out the order for the autodetect sensors here (https://github.com/Sensirion/arduino-sht/blob/master/SHTSensor.cpp#L277) to try to detect the SHT4x first, and have since not been able to recreate the issue. Unfortunately, I didn't have an SHT3x available to make sure it didn't break that.

If you have a moment, I'd appreciate if you could try that as well and let me know if this fixes it for your setup, too.

winkj commented 1 year ago

Here's the array I'm using now:

const SHTSensor::SHTSensorType SHTSensor::AUTO_DETECT_SENSORS[] = {
  SHT4X,
  SHT3X,
  SHT3X_ALT,
  SHTC1
};
floatAsNeeded commented 1 year ago

Thank you very much for the detailed reply! I will try that array with both the SHT3x and 4x and report back.

floatAsNeeded commented 1 year ago

I just tried with my setup and now it's working perfectly fine for both the SHT4x series and the SHT3x series using autodetect.

I think the issue as you noted was regarding the SHT4x that it's a bit "slow" to set up properly, so this issue is solved by putting it first on the array and it doesn't affect the stability at the start of the SHT3x

Thank you very much for your help! I guess we can close the issue and modify the library to change the order in the array.

By the way, something that is not related to the issue, but is there a way to know which sensor has been detected? If it is the SHT3x or 4x

floatAsNeeded commented 1 year ago

Regarding my last question, I found a way to get the SHT class of the sensor by calling sht.mSensorType if someone wants to know. It will return a number and that number will have to be looked up to know which sensor you're talking to.

enum SHTSensorType { /** Automatically detect the sensor type (only i2c sensors listed above) */ AUTO_DETECT, // i2c Sensors: /** SHT3x-DIS with ADDR (sensor pin 2) connected to VSS (default) */ SHT3X, SHT85, /** SHT3x-DIS with ADDR (sensor pin 2) connected to VDD */ SHT3X_ALT, SHTC1, SHTC3, SHTW1, SHTW2, SHT4X };

For example, the SHT4x will return 8 and SHT3x 1

winkj commented 1 year ago

I'll reopen this while we're testing the fix

floatAsNeeded commented 1 year ago

Just wanted to leave a comment that I'm testing the setup since then and had no issues encountered with both the SHT3x and SHT4x connected, using the fix that you suggested.

In my Setup, I'm testing them together by connecting a multiplexer and I use the same address for both.