Component guide · Intermediate · 3 min read
LSM303DLHC, LSM303DLH

GY-51), Pimoroni LSM303D, LSM303AGR, LSM303AH and NXP FXOS8700 E-Compass Sensors
By Geekobot Learning Team · Updated 29 Oct 2026
On this page
An e-compass combines a 3-axis accelerometer and a 3-axis magnetometer, so it can give a heading that stays correct when tilted. The LSM303DLHC and GY-51 are classic Arduino boards; the LSM303D and LSM303AGR are newer versions; and NXP's FXOS8700 evaluation board does the same job. Calibrate the magnetometer for hard-iron offsets before trusting the heading.
Key specs at a glance
- Sensors
- 3-axis accelerometer and 3-axis magnetometer
- Accel range
- ±2 to ±16 g
- Mag range
- ±1.3 to ±50 gauss depending on part
- Interface
- I2C (SPI on LSM303D, AGR, AH)
- Supply
- Modules 3.3 or 5 V; chips 1.7 to 3.6 V
- Use
- Tilt-compensated compass, orientation
Models in this guide
These are alternatives. Pick the one that fits your project; you only need one.
Option 1LSM303DLHC High-Precision 3-Axis Accelerometer & Magnetometer Module – I2C Interface₹239
Option 2LSM303DLH GY-51 3-Axis Magnetic Field & Acceleration Module – I2C Interface₹449
Option 3Pimoroni LSM303D 6DoF Motion Sensor Breakout – I2C Interface, 3.3V/5V Compatible₹1,086
Option 4STMicroelectronics LSM303AGRTR LGA-12 Accelerometer Magnetometer Module – SPI/I2C Interface₹939
Option 5STMicroelectronics LSM303AH 6-Axis eCompass Sensor, ±16g Accelerometer + ±50 Gauss Magnetometer, SPI/I²C, 16-bit, LGA-12₹559
Option 6NXP Semiconductors FXOS8700CQEVB 6-Axis Sensor Evaluation Board with Headers₹1,203
What the LSM303 and FXOS8700 accelerometer-magnetometer e-compasses is
The ST LSM303 (such as LSM303AGR and LSM303DLHC) combines a 3-axis accelerometer and 3-axis magnetometer, so it works as a tilt-compensated electronic compass.
The accelerometer tells the compass which way is down, so the magnetometer reading can be rotated back to level before computing heading.
Magnets, motors and steel nearby distort the field. Mount the sensor away from them and calibrate in place by rotating it in all directions.
LSM303 and FXOS8700 accelerometer-magnetometer e-compasses pinout
The pins on the LSM303 and FXOS8700 accelerometer-magnetometer e-compasses and what each one does.
| Pin | Function |
|---|---|
| VIN | Supply |
| GND | Ground |
| SCL / SDA | I2C |
| DRDY / INT | Data ready and interrupts |
- VIN
- Supply
- GND
- Ground
- SCL / SDA
- I2C
- DRDY / INT
- Data ready and interrupts
Wiring the LSM303 and FXOS8700 accelerometer-magnetometer e-compasses
Modules with a regulator run from 3.3 V or 5 V. Bare chips (LGA or QFN parts) are 1.8 to 3.6 V only: mount them on a breakout and use a level shifter with 5 V boards.
| LSM303 and FXOS8700 accelerometer-magnetometer e-compasses pin | Arduino Uno | ESP32 |
|---|---|---|
| VCC | 5V (or 3.3V) | 3V3 |
| GND | GND | GND |
| SDA | A4 | GPIO 21 |
| SCL | A5 | GPIO 22 |
- VCC
- Arduino Uno:
- 5V (or 3.3V)
- ESP32:
- 3V3
- GND
- Arduino Uno:
- GND
- ESP32:
- GND
- SDA
- Arduino Uno:
- A4
- ESP32:
- GPIO 21
- SCL
- Arduino Uno:
- A5
- ESP32:
- GPIO 22
Example code
Run the library's Calibrate example first and paste its min and max values. The LSM303AGR needs Adafruit's LSM303 Accel and LIS2MDL libraries instead.
#include <Wire.h>
#include <LSM303.h>
LSM303 compass;
void setup() {
Serial.begin(115200);
Wire.begin();
if (!compass.init()) { Serial.println("LSM303 not found"); while (1); }
compass.enableDefault();
compass.m_min = (LSM303::vector<int16_t>){-32767, -32767, -32767}; // replace with calibration
compass.m_max = (LSM303::vector<int16_t>){+32767, +32767, +32767};
}
void loop() {
compass.read();
Serial.print("Heading: "); Serial.println(compass.heading());
delay(200);
}Setup and tuning
VCC, GND, SDA and SCL; Adafruit breakouts accept 3.3 or 5 V.
use the Adafruit LSM303 libraries for accel and mag.
rotate the sensor in all directions to find hard-iron offsets, then subtract them.
use accelerometer pitch and roll to correct the heading.
keep away from magnets, motors and steel.
What you need
Shop this list8 of 9 found in our store · ₹5,003
LSM303DLHC High-Precision 3-Axis Accelerometer & Magnetometer Module – I2C Interface: the part this guide coversLSM303DLHC High-Precision 3-Axis Accelerometer & Magnetometer Module – I2C Interface₹239
LSM303DLH GY-51 3-Axis Magnetic Field & Acceleration Module – I2C Interface: another version this guide coversLSM303DLH GY-51 3-Axis Magnetic Field & Acceleration Module – I2C Interface₹449
Pimoroni LSM303D 6DoF Motion Sensor Breakout – I2C Interface, 3.3V/5V Compatible: another version this guide coversPimoroni LSM303D 6DoF Motion Sensor Breakout – I2C Interface, 3.3V/5V Compatible₹1,086
STMicroelectronics LSM303AGRTR LGA-12 Accelerometer Magnetometer Module – SPI/I2C Interface: another version this guide coversSTMicroelectronics LSM303AGRTR LGA-12 Accelerometer Magnetometer Module – SPI/I2C Interface₹939
STMicroelectronics LSM303AH 6-Axis eCompass Sensor, ±16g Accelerometer + ±50 Gauss Magnetometer, SPI/I²C, 16-bit, LGA-12: another version this guide coversSTMicroelectronics LSM303AH 6-Axis eCompass Sensor, ±16g Accelerometer + ±50 Gauss Magnetometer, SPI/I²C, 16-bit, LGA-12₹559
NXP Semiconductors FXOS8700CQEVB 6-Axis Sensor Evaluation Board with Headers: another version this guide coversNXP Semiconductors FXOS8700CQEVB 6-Axis Sensor Evaluation Board with Headers₹1,203- Uno R3 development board: the controller used in the wiring tableFind this part
ESP32-WROOM-32 38-pin development board: for Wi-Fi logging and dashboardsESP32-WROOM-32 38-Pin Development Board₹389
0.96 inch OLED display 128x64: to show readings without a computer0.96" OLED Display 128x64₹139Closest match, check the specs
Alternatives and when to choose them
Common mistakes to avoid
- Skipping calibration.
- Mounting near motors or magnets.
- Ignoring tilt.
- Forgetting declination.
Frequently asked questions
Why is my compass heading wrong?
The magnetometer needs hard-iron calibration and must be away from metal and motors.
What is tilt compensation?
Using the accelerometer to correct heading when the sensor is not level.
Does the LSM303 have a gyro?
No. Add a gyro for smooth fast motion tracking.
What is magnetic declination?
The angle between magnetic and true north; add it for true heading.
Can I use two LSM303 on one bus?
No. The addresses are fixed; use an I2C multiplexer.
Parts for this guide
Keep learning
Sources
Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

