Unable to detect a i2c sensor using twi_scanner example

I want to use a sensor with nrf52 dev kit. for the same purpose I am first scanning sensor using twi_scanner example but it does not detect.


configuration used for nrf_drv_twi_config_t is :

>>
    const nrf_drv_twi_config_t twi_config = {
       .scl                = 8,
       .sda                = 14,
       .frequency          = NRF_TWI_FREQ_250K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = true
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);


I have tried the .frequency as NRF_TWI_FREQ_250K, NRF_DRV_TWI_FREQ_100K, NRF_TWI_FREQ_400K and also checked on other GPIOs but still it did not worked.
I checked the same sensor with Ardunio and Raspberry Pi , there it works fine.

Parents
  • Try using the default pins. I see that you have tested on different pins, but perhaps you were unlucky. Try to use pins 26 and 27, as these are not used by anything else by default. 

    What does nrf_drv_twi_init() return? That is, what is err_code after nrf_drv_twi_init() is done executing?

    Initializing the twi is something you need to do, but it doesn't start the scanning?

    Did you leave the rest of the main.c file from the twi_scanner example unmodified? 

    Best regards,

    Edvin

  • First time I used the default pins , but it was unsuccessful. The rest part of the code is unmodified. 

    Additionally I tried a different sensor , nrf scanner example detects it..

    Problem is that the sensor which I want use for development is not getting dedicated. 

  • Edvin said:
    What does nrf_drv_twi_init() return? That is, what is err_code after nrf_drv_twi_init() is done executing?

    Did you check this?

    Prayjit said:
    The rest part of the code is unmodified

    Can you show me the rest of your code? Perhaps you can upload the main.c file?

    Prayjit said:

    Additionally I tried a different sensor , nrf scanner example detects it..

    Do you mean that it detects its address, or that you are able to use the sensor? It was also a TWI sensor, right?

  • Here is my main.c

    #include <stdio.h>
    #include "boards.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "nrf_drv_twi.h"
    #include "nrf_delay.h"
    
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    /* TWI instance ID. */
    #if TWI0_ENABLED
    #define TWI_INSTANCE_ID     0
    #elif TWI1_ENABLED
    #define TWI_INSTANCE_ID     1
    #endif
    
     /* Number of possible TWI addresses. */
     #define TWI_ADDRESSES      127
    
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    
    /**
     * @brief TWI initialization.
     */
    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_config = {
           .scl                = 8,//ARDUINO_SCL_PIN,
           .sda                = 14,//ARDUINO_SDA_PIN,
           .frequency          = NRF_TWI_FREQ_250K,//NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = true,
          // .hold_bus_uninit     =false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi);
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        ret_code_t err_code;
        uint8_t address;
        uint8_t sample_data;
        bool detected_device = false;
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("TWI scanner started.");
        NRF_LOG_FLUSH();
        twi_init();
       // nrf_drv_twi_xfer(&m_twi,NRF_DRV_TWI_XFER_RX,0);
       // nrf_delay_ms(5000);
        NRF_LOG_INFO("TWI scanner started scannning...");
       // while(1)
        for (address = 1; address <= TWI_ADDRESSES; address++)
        {
          NRF_LOG_INFO("scannning...");
            //nrf_delay_ms(500);
            //address=37;
            err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
             NRF_LOG_INFO("%d",err_code);
              //APP_ERROR_CHECK(err_code);
            if (err_code == NRF_SUCCESS)
            {
                detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
            }
            NRF_LOG_FLUSH();
        }
    
        if (!detected_device)
        {
            NRF_LOG_INFO("No device was found.");
            NRF_LOG_FLUSH();
        }
    
        while (true)
        {
            /* Empty loop. */
        }
    }
    
    /** @} */

    For other sensors it detects address as well as i able to use that sensor

  • Perhaps the sensor requires a different voltage than what the nRF provides on the TWI pins? Have you tried measuring the voltage from your other devices where the sensor works, and compare it to the voltage on the nRF?

    Have you tried to analyze the TWI communication using a logic analyzer and compare it with the communication when you are using the other devices?

Reply
  • Perhaps the sensor requires a different voltage than what the nRF provides on the TWI pins? Have you tried measuring the voltage from your other devices where the sensor works, and compare it to the voltage on the nRF?

    Have you tried to analyze the TWI communication using a logic analyzer and compare it with the communication when you are using the other devices?

Children
Related