Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SHT30 read fails with SoftWire (STM32) #12

Closed
caiotbc opened this issue May 30, 2024 · 8 comments
Closed

SHT30 read fails with SoftWire (STM32) #12

caiotbc opened this issue May 30, 2024 · 8 comments
Assignees
Labels
question Further information is requested

Comments

@caiotbc
Copy link

caiotbc commented May 30, 2024

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
Copy link
Owner

RobTillaart commented May 30, 2024

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

@RobTillaart RobTillaart self-assigned this May 30, 2024
@RobTillaart RobTillaart added the question Further information is requested label May 30, 2024
@RobTillaart
Copy link
Owner

RobTillaart commented May 30, 2024

...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.

@RobTillaart RobTillaart changed the title SHT30 read fails with SoftWire SHT30 read fails with SoftWire (STM32) May 30, 2024
@caiotbc
Copy link
Author

caiotbc commented May 30, 2024

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
Copy link
Owner

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
Copy link
Author

caiotbc commented May 31, 2024

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
Copy link
Owner

RobTillaart commented May 31, 2024

@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
Copy link
Owner

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

@RobTillaart
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants