LSM303DLHC: Magnetometer and Accelerometer data Monitor


This sample application periodically reads magnetometer and accelerometer data from the LSM303DLHC eCompass module’s sensors, and displays the sensor data on the console.


This sample uses the LSM303DLHC, ST MEMS system-in-package featuring a 3D digital linear acceleration sensor and a 3D digital magnetic sensor, controlled using the I2C interface.


For more information about the LSM303DLHC eCompass module, see

Building and Running

This project outputs sensor data to the console. It requires a LSM303DLHC system-in-package, which is present on the stm32f3_disco board

Using west:

west build -b stm32f3_disco samples/sensors/lsm303dlhc

Using CMake and ninja:

cmake -B build -GNinja -DBOARD=stm32f3_disco samples/sensors/lsm303dlhc
ninja -C build

Sample Output

Magnetometer data:
( x y z ) = ( 0.531818  -0.435454  -0.089090 )
Accelerometer data:
( x y z ) = ( -0.078127  -0.347666  1.105502 )
Magnetometer data:
( x y z ) = ( -0.003636  0.297272  -0.255454 )
Accelerometer data:
( x y z ) = ( 0.074221  -0.304696  0.972685 )

<repeats endlessly every 2 seconds>