The LSM303 3D Accelerometers/Magnetometer Models are a system-in-package featuring a 3D digital linear acceleration sensor and a 3D digital magnetic sensor. These best-in-class eCompass devices enable superior PDR or unique use cases in emerging applications, including drones and personal navigation systems.
All full-scales available are fully selectable by the user. The device includes an I2C serial bus interface that supports standard and fast mode 100kHz and 400kHz.
Features
|
Here is a picture of the module I bought
This is a schematic of a typical module such as the one above
Now lets look at a layout showing how to connect the module to our Arduino
Schematic and layout
Quite a straightforward connection being an I2C device, as this module has a 3v3 voltage regulator you can connect this to your Arduino's 5v.
Code
[codesyntax lang=”cpp”]
#include <Wire.h> #include <LSM303.h> LSM303 compass; char report[80]; void setup() { Serial.begin(9600); Wire.begin(); compass.init(); compass.enableDefault(); } void loop() { compass.read(); snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d", compass.a.x, compass.a.y, compass.a.z, compass.m.x, compass.m.y, compass.m.z); Serial.println(report); delay(500); }
[/codesyntax]
Testing
Open the Serial Monitor window and you should see something like this
A: 1408 -10144 13072 M: -54 149 -458
A: 2944 -8896 13152 M: -56 121 -469
A: -2704 -10432 -1216 M: 346 431 200
A: 896 -8992 20512 M: 172 456 -259
A: 21296 -14512 25712 M: -171 481 275
A: -14000 16512 -12064 M: 242 141 475
A: 7296 -10800 -7216 M: -409 362 220
A: 31648 1840 -5344 M: -551 205 52
A: -272 30544 -6688 M: 248 -440 335
A: -6992 -7504 -3472 M: -13 566 -91
A: -1632 -16992 20464 M: -53 584 49
Links
You can pick up one of these modules for about $5
AliExpress.com Product – Accelerometer + 3 Axis Magnetometer Module Sensor