source: trunk/Arduino/GSM/libraries/LSM303/examples/Serial/Serial.ino@ 17994

Last change on this file since 17994 was 17969, checked in by dneise, 10 years ago
adding libraries folder, with LSM303 lib, this takes precedence over any locally installed LSM303 library, e.g. in HOME/arduino-1.0.6/libraries.
File size: 1.7 KB
Line 
1/*
2The sensor outputs provided by the library are the raw 16-bit values
3obtained by concatenating the 8-bit high and low accelerometer and
4magnetometer data registers. They can be converted to units of g and
5gauss using the conversion factors specified in the datasheet for your
6particular device and full scale setting (gain).
7
8Example: An LSM303D gives a magnetometer X axis reading of 1982 with
9its default full scale setting of +/- 4 gauss. The M_GN specification
10in the LSM303D datasheet (page 10) states a conversion factor of 0.160
11mgauss/LSB (least significant bit) at this FS setting, so the raw
12reading of -1982 corresponds to 1982 * 0.160 = 317.1 mgauss =
130.3171 gauss.
14
15In the LSM303DLHC, LSM303DLM, and LSM303DLH, the acceleration data
16registers actually contain a left-aligned 12-bit number, so the lowest
174 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
19in the datasheets.
20
21Example: An LSM303DLH gives an accelerometer Z axis reading of -16144
22with its default full scale setting of +/- 2 g. Dropping the lowest 4
23bits gives a 12-bit raw value of -1009. The LA_So specification in the
24LSM303DLH datasheet (page 11) states a conversion factor of 1 mg/digit
25at this FS setting, so the value of -1009 corresponds to -1009 * 1 =
261009 mg = 1.009 g.
27*/
28
29#include <Wire.h>
30#include <LSM303.h>
31
32LSM303 compass;
33
34char report[80];
35
36void setup()
37{
38 Serial.begin(9600);
39 Wire.begin();
40 compass.init();
41 compass.enableDefault();
42}
43
44void 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}
Note: See TracBrowser for help on using the repository browser.