| 1 | /*
|
|---|
| 2 | The sensor outputs provided by the library are the raw 16-bit values
|
|---|
| 3 | obtained by concatenating the 8-bit high and low accelerometer and
|
|---|
| 4 | magnetometer data registers. They can be converted to units of g and
|
|---|
| 5 | gauss using the conversion factors specified in the datasheet for your
|
|---|
| 6 | particular device and full scale setting (gain).
|
|---|
| 7 |
|
|---|
| 8 | Example: An LSM303D gives a magnetometer X axis reading of 1982 with
|
|---|
| 9 | its default full scale setting of +/- 4 gauss. The M_GN specification
|
|---|
| 10 | in the LSM303D datasheet (page 10) states a conversion factor of 0.160
|
|---|
| 11 | mgauss/LSB (least significant bit) at this FS setting, so the raw
|
|---|
| 12 | reading of -1982 corresponds to 1982 * 0.160 = 317.1 mgauss =
|
|---|
| 13 | 0.3171 gauss.
|
|---|
| 14 |
|
|---|
| 15 | In the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data
|
|---|
| 16 | registers actually contain a left-aligned 12-bit number, so the lowest
|
|---|
| 17 | 4 bits are always 0, and the values should be shifted right by 4 bits
|
|---|
| 18 | (divided by 16) to be consistent with the conversion factors specified
|
|---|
| 19 | in the datasheets.
|
|---|
| 20 |
|
|---|
| 21 | Example: An LSM303DLH gives an accelerometer Z axis reading of -16144
|
|---|
| 22 | with its default full scale setting of +/- 2 g. Dropping the lowest 4
|
|---|
| 23 | bits gives a 12-bit raw value of -1009. The LA_So specification in the
|
|---|
| 24 | LSM303DLH datasheet (page 11) states a conversion factor of 1 mg/digit
|
|---|
| 25 | at this FS setting, so the value of -1009 corresponds to -1009 * 1 =
|
|---|
| 26 | 1009 mg = 1.009 g.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | #include <Wire.h>
|
|---|
| 30 | #include <LSM303.h>
|
|---|
| 31 |
|
|---|
| 32 | LSM303 compass;
|
|---|
| 33 |
|
|---|
| 34 | char report[80];
|
|---|
| 35 |
|
|---|
| 36 | void setup()
|
|---|
| 37 | {
|
|---|
| 38 | Serial.begin(9600);
|
|---|
| 39 | Wire.begin();
|
|---|
| 40 | compass.init();
|
|---|
| 41 | compass.enableDefault();
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | void loop()
|
|---|
| 45 | {
|
|---|
| 46 | compass.read();
|
|---|
| 47 |
|
|---|
| 48 | snprintf(report, sizeof(report), "A: %6d %6d %6d M: %6d %6d %6d",
|
|---|
| 49 | compass.a.x, compass.a.y, compass.a.z,
|
|---|
| 50 | compass.m.x, compass.m.y, compass.m.z);
|
|---|
| 51 | Serial.println(report);
|
|---|
| 52 |
|
|---|
| 53 | delay(100);
|
|---|
| 54 | } |
|---|