Topics

Reading data from LSM6DS33 , I2C


Edyta Bosacka <edyta.bosacka@...>
 

I am trying to get Who I am data from accelerometer LSM6DS33, but I cannot receive them.  I' m getting this error: I2C transfer errorI am: 0. What can be the reason of this? 

#define LSM6DS33_XG_WHO_AM_I_ADDR                            0xF0

static int i2c_read_byte(struct device *i2c_dev_0uint8_t *datauint32_t num_bytesuint16_t addr ){

    uint8_t wr_addr;
    struct i2c_msg msgs[2];
    
    /*Register addres*/
    wr_addr = addr;

    /*Setup I2C messages*/

    /*Send the address to read*/
    msgs[0].buf = &wr_addr;
    msgs[0].len = 1U;
    msgs[0].flags = I2C_MSG_WRITE;

    /*read from device*/
    msgs[1].buf = data;
    msgs[1].len = num_bytes;
    msgs[1].flags = I2C_MSG_READ | I2C_MSG_STOP;

    return i2c_transfer(i2c_dev_0, &msgs[0],2I2C_ADDR);

}


//Read Who i am
    data[0] = 0x00;
    int err = i2c_read_byte(i2c_dev_0, &data[0], 1LSM6DS33_XG_WHO_AM_I_ADDR);
    if(err){
        printk("I2C transfer error");
    }
    printk("I am: %s \n"data[0]);


Lawrence King
 

Hi Edyta

 

Your code doesn’t show what value you used for I2C_ADDR. You will not get an ACK on the I2C bus if you have the wrong I2C addr for the LSM3DS33. Without an ACK, you get a bus error.

 

I think your I2C_ADDR should be 0x6B.

 

The WHO_AM_I register in the  LSM3DS33 is 0x0F, not 0xF0.

 

Lawrence King

Principal Developer

+1(416)627-7302

 

From: users@... <users@...> On Behalf Of Edyta Bosacka
Sent: Thursday, July 23, 2020 9:35 AM
To: users@...
Subject: [Zephyr-users] Reading data from LSM6DS33 , I2C

 

I am trying to get Who I am data from accelerometer LSM6DS33, but I cannot receive them.  I' m getting this error: I2C transfer errorI am: 0. What can be the reason of this? 

 

#define LSM6DS33_XG_WHO_AM_I_ADDR                            0xF0

 

static int i2c_read_byte(struct device *i2c_dev_0uint8_t *datauint32_t num_bytesuint16_t addr ){

 

    uint8_t wr_addr;

    struct i2c_msg msgs[2];

    

    /*Register addres*/

    wr_addr = addr;

 

    /*Setup I2C messages*/

 

    /*Send the address to read*/

    msgs[0].buf = &wr_addr;

    msgs[0].len = 1U;

    msgs[0].flags = I2C_MSG_WRITE;

 

    /*read from device*/

    msgs[1].buf = data;

    msgs[1].len = num_bytes;

    msgs[1].flags = I2C_MSG_READ | I2C_MSG_STOP;

 

    return i2c_transfer(i2c_dev_0, &msgs[0],2I2C_ADDR);

 

}

 

 

//Read Who i am

    data[0] = 0x00;

    int err = i2c_read_byte(i2c_dev_0, &data[0], 1LSM6DS33_XG_WHO_AM_I_ADDR);

    if(err){

        printk("I2C transfer error");

    }

    printk("I am: %s \n"data[0]);

 


Adam Podogrocki
 

Edyta,

WHO_AM_I_REG should be the third parameter, not forth.

Cheers,
Adam


On Thu, 23 Jul 2020 at 16:47, Lawrence King <lawrence.king@...> wrote:

Hi Edyta

 

Your code doesn’t show what value you used for I2C_ADDR. You will not get an ACK on the I2C bus if you have the wrong I2C addr for the LSM3DS33. Without an ACK, you get a bus error.

 

I think your I2C_ADDR should be 0x6B.

 

The WHO_AM_I register in the  LSM3DS33 is 0x0F, not 0xF0.

 

Lawrence King

Principal Developer

+1(416)627-7302

 

From: users@... <users@...> On Behalf Of Edyta Bosacka
Sent: Thursday, July 23, 2020 9:35 AM
To: users@...
Subject: [Zephyr-users] Reading data from LSM6DS33 , I2C

 

I am trying to get Who I am data from accelerometer LSM6DS33, but I cannot receive them.  I' m getting this error: I2C transfer errorI am: 0. What can be the reason of this? 

 

#define LSM6DS33_XG_WHO_AM_I_ADDR                            0xF0

 

static int i2c_read_byte(struct device *i2c_dev_0uint8_t *datauint32_t num_bytesuint16_t addr ){

 

    uint8_t wr_addr;

    struct i2c_msg msgs[2];

    

    /*Register addres*/

    wr_addr = addr;

 

    /*Setup I2C messages*/

 

    /*Send the address to read*/

    msgs[0].buf = &wr_addr;

    msgs[0].len = 1U;

    msgs[0].flags = I2C_MSG_WRITE;

 

    /*read from device*/

    msgs[1].buf = data;

    msgs[1].len = num_bytes;

    msgs[1].flags = I2C_MSG_READ | I2C_MSG_STOP;

 

    return i2c_transfer(i2c_dev_0, &msgs[0],2I2C_ADDR);

 

}

 

 

//Read Who i am

    data[0] = 0x00;

    int err = i2c_read_byte(i2c_dev_0, &data[0], 1LSM6DS33_XG_WHO_AM_I_ADDR);

    if(err){

        printk("I2C transfer error");

    }

    printk("I am: %s \n"data[0]);

 


Edyta Bosacka <edyta.bosacka@...>
 

Hi 🙂
I want to read some data (axis_X) from accelerometer but I constantly get "0" answer. 



I think my code is fine, but I dont know what could be a reason of my problem 🙁




Lawrence King
 

Did you power up the Accelerometer? You should read the full datasheet, here is what is says about power:

 

Operating modes

The LSM6DS33 has three operating modes available:

only accelerometer active and gyroscope in power-down

only gyroscope active and accelerometer in power-down

both accelerometer and gyroscope sensors active with independent ODR

The accelerometer is activated from power down by writing ODR_XL[3:0] in CTRL1_XL

(10h) while the gyroscope is activated from power-down by writing ODR_G[3:0] in

CTRL2_G (11h). For combo mode the ODRs are totally independent.

 

There are a lot of registers (about 60) in this chip, you need to setup the chip before you can use it. Did you set the output data rates? The Fifo? The filters?

 

Lawrence King

Principal Developer

+1(416)627-7302

 

From: users@... <users@...> On Behalf Of Edyta Bosacka
Sent: Friday, July 24, 2020 5:27 AM
To: users@...
Subject: [Zephyr-users] Reading data from LSM6DS33 , I2C

 

Hi 🙂

I want to read some data (axis_X) from accelerometer but I constantly get "0" answer. 

 


I think my code is fine, but I dont know what could be a reason of my problem
🙁