RobTillaart / SHT31_SW

Arduino library for the SHT31 temperature and humidity sensor - using SoftWire
MIT License
4 stars 0 forks source link

SHT30 read fails with SoftWire (STM32) #12

Closed caiotbc closed 5 months ago

caiotbc commented 5 months ago

Hey, I'm not sure if I should be creating a new issue since it seems directly related to #5, but I can't read anything from an SHT30 sensor. I am trying to run the demo example, getError() returns code 0x82 if placed right after readStatus(), although the isConnected() call above it seems to return 1 as expected. By reading the other issues, it appears that using the AVR exclusive SWW library would probably solve the issue, but I can't since my board has an STM32F103C8T6. Did anyone manage to get it working using SoftWire? Thanks.

RobTillaart commented 5 months ago

Thanks for opening the issue. I will come back to it asap.

RobTillaart commented 5 months ago

...my board has an STM32F103C8T6. Did anyone manage to get it working using SoftWire? Thanks.

Sorry, up now (2024.05.30) I have not seen a working version. Furthermore I have very little to no STM32 experience so I can't provide a solution.

I have created these repositories (SHT31_SW and SHT31_SWW) so people have a starting point to give it a try.

caiotbc commented 5 months ago

Got it working now! At least for me, the problem was not your library at all. As the SoftWire author comments on #9 you need to set the Tx and Rx buffers. Adding

char swTxBuffer[16]; 
char swRxBuffer[16];

as global variables and then

 sw.setTxBuffer(swTxBuffer, sizeof(swTxBuffer));
 sw.setRxBuffer(swRxBuffer, sizeof(swRxBuffer));` 

right before the sw.begin() on the SHT31_demo file solves the issue for my SHT30 sensor. I wonder if that would help on the SHT85 though, or if it is an oddity caused by the stm32 chip that has nothing to do with the other issue. Thanks for your quick answer!

RobTillaart commented 5 months ago

Goodmorning, Thanks for posting the solution you found,

Do you have time to see if it can be integrated in the library? Would be appreciated!

1) add the two variables to the SHT31_SW.h file (as private) 2) change the begin() function in SHT31_SW.cpp

bool SHT31_SW::begin()
{
  if ((_address != 0x44) && (_address != 0x45))
  {
    return false;
  }
  _softWire->begin();
  _softWire->setTxBuffer(swTxBuffer, sizeof(swTxBuffer));
  _softWire->setRxBuffer(swRxBuffer, sizeof(swRxBuffer));
  return reset();
}

Or maybe these 2 calls need to be set before the _softWire->begin(). If that works I can include it in the library.

caiotbc commented 5 months ago

I can confirm it works with the 2 integrations you suggested on the SHT31 library. As for the placement, it does not appear to matter whether the calls are made after or before the _softWire->begin(), as long as they are present the code works as expected. I have experimented with different buffer sizes, I don't know anything about the inner workings of the SoftWire library but I can confirm that if the buffers are of size 5 or less the code stops working, anything 6 or greater and the sensor returns the correct values.

Out of curiosity, your begin function calls _softWire->begin() but your provided code example SHT31_demo.ino also has a sw.begin(); on line 29, is it redundant?

And just in case it has anything to do with processor clocks, I would like to point out that this is a custom board and my MCU is running on 8MHz without a crystal oscillator, as opposed to the 72MHz you would find on a standard bluepill dev board.

RobTillaart commented 5 months ago

@caiotbc Thanks for testing, really appreciated!

6 bytes is the minimum as the SHT31 library has calls that size

bool SHT31::readData(bool fast)
{
  uint8_t buffer[6];
  if (readBytes(6, (uint8_t*) &buffer[0]) == false)

Out of curiosity, your begin function calls _softWire->begin() but your provided code example SHT31_demo.ino also has a sw.begin(); on line 29, is it redundant?

Looks like it, however it is used to be able to call sw.setClock(). (which I see I set it once to 10.000 instead of 100.000 - in my local code)

Again thanks for testing, I will work on a patch to have this fix into the library.

RobTillaart commented 5 months ago

Created a develop branch with the solution, When time permits I will do some additional tests

RobTillaart commented 5 months ago

Found some test time, SHT31 + AVR UNO + SHT31_demo.ino

SHT31_SW_LIB_VERSION:   0.3.0
CON:    1
8010
    8912    19.8    63.6    0
    8876    19.7    63.4    0
    8884    19.8    63.5    0
    8880    19.7    63.5    0
    8872    19.8    63.5    0
    8884    19.7    63.6    0
    8884    19.7    63.5    0
    8880    19.7    63.6    0
    8872    19.7    63.6    0
    8884    19.7    63.6    0
    8884    19.6    63.7    0
    8876    19.7    63.7    0

Works OK, so I'm going to prepare the develop branch / PR for merging.