Not able to read IMU data.

Dear Team,

I have DWM3001C module which has nRF52833 chip and multiple sensors (IMU,accelerometer ,magnetometer,etc). But currently with my code, I am not able to read the data (of lis2dh12 accelerometer). I think it might be because of wrong configuration values of scl,sda and sensor address. I am attaching my code. I am getting this error :

00> <error> app: ERROR 33281 [NRF_ERROR_DRV_TWI_ERR_ANACK] at D:\nRF5_SDK_17.1.0_ddde560\nRF5_SDK_17.1.0_ddde560\examples\Myprojects\twi_scanner\main.c:88
00>
00> PC at: 0x00000623
00>
00> <error> app: End of error report

Please let me know if anyone finds the issue in the code or provide the code which directly reads the data of lis2dh12 sensor.

Thank you

#include <stdbool.h>
#include <stdint.h>
#include "nrf_delay.h"
#include "nrf_gpio.h"
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#define TWI_INSTANCE_ID 0
#define LIS2DH12_ADDR        0x19 // I2C address of LIS2DH12
#define LIS2DH12_REG_WHO_AM_I 0x33 // Who Am I register
#define LIS2DH12_REG_CTRL_REG1 0x20 // Control Register 1
#define LIS2DH12_REG_OUT_X_L 0x28 // Output X Low register
#define LIS2DH12_REG_OUT_X_H 0x29

#include "lis2dh12.h"
#include "lis2dh12_internal.h"

static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID); // TWI instance

// Function for TWI initialization
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = SCL_PIN,
       .sda                = SDA_PIN,
       .frequency          = NRF_DRV_TWI_FREQ_400K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

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

    nrf_drv_twi_enable(&m_twi);
}

static volatile bool m_xfer_done = false;
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
    m_xfer_done = true;
}

// Function for reading data from LIS2DH12 accelerometer
void lis2dh12_read_acceleration(int16_t *x, int16_t *y, int16_t *z)
{
    ret_code_t err_code;
    uint8_t data[6];

    // Set the address from which to read accelerometer data
    uint8_t reg_address = LIS2DH12_REG_OUT_X_L;

    // Send the register address over TWI to the accelerometer
    err_code = nrf_drv_twi_tx(&m_twi, LIS2DH12_ADDR, &reg_address, 1, true);
    APP_ERROR_CHECK(err_code);

    // Receive accelerometer data over TWI
    err_code = nrf_drv_twi_rx(&m_twi, LIS2DH12_ADDR, data, sizeof(data));
    APP_ERROR_CHECK(err_code);

    // Extract accelerometer data from received buffer
    *x = (int16_t)((data[1] << 8) | data[0]);
    *y = (int16_t)((data[3] << 8) | data[2]);
    *z = (int16_t)((data[5] << 8) | data[4]);
}

int main(void)
{
    ret_code_t err_code;
    int16_t accel_x, accel_y, accel_z;

    // Initialize logging
    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    // Initialize TWI (I2C)
    twi_init();

    // Verify connection to LIS2DH12
    uint8_t who_am_i;
    err_code = nrf_drv_twi_rx(&m_twi, LIS2DH12_ADDR, &who_am_i, 1);
    APP_ERROR_CHECK(err_code);

     nrf_delay_ms(2);
    if (who_am_i != 0x33) {
        printf("Failed to connect to LIS2DH12\r\n");
        while (true);
    }

    // Configure accelerometer
    uint8_t ctrl_reg1_data = 0x57; // Normal mode, XYZ axes enabled, 50Hz data rate
    uint8_t reg_address = LIS2DH12_REG_CTRL_REG1;
    
    // Send control register address over TWI to the accelerometer
    err_code = nrf_drv_twi_tx(&m_twi, LIS2DH12_ADDR, &reg_address, 1, true);
    APP_ERROR_CHECK(err_code);
    
    // Write control register data
    err_code = nrf_drv_twi_tx(&m_twi, LIS2DH12_ADDR, &ctrl_reg1_data, 1, false);
    APP_ERROR_CHECK(err_code);

    printf("LIS2DH12 Accelerometer Initialized!\r\n");

    // Main loop
    while (true)
    {
        lis2dh12_read_acceleration(&accel_x, &accel_y, &accel_z);
        printf("Acceleration: X=%d, Y=%d, Z=%d\r\n", accel_x, accel_y, accel_z);
        nrf_delay_ms(1000); // Delay between readings
    }
}

.

Related