This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nrf52810: TWI interfacing with SHTW2 Humidity/Temperature Sensor

Hi,

I am using a custom board with the nrf52810 and trying to interface with the SHTW2 sensor. The sensor has an I2C interface and hence I use the TWI peripheral for this. However, the transfer of data does not take place. Below is my read function:

void SHTW2_read(){
    uint8_t m_sample[2];
    uint8_t writeMode[] = {0x70};
    uint8_t readMode[] = {0x71};
    uint8_t reg[2] = {0x78, 0x66};
    ret_code_t err_code;
    //while (m_xfer_done == false);
              
    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, writeMode, sizeof(writeMode), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    nrf_delay_ms(5);

    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, reg, sizeof(reg), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    nrf_delay_ms(5);

    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, SHTW2_ADDR, readMode, sizeof(readMode), true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    m_xfer_done = false;

    err_code = nrf_drv_twi_rx(&m_twi, SHTW2_ADDR, m_sample, sizeof(m_sample));
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
    temperature = m_sample[0] << 8 | m_sample[1];
}

I am using the TWI sensor example as base for this, but the code gets stuck at the while loop waiting for the handler to be called where the flag m_xfer_done is set. The handler never gets called. Can anyone point me as to where I am going wrong? May be my fundamental understanding of TWI is wrong. Thanks for the help in advance.

  • Hi,

     

    In which context are you calling the function SHTW2_read() ? If this is called from an interrupt context, your TWI handler, which sets the m_xfer_done = true, must be a higher priority.

    Since it looks like you are using the TWI driver in a block fashion, you can just omit the handler, like this:

    err_code = nrf_drv_twi_init(&m_my_twi_instance, &my_twi_config, NULL, NULL);

     

    This way, each call will only return when the bus itself it done.

     

    Kind regards,

    Håkon

  • Hi,

    I am calling the SHTW2_read() in a while loop in my main function. 

    I managed to get it working using the blocking method until I try reading, then I get a NRF_DRV_TWI_EVT_ADDRESS_NACK error. Do you have any idea of what this points to? Thanks for the help in advance.

    Regards,

    Faizan  

  • Your sensor is NACKing the transaction, which could be due to several reasons (incorrect address, weak pull-up resistors etc). Have you tried scoping the pins to see how the actual bus communication looks like?

     

    Kind regards,

    Håkon

  • Hi,

    I will scope the pins and post the result soon. However, I do want to make sure that I am doing the right things in the code. Below is a snapshot from the SHTW2 datasheet showing the measurement reading process:

    So according to the above flow, we need to write the I2C address + write bit to the I2C bus first wait for the ACK, then write the two bytes of Command one by one. Then write the I2C address + read Bit and then read the sensor data. The I2C address is 0x70 and I think I have covered all the above process in the code I posted. Am I moving in the right direction?

    The place where the NACKing occurs is right at the end i.e. when I try to read the sensor data. 

  • Hi,

    I probed the pins I do see the change in waveform when I am sending data. I have external pullup resistors so I removed the internal Pullup setting for the TWI pins. But I still get the NACK at the reading stage. Do you have any idea as to why this would be happening? I thinking that may be I am not sending in the command properly. But not sure. Let me know if you do have any idea on this. Thanks in advance.

Related