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

TH02 sensor using TWI

Hi,

In order to read values from TH02 sensor(Address: 0x40) I first need to give a command i.e. write 0x01(for temperature) and 0x11(for RH) resp into internal CONFIG register(having address 0x03) and then read the values. 

I am unable to understand where to write my commands in the nrf_drv_twi_tx() function after which I can read using nrf_drv_twi_rx() function.

Please let me know if anyone can help with this. Here is the datasheet for TH02 sensor. 

  • Hi,

    Have you seen the TWI Sensor Example? I think this example is a good starting point that you can adapt to the TH02 sensor. 

    For the read sequence, you can try the transfer API and do something like this:

       /*Sensor read sequence*/
       static uint8_t tx_buf[] = {0x01}; //Address Pointer
       static uint8_t rx_buf[2];  // receive buffer for data high and data low
    
       nrf_drv_twi_xfer_desc_t xfer_desc =
           NRF_DRV_TWI_XFER_DESC_TXRX(TH02_ADDR, tx_buf, sizeof(tx_buf), rx_buf, sizeof(rx_buf));
    
       nrf_drv_twi_xfer(&m_twi, &xfer_desc, NRF_DRV_TWI_FLAG_TX_NO_STOP);

    Note: I don't have the sensor to test with, so I'm not sure if the code above will work. To make it easier to debug, you should monitor the NRF_DRV_TWI_EVT_ADDRESS_NACK and NRF_DRV_TWI_EVT_DATA_NACK events in your TWI callback assuming you use the API in non-blocking mode. 

    TWIM driver documentation: TWI master 

Related