STS30 temperature sensor I2C code

I am trying to read temperature value from STS30 temperature sensor using this simple code snippet: 

During debugging, my debugger stuck at line "if (STS30_read_temp(temperature)==true)". When I step into this line, I can see debugger is stuck at line "return result" of function "nrf_drv_twi_tx" in nrf_drv_twi.h file. 

Can you check the code and advise what am I doing wrong here. 

I have attached the command sequence for reading temperature from STS30 sensor. 

  

Thanks

Parents
  • Hello,

    Please make sure to check the error code returned by nrf_drv_twi_rx - if you do not check the error code you will not have any way of knowing if the function call unexpectedly failed, and so you will be stuck in the while loop forever waiting for the transfer to complete, without the transfer ever having started.

    Please also make sure to have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make your logger output a detailed error message whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.
    Use this error message to find the function that returned the error code, and look at the function's API reference to see why it returned this particular error code.

    Best regards,
    Karl

  • I thought the address of this sensor was 0b0011xxxr where xxx is the state of the external pins A2/A1/A0. Try this (assuming all 3 address pins are tied low):

        uint8_t STS30_Address = 0x18; // address of the sensor

  • There is just 1 address pin on this device. Which if grounded gives address of 0x4A. I have already been able to successfully scan for STS30 on I2C address 0x4A. 

    I think the problem is to find if this code below is sending the Address/w/Ack/0x2C/Ack/0x10/Ack/Stop sequence as sensor expects to initiate a measurement? 

    tx_buf[0] = 0x2C;
    tx_buf[1] = 0x10;

    err_code = nrf_drv_twi_tx(&m_twi, STS30_Address, tx_buf, 2, false);

Reply
  • There is just 1 address pin on this device. Which if grounded gives address of 0x4A. I have already been able to successfully scan for STS30 on I2C address 0x4A. 

    I think the problem is to find if this code below is sending the Address/w/Ack/0x2C/Ack/0x10/Ack/Stop sequence as sensor expects to initiate a measurement? 

    tx_buf[0] = 0x2C;
    tx_buf[1] = 0x10;

    err_code = nrf_drv_twi_tx(&m_twi, STS30_Address, tx_buf, 2, false);

Children
  • It seems the originally posted code works when I remove "while (m_xfer_done == false){}" lines in STS30_read_command() and STS30_write_command() functions. 

    Do I really need twi_handler() function in my code? 

  • Hello again, Kaveh.m

    Thank you for your patience with this.

    Kaveh.M said:
    I have already been able to successfully scan for STS30 on I2C address 0x4A. 
    Kaveh.M said:
    Without a breakpoint, nothing different happens from the case I do not have APP_ERROR_CHECK. Debugger gets stuck in the while loop. 

    Thank you for confirming that the addressing and hardware connections of the setup is working as expected, and that the APP_ERROR_CHECK does not trigger - this means that the function call returns NRF_SUCCESS as expected.

    Kaveh.M said:

    It seems the originally posted code works when I remove "while (m_xfer_done == false){}" lines in STS30_read_command() and STS30_write_command() functions. 

    Do I really need twi_handler() function in my code? 

    The reason for the m_xfer_done flag in the TWI peripheral examples is that the device might be doing other things in the meantime, while waiting for the transfer to complete.
    I just looked more closely through your code, and it seems that you do not provide the twi_handler function in your nrf_drv_twi_init call - this is likely the issue here.
    Since the nrf_drv_twi_init function is provided NULL as the callback function, there will be no twi events forwarded to the application layer.
    Please add twi_handler as your third argument to nrf_drv_twi_init, and see if this resolves your issue.

    Best regards,
    Karl

  • Thanks Karl. I modified the code as you mentioned and added twi_handler to the nrf_drv_twi_init. Now I can see the while loops are passed properly in the STS30_read_command() and STS30_write_command() by the debugger and temperature is measured properly. Please see below is the modified code: 

    The only remaining issue is that the temperature is now measured properly 1 time. As you can see in the code, there is a while(true) and nrf_delay_ms(1000) in the main function, that repeats this measurement every 1 second. The first time debugger enters the loop, it executes STS30_detect() fine and STS30_read_temp(temperature) fine with correct measurement. But after 1s in the second round of measurement, it it executes STS30_detect() fine and sets (STS30_detected = true) but then during execution of STS30_read_temp(temperature) debugger transition to line 100 in app_error_weak.c snd stuck there. Please see below.

  • I am happy to hear that the suggested change resolved your initial issue!

    Kaveh.M said:
    The first time debugger enters the loop, it executes STS30_detect() fine and STS30_read_temp(temperature) fine with correct measurement. But after 1s in the second round of measurement, it it executes STS30_detect() fine and sets (STS30_detected = true) but then during execution of STS30_read_temp(temperature) debugger transition to line 100 in app_error_weak.c snd stuck there. Please see below.

    What does your logger output when this happens? Could you confirm that you have DEBUG defined in your preprocessor defines, like I mentioned in my initial comment?

    Best regards,
    Karl

  • Yes, DEBUG is defined in the preprocessor defines as before. 

    How can I see the logger output? I have only have JLINK connected to my board. 

Related