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

Problem in getting data from sensor(vl53l0x) to nrf52832 using I2C interface

Hi Nordic Support,

I am getting zero distance value from sensor(vl53l0x). I used SLAVE_ADDRESS =0x29 and register address=0x01. My code is mentioned below



 void data_handler(void)
{

    uint16_t dist_buf=0;

    NRF_LOG_INFO("distance: %d (in mm).", data);
}


void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
//printf("event =%d\n",p_event->type);
    switch (p_event->type)
    {
    
        case NRF_DRV_TWI_EVT_DONE:
             if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
             {
               data_handler();
             }
            m_xfer_done = true;
          
            printf("done\n");
            break;
            
        case NRF_DRV_TWI_EVT_ADDRESS_NACK:
              printf("No address ACK on address\n");
            break;
            
        case NRF_DRV_TWI_EVT_DATA_NACK:
              printf("No data ACK on address\n");
            break;
          
        default:
            break;
    }
}


/**
 * @brief Initialize the master TWI
 *
 * Function used to initialize master TWI interface that would communicate with simulated EEPROM.
 *
 * @return NRF_SUCCESS or the reason of failure
 */
static ret_code_t twi_master_init(void)
{
    ret_code_t ret;
    const nrf_drv_twi_config_t config =
    {
       .scl                = TWI_SCL_M,
       .sda                = TWI_SDA_M,
       .frequency          = NRF_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    do
    {
        ret = nrf_drv_twi_init(&m_twi_master, &config, twi_handler,NULL);
        if(NRF_SUCCESS != ret)
        {
            break;
        }
        nrf_drv_twi_enable(&m_twi_master);
    }while(0);
    return ret;
}


static void read_sensor_data()
{
    m_xfer_done = false;

    /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
    ret_code_t err_code = nrf_drv_twi_rx(&m_twi_master, SLAVE_ADDRESS, &data, sizeof(data));
    APP_ERROR_CHECK(err_code);
   // result = (float)(data * 3.0517) ;
   // distance = (uint16_t)result;
    distance = (uint16_t)(data[0] << 8 | data[1]);
    printf("value %d\n",distance);

}


int main(void)
{
    bool erase_bonds;

  uint8_t register_address = 0x01;
  uint8_t buffer={0};

    // Initialize.
    log_init();

   

     ret_code_t err_code;


     twi_master_init();
      
      m_xfer_done=false;

     err_code=nrf_drv_twi_tx(&m_twi_master,SLAVE_ADDRESS,&register_address,sizeof(register_address),false);
     if(err_code == NRF_SUCCESS)
     {
              NRF_LOG_INFO("Device Address and Register Address(3 bit mode+ 5 bit reg address sent");

     }
     while(m_xfer_done == false)
     {
        NRF_LOG_PROCESS();
     }


    while (true)
    {
        nrf_delay_ms(500);

        do
        {
            __WFE();
        }while (m_xfer_done == false);

        read_sensor_data();
        NRF_LOG_FLUSH();
    }

Please suggest whats wrong in this code and why i am getting zero distance value.

Thanks & Regards,

HEY

Parents
  • Hi,

    Have you tried to debug the code to see if data_handler() gets called from your twi_handler() callback?  if you always get 0, it likely means that the data is not being read from the sensor so you're just getting the variable init value. 

    Regards,

    Vidar

  • Hey vidar

    Thanks for the update!

    I have checked by debugging the code to see the data_handler() functionality and it is being called by the twi_handler()

    correctly.

    Now i am getting below mentioned Values always/constantly, whenever going for debugging the code.

    Please Suggest how to proceed on this.

    distance: 255 (in mm).done
    distance: 1 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 1 (in mm).done
    distance: 64 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 186 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 26 (in mm).done
    distance: 4 (in mm).done
    distance: 3 (in mm).done
    distance: 3 (in mm).done
    distance: 3 (in mm).done
    distance: 68 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 130 (in mm).done
    distance: 128 (in mm).done
    distance: 7 (in mm).done
    distance: 19 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done
    distance: 42 (in mm).done
    distance: 0 (in mm).done
    distance: 0 (in mm).done

  • what i meant to tell is Values sequence is always same

  • I'm not familiar with this particular sensor but it seems like you're correctly receiving the data from it, so I guess the problem is with the command sequence. Have you considered porting the sensor library ST mentions in their User guide?

  • hey vidar, from my end i am doing with the considerations of user manual of VL53L0X.

    I dont know what i am missing, please suggest.

  • The user manual (UM2039) seems to describe how you can use their API, but you haven't used it in the code snippets you have posted here. 

  • I ported the arduino code for the VL53L0X to segger. using the c++ compiler instead of the c compiler

    the steps to do are

    1) download and install segger

    2) dowmload SDK16 from nordic.

    3)download the zip-file from this link www.dropbox.com/.../AABE2vPIrNAIi8vJMVvTMxkna

    4)unpack it in /where you put nordic SDK16/examples/peripheral

    5) "open solution" in segger /where you put nordic SDK16/examples/peripheral/vl53lox/pca10040/s132/ses/VL53L0X.emProject

    6)set the correct pins in VL530X.cpp for sda en scl.

    7) built and download it to the nrf52832

    be sure you have SDK16, i am not sure if it compiles on lower versions

    Hope this will work for you!

Reply Children
Related