LSM303DLHC not providing reasonable data

I am interfacing with the LSM303DLHC and the data for the linear acceleration does not seem to be correct.

When reading the data on registers 28h to 2dh, the data is all 0, 64, or 192. (it is very odd that the data across all axis is the same) Also, this data is always changing between the stated values even at rest.

The magnetic field data seems to be correct as each axis has it’s own value which changes upon turning the device

Will someone please check to see if I have setup the device correctly. (Coded in MikroC)

Init code:

    // INIT LSM303DLHC_electronic_compass

    // set CRA_REG_M register
    // 30 Hz update rate
    I2C1_Start();
    I2C1_Wr(0x3c);
    I2C1_Wr(0x00);
    I2C1_Wr(0x14);
    I2C1_Stop();

    // set MR_REG_M register
    // continuous-coversion mode
    I2C1_Start();
    I2C1_Wr(0x3c);
    I2C1_Wr(0x02);
    I2C1_Wr(0x00);
    I2C1_Stop();

    // set CTRL_REG1_A register
    // enable all axis, set x Hz refresh
    I2C1_Start();
    I2C1_Wr(0x32);
    I2C1_Wr(0x20);
    I2C1_Wr(0b00100111);
    I2C1_Stop();

Reading code:

             I2C1_Start();
             I2C1_Wr(0x3c); // LSM303DLHC board device ID
             I2C1_Wr(0x03);
             I2C1_Repeated_Start();
             I2C1_Wr(0x3d);
             USB_out_buffer[24] = I2C1_Rd (1);
             USB_out_buffer[25] = I2C1_Rd (1);
             USB_out_buffer[26] = I2C1_Rd (1);
             USB_out_buffer[27] = I2C1_Rd (1);
             USB_out_buffer[28] = I2C1_Rd (1);
             USB_out_buffer[29] = I2C1_Rd (0);
             I2C1_Stop();
            
             I2C1_Start();
             I2C1_Wr(0x32); // LSM303DLHC board device ID
             I2C1_Wr(0x28);
             I2C1_Repeated_Start();
             I2C1_Wr(0x33);
             USB_out_buffer[15] = I2C1_Rd (1);
             USB_out_buffer[16] = I2C1_Rd (1);
             USB_out_buffer[17] = I2C1_Rd (1);
             USB_out_buffer[18] = I2C1_Rd (1);
             USB_out_buffer[19] = I2C1_Rd (1);
             USB_out_buffer[20] = I2C1_Rd (0);
             I2C1_Stop();

Hi.

Could you please post your wiring diagram and a picture of your setup including all connections?

-Derrill

Hello, SugarBombs.

Could you try changing your reading code to use register address 0xA8 instead of 0x28? For the accelerometer, the MSB of the register address needs to be set to 1 to enable subaddress auto-incrementing (see page 20 of the datasheet).

- Kevin

The code now reads the data after changing the register.

Can anyone tell me what ranges should the data be in? I am working with Pololu example code and the direction value is incorrect. (github.com/pololu/LSM303/blob/m … LSM303.cpp)

The project is currently down and once I have it up again I will update the thread if the data being received.

The raw gyro data should fall within +/-32768 and the accelerometer and magnetometer data should be within +/-2000, though depending on the sensitivity/gain settings for the sensors, it’s likely they will be much lower. What kind of values are you getting? Have you run the Calibrate program to get a set of calibration constants?

- Kevin

hi
i have got minimu-9v2 for my project, but i don’t know how to write code using ccs compiler for PIC controller. if u got code kindly post the full code…

regards

Having worked with PICs and the CCS compiler for some time, I would NOT recommend it for any project involving a lot of math, like navigation. The compiler has too many quirks and I constantly have to check the assembly language listing in order to verify that the compiler actually does what I tell it to do – too often not. The compiler is particularly weak in typecasting expressions and properly distinguishing unsigned versus signed integer operations.

I strongly recommend to use AVR processors and gcc, or use an industrial grade (read: expensive) PIC compiler.