Adafruit LSM303DLH Magnetometer Library
Adafruit_LSM303DLH_Mag.h
1 /***************************************************************************
2  This is a library for the LSM303DLH magnentometer/compass
3 
4  Designed specifically to work with the Adafruit LSM303DLHC Breakout
5 
6  These displays use I2C to communicate, 2 pins are required to interface.
7 
8  Adafruit invests time and resources providing this open source code,
9  please support Adafruit andopen-source hardware by purchasing products
10  from Adafruit!
11 
12  Written by Bryan Siepert for Adafruit Industries.
13  BSD license, all text above must be included in any redistribution
14  ***************************************************************************/
15 #ifndef LSM303DLH_MAG_H
16 #define LSM303DLH_MAG_H
17 
18 #if (ARDUINO >= 100)
19 #include "Arduino.h"
20 #else
21 #include "WProgram.h"
22 #endif
23 #include <Adafruit_BusIO_Register.h>
24 #include <Adafruit_I2CDevice.h>
25 #include <Adafruit_Sensor.h>
26 #include <Wire.h>
27 
28 /*=========================================================================
29  I2C ADDRESS/BITS
30  -----------------------------------------------------------------------*/
31 #define _ADDRESS_MAG 0x1E
32 /*=========================================================================*/
33 
34 typedef enum {
35  LSM303_REGISTER_MAG_CRA_REG_M = 0x00,
36  LSM303_REGISTER_MAG_CRB_REG_M = 0x01,
37  LSM303_REGISTER_MAG_MR_REG_M = 0x02,
38  LSM303_REGISTER_MAG_OUT_X_H_M = 0x03,
39  LSM303_REGISTER_MAG_OUT_X_L_M = 0x04,
40  LSM303_REGISTER_MAG_OUT_Z_H_M = 0x05,
41  LSM303_REGISTER_MAG_OUT_Z_L_M = 0x06,
42  LSM303_REGISTER_MAG_OUT_Y_H_M = 0x07,
43  LSM303_REGISTER_MAG_OUT_Y_L_M = 0x08,
44  LSM303_REGISTER_MAG_SR_REG_Mg = 0x09,
45  LSM303_REGISTER_MAG_IRA_REG_M = 0x0A,
46  LSM303_REGISTER_MAG_IRB_REG_M = 0x0B,
47  LSM303_REGISTER_MAG_IRC_REG_M = 0x0C,
48  LSM303_REGISTER_MAG_TEMP_OUT_H_M = 0x31,
49  LSM303_REGISTER_MAG_TEMP_OUT_L_M = 0x32
50 } lsm303MagRegisters_t;
51 /*=========================================================================*/
52 
53 /*=========================================================================
54  MAGNETOMETER GAIN SETTINGS
55  -----------------------------------------------------------------------*/
56 typedef enum {
57  LSM303_MAGGAIN_1_3, // +/- 1.3
58  LSM303_MAGGAIN_1_9, // +/- 1.9
59  LSM303_MAGGAIN_2_5, // +/- 2.5
60  LSM303_MAGGAIN_4_0, // +/- 4.0
61  LSM303_MAGGAIN_4_7, // +/- 4.7
62  LSM303_MAGGAIN_5_6, // +/- 5.6
63  LSM303_MAGGAIN_8_1 // +/- 8.1
64 } lsm303MagGain;
65 /*=========================================================================*/
66 
67 /*=========================================================================
68  MAGNETOMETER UPDATE RATE SETTINGS
69  -----------------------------------------------------------------------*/
70 typedef enum {
71  LSM303_MAGRATE_0_7, // 0.75 Hz
72  LSM303_MAGRATE_1_5, // 1.5 Hz
73  LSM303_MAGRATE_3_0, // 3.0 Hz
74  LSM303_MAGRATE_7_5, // 7.5 Hz
75  LSM303_MAGRATE_15, // 15 Hz
76  LSM303_MAGRATE_30, // 30 Hz
77  LSM303_MAGRATE_75, // 75 Hz
78  LSM303_MAGRATE_220 // 220 Hz
79 } lsm303MagRate;
80 /*=========================================================================*/
81 
82 /**************************************************************************/
86 /**************************************************************************/
87 typedef struct lsm303MagData_s {
88  int16_t x;
89  int16_t y;
90  int16_t z;
92 /*=========================================================================*/
93 
94 /*=========================================================================
95  CHIP ID
96  -----------------------------------------------------------------------*/
97 #define LSM303_ID (0b11010100)
98 /*=========================================================================*/
99 
103 class Adafruit_LSM303DLH_Mag_Unified : public Adafruit_Sensor {
104 public:
105  Adafruit_LSM303DLH_Mag_Unified(int32_t sensorID = -1);
106 
107  bool begin(uint8_t i2c_addr = _ADDRESS_MAG, TwoWire *wire = &Wire);
108 
109  void enableAutoRange(bool enable);
110  void setMagGain(lsm303MagGain gain);
111  void setMagRate(lsm303MagRate rate);
112  bool getEvent(sensors_event_t *);
113  void getSensor(sensor_t *);
114 
116  lsm303MagGain magGain;
118 
119 private:
120  int32_t _sensorID;
121 
122  void write8(byte address, byte reg, byte value);
123  byte read8(byte address, byte reg);
124  void read(void);
125  Adafruit_I2CDevice *i2c_dev;
126 };
127 
128 #endif
lsm303MagGain magGain
The current magnetometer gain.
Definition: Adafruit_LSM303DLH_Mag.h:116
int16_t y
y-axis data
Definition: Adafruit_LSM303DLH_Mag.h:89
int16_t z
z-axis data
Definition: Adafruit_LSM303DLH_Mag.h:90
lsm303MagData raw
Last read magnetometer data will be available here.
Definition: Adafruit_LSM303DLH_Mag.h:115
int16_t x
x-axis data
Definition: Adafruit_LSM303DLH_Mag.h:88
INTERNAL ACCELERATION DATA TYPE.
Definition: Adafruit_LSM303DLH_Mag.h:87
bool autoRangeEnabled
True if auto ranging is enabled.
Definition: Adafruit_LSM303DLH_Mag.h:117
Unified sensor driver for the magnetometer.
Definition: Adafruit_LSM303DLH_Mag.h:103