cancel
Showing results for 
Search instead for 
Did you mean: 

I read the data of the accelerometer and magnetic sensor from STEVAL-MKI173V using I2C communication. However, 0 is output for the magnetic sensor data. How can I read the value of the magnetic sensor accurately? Please teach.

KTaky.1
Associate II

In addition, what formula do you use to convert the magnetic sensor data to azimuth?

There was no error in the I2C communication itself.

In addition, I2C communication was performed according to the communication format of LSM303AH.

The program is as follows.

Write 0x04 to the CTRL2_A register // I2C interface enabled

// Register address automatically incremented

// during multiple byte access with a serial

// interface.

Write 0x01 to the CTRL5_A register // Configure INT3 pin as digital output and route

//data-ready signal

Write 0x40 to the CTRL1_A register // Turn on the accelerometer, ODR = 100 Hz,

//HR(High-Resolution) modes, Full Scale = ±2 g

Write 0x01 to the CFG_REG_C_M register // Configure INT3 pin as digital output and route data-ready signal

Write 0x02 to the CFG_REG_B_M register // Enable offset cancellation of magnetic sensor

Write 0x8C to the CFG_REG_A_M register // Temperature compensation enabled,

//ODR = 100 Hz

//Continuous and High-Resolution modes

Read the DRDY in STATUS_A register

When DRDY = 1

Read the 

OUT_X_L_A_REG,OUT_X_H_A_REG,

OUT_Y_L_A_REG,OUT_Y_H_A_REG,

OUT_Z_L_A_REG and OUT_Z_H_A_REG register

Store the read data in a defined variable.

Read the Zyxda in STATUS_REG_M register

When Zyxda = 1

Read the 

OUT_X_L_M_REG,OUT_X_H_M_REG, // Read 0x00

OUT_Y_L_M_REG,OUT_Y_H_M_REG, //

OUT_Z_L_M_REG and OUT_Z_H_M_REG register

Store the read data in a defined variable.

5 REPLIES 5
Eleon BORLINI
ST Employee

Hi @KTaky.1​ ,

the configuration you wrote seems ok...

Could you please have a look to the lsm303ah_read_data_polling.c C example on Github?

This sample code configures and acquires data from both accelerometer and magnetometer in polling mode.

You should check the order of the register configuration if there is something wrong...

You could also try with setting the bits MD[1:0] to '01' in CFG_REG_A_M (60h), to enable the single measurement mode and see if something changes.

-Eleon

KTaky.1
Associate II

Hi Eleon BORLINI (ST Employee),

Thank you for your advice.

I will try as advised.

KTaky.1
Associate II

Hi Eleon BORLINI (ST Employee),

In addition, I have a question. What kind of formula do you use for converting magnetic sensor data to azimuth?

The theoretical formula I searched on the Internet was described as the following formula. Is this formula correct?

Please tell me.

Well-formed formula

heading = atan2 (My, Mx) * 180 / M_PI;

if (heading <0)

{

heading = 360 + heading;

}

heading: Azimuth

Mx, My: Magnetic flux density (value obtained by converting the magnetic sensor output value into 16-bit data and multiplying it by sensitivity)

M_PI: Pi

Eleon BORLINI
ST Employee

Hi @KTaky.1​ ,

you are right, the "atan2" formula is the standard one used for heading / azimuth angle calculation.

For completeness, I suggest you to check also the Appendix section A.2 of the AN3192 application note, that describes the steps of the heading calculation.

-Eleon

KTaky.1
Associate II

Hi Eleon BORLINI,

Thank you for your reply.

If the output value of the magnetic sensor can be read, I will refer to it. Thank you for teaching.