AnsweredAssumed Answered

How to read the x/y/z and temperature values of MAG3110 correctly?

Question asked by Intruder T on Aug 29, 2018
Latest reply on Sep 4, 2018 by Tomas Vaverka

Hi All,

Am working with MAG3110 and STM32L0..

I just want to read the temperature and x/y/z values.

Here is my code:

void mag_init(){

      //I tried with all sampling rates and OSR too
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf1,2,100);    
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf2,2,100);    
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, ctrl_conf1,2,100);    

//Temperature reading

void read_temp(){
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, &temp_add ,1,100);
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, temp_val,2,100);

//Reading the x/y/z/lsb/msb

void read_reg(){
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, x_msb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &x_msb[1],1,100);
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, x_lsb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &x_lsb[1],1,100);    
        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, y_msb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &y_msb[1],1,100);


        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, y_lsb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &y_lsb[1],1,100);    


        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, z_msb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &z_msb[1],1,100);


        HAL_I2C_Master_Transmit(&hi2c3, MAG_ADDRESS, z_lsb,1,100);        
        HAL_I2C_Master_Receive(&hi2c3, MAG_ADDRESS, &z_lsb[1],1,100);        


void read_full_scl_values(){
        x_val = ((short) (x_msb[0]<<8 | x_lsb[1]));
        y_val = ((short) (y_msb[0]<<8 | y_lsb[1]));
        z_val = ((short) (z_msb[0]<<8 | z_lsb[1]));



But the temperature is always either a constant value at 128 or randomly fluctuates around 127 but never be a consistant or predictable value..

Same case with the x/y/z values too..

X/y/z values are not at all consistant and looks lie am getting spikes in the middle of the data..

And therre is no change "If I BRING A METAL" near to the sensor..

I tried programming with PIC ic too... but the result is same..