Adafruit MLX90393 Library
Adafruit_MLX90393.h
1 /******************************************************************************
2  This is a library for the MLX90393 magnetometer.
3 
4  Designed specifically to work with the MLX90393 breakout from Adafruit:
5 
6  ----> https://www.adafruit.com/products/4022
7 
8  These sensors use I2C to communicate, 2 pins are required to interface.
9 
10  Adafruit invests time and resources providing this open source code, please
11  support Adafruit and open-source hardware by purchasing products from
12  Adafruit!
13 
14  Written by Kevin Townsend/ktown for Adafruit Industries.
15 
16  MIT license, all text above must be included in any redistribution
17  *****************************************************************************/
18 #ifndef ADAFRUIT_MLX90393_H
19 #define ADAFRUIT_MLX90393_H
20 
21 #include "Arduino.h"
22 #include <Adafruit_I2CDevice.h>
23 #include <Adafruit_SPIDevice.h>
24 #include <Adafruit_Sensor.h>
25 
26 #define MLX90393_DEFAULT_ADDR (0x0C) /* Can also be 0x18, depending on IC */
27 
28 #define MLX90393_AXIS_ALL (0x0E)
29 #define MLX90393_CONF1 (0x00)
30 #define MLX90393_CONF2 (0x01)
31 #define MLX90393_CONF3 (0x02)
32 #define MLX90393_CONF4 (0x03)
33 #define MLX90393_GAIN_SHIFT (4)
34 #define MLX90393_HALL_CONF (0x0C)
35 #define MLX90393_STATUS_OK (0x00)
36 #define MLX90393_STATUS_SMMODE (0x08)
37 #define MLX90393_STATUS_RESET (0x01)
38 #define MLX90393_STATUS_ERROR (0xFF)
39 #define MLX90393_STATUS_MASK (0xFC)
42 enum {
43  MLX90393_REG_SB = (0x10),
44  MLX90393_REG_SW = (0x20),
45  MLX90393_REG_SM = (0x30),
46  MLX90393_REG_RM = (0x40),
47  MLX90393_REG_RR = (0x50),
48  MLX90393_REG_WR = (0x60),
49  MLX90393_REG_EX = (0x80),
50  MLX90393_REG_HR = (0xD0),
51  MLX90393_REG_HS = (0x70),
52  MLX90393_REG_RT = (0xF0),
53  MLX90393_REG_NOP = (0x00),
54 };
55 
57 typedef enum mlx90393_gain {
58  MLX90393_GAIN_5X = (0x00),
59  MLX90393_GAIN_4X,
60  MLX90393_GAIN_3X,
61  MLX90393_GAIN_2_5X,
62  MLX90393_GAIN_2X,
63  MLX90393_GAIN_1_67X,
64  MLX90393_GAIN_1_33X,
65  MLX90393_GAIN_1X
66 } mlx90393_gain_t;
67 
69 typedef enum mlx90393_resolution {
70  MLX90393_RES_16,
71  MLX90393_RES_17,
72  MLX90393_RES_18,
73  MLX90393_RES_19,
74 } mlx90393_resolution_t;
75 
77 typedef enum mlx90393_axis {
78  MLX90393_X,
79  MLX90393_Y,
80  MLX90393_Z
81 } mlx90393_axis_t;
82 
84 typedef enum mlx90393_filter {
85  MLX90393_FILTER_0,
86  MLX90393_FILTER_1,
87  MLX90393_FILTER_2,
88  MLX90393_FILTER_3,
89  MLX90393_FILTER_4,
90  MLX90393_FILTER_5,
91  MLX90393_FILTER_6,
92  MLX90393_FILTER_7,
93 } mlx90393_filter_t;
94 
96 typedef enum mlx90393_oversampling {
97  MLX90393_OSR_0,
98  MLX90393_OSR_1,
99  MLX90393_OSR_2,
100  MLX90393_OSR_3,
101 } mlx90393_oversampling_t;
102 
105 const float mlx90393_lsb_lookup[2][8][4][2] = {
106 
107  /* HALLCONF = 0xC (default) */
108  {
109  /* GAIN_SEL = 0, 5x gain */
110  {{0.751, 1.210}, {1.502, 2.420}, {3.004, 4.840}, {6.009, 9.680}},
111  /* GAIN_SEL = 1, 4x gain */
112  {{0.601, 0.968}, {1.202, 1.936}, {2.403, 3.872}, {4.840, 7.744}},
113  /* GAIN_SEL = 2, 3x gain */
114  {{0.451, 0.726}, {0.901, 1.452}, {1.803, 2.904}, {3.605, 5.808}},
115  /* GAIN_SEL = 3, 2.5x gain */
116  {{0.376, 0.605}, {0.751, 1.210}, {1.502, 2.420}, {3.004, 4.840}},
117  /* GAIN_SEL = 4, 2x gain */
118  {{0.300, 0.484}, {0.601, 0.968}, {1.202, 1.936}, {2.403, 3.872}},
119  /* GAIN_SEL = 5, 1.667x gain */
120  {{0.250, 0.403}, {0.501, 0.807}, {1.001, 1.613}, {2.003, 3.227}},
121  /* GAIN_SEL = 6, 1.333x gain */
122  {{0.200, 0.323}, {0.401, 0.645}, {0.801, 1.291}, {1.602, 2.581}},
123  /* GAIN_SEL = 7, 1x gain */
124  {{0.150, 0.242}, {0.300, 0.484}, {0.601, 0.968}, {1.202, 1.936}},
125  },
126 
127  /* HALLCONF = 0x0 */
128  {
129  /* GAIN_SEL = 0, 5x gain */
130  {{0.787, 1.267}, {1.573, 2.534}, {3.146, 5.068}, {6.292, 10.137}},
131  /* GAIN_SEL = 1, 4x gain */
132  {{0.629, 1.014}, {1.258, 2.027}, {2.517, 4.055}, {5.034, 8.109}},
133  /* GAIN_SEL = 2, 3x gain */
134  {{0.472, 0.760}, {0.944, 1.521}, {1.888, 3.041}, {3.775, 6.082}},
135  /* GAIN_SEL = 3, 2.5x gain */
136  {{0.393, 0.634}, {0.787, 1.267}, {1.573, 2.534}, {3.146, 5.068}},
137  /* GAIN_SEL = 4, 2x gain */
138  {{0.315, 0.507}, {0.629, 1.014}, {1.258, 2.027}, {2.517, 4.055}},
139  /* GAIN_SEL = 5, 1.667x gain */
140  {{0.262, 0.422}, {0.524, 0.845}, {1.049, 1.689}, {2.097, 3.379}},
141  /* GAIN_SEL = 6, 1.333x gain */
142  {{0.210, 0.338}, {0.419, 0.676}, {0.839, 1.352}, {1.678, 2.703}},
143  /* GAIN_SEL = 7, 1x gain */
144  {{0.157, 0.253}, {0.315, 0.507}, {0.629, 1.014}, {1.258, 2.027}},
145  }};
146 
149 const float mlx90393_tconv[8][4] = {
150  /* DIG_FILT = 0 */
151  {1.27, 1.84, 3.00, 5.30},
152  /* DIG_FILT = 1 */
153  {1.46, 2.23, 3.76, 6.84},
154  /* DIG_FILT = 2 */
155  {1.84, 3.00, 5.30, 9.91},
156  /* DIG_FILT = 3 */
157  {2.61, 4.53, 8.37, 16.05},
158  /* DIG_FILT = 4 */
159  {4.15, 7.60, 14.52, 28.34},
160  /* DIG_FILT = 5 */
161  {7.22, 13.75, 26.80, 52.92},
162  /* DIG_FILT = 6 */
163  {13.36, 26.04, 51.38, 102.07},
164  /* DIG_FILT = 7 */
165  {25.65, 50.61, 100.53, 200.37},
166 };
167 
171 class Adafruit_MLX90393 : public Adafruit_Sensor {
172 public:
174  bool begin_I2C(uint8_t i2c_addr = MLX90393_DEFAULT_ADDR,
175  TwoWire *wire = &Wire);
176  bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI = &SPI);
177 
178  bool reset(void);
179  bool exitMode(void);
180 
181  bool readMeasurement(float *x, float *y, float *z);
182  bool startSingleMeasurement(void);
183 
184  bool setGain(enum mlx90393_gain gain);
185  enum mlx90393_gain getGain(void);
186 
187  bool setResolution(enum mlx90393_axis, enum mlx90393_resolution resolution);
188  enum mlx90393_resolution getResolution(enum mlx90393_axis);
189 
190  bool setFilter(enum mlx90393_filter filter);
191  enum mlx90393_filter getFilter(void);
192 
193  bool setOversampling(enum mlx90393_oversampling oversampling);
194  enum mlx90393_oversampling getOversampling(void);
195 
196  bool setTrigInt(bool state);
197  bool readData(float *x, float *y, float *z);
198 
199  bool getEvent(sensors_event_t *event);
200  void getSensor(sensor_t *sensor);
201 
202 private:
203  Adafruit_I2CDevice *i2c_dev = NULL;
204  Adafruit_SPIDevice *spi_dev = NULL;
205 
206  bool readRegister(uint8_t reg, uint16_t *data);
207  bool writeRegister(uint8_t reg, uint16_t data);
208  bool _init(void);
209  uint8_t transceive(uint8_t *txbuf, uint8_t txlen, uint8_t *rxbuf = NULL,
210  uint8_t rxlen = 0, uint8_t interdelay = 10);
211 
212  enum mlx90393_gain _gain;
213  enum mlx90393_resolution _res_x, _res_y, _res_z;
214  enum mlx90393_filter _dig_filt;
215  enum mlx90393_oversampling _osr;
216 
217  int32_t _sensorID = 90393;
218  int _cspin;
219 };
220 
221 #endif /* ADAFRUIT_MLX90393_H */
enum mlx90393_oversampling getOversampling(void)
Definition: Adafruit_MLX90393.cpp:265
bool setTrigInt(bool state)
Definition: Adafruit_MLX90393.cpp:276
bool readData(float *x, float *y, float *z)
Definition: Adafruit_MLX90393.cpp:362
bool setResolution(enum mlx90393_axis, enum mlx90393_resolution resolution)
Definition: Adafruit_MLX90393.cpp:174
bool reset(void)
Definition: Adafruit_MLX90393.cpp:124
enum mlx90393_filter getFilter(void)
Definition: Adafruit_MLX90393.cpp:241
enum mlx90393_gain getGain(void)
Definition: Adafruit_MLX90393.cpp:158
bool readMeasurement(float *x, float *y, float *z)
Definition: Adafruit_MLX90393.cpp:317
enum mlx90393_resolution getResolution(enum mlx90393_axis)
Definition: Adafruit_MLX90393.cpp:207
void getSensor(sensor_t *sensor)
Gets the sensor_t device data, Adafruit Unified Sensor format.
Definition: Adafruit_MLX90393.cpp:477
bool exitMode(void)
Definition: Adafruit_MLX90393.cpp:113
Definition: Adafruit_MLX90393.h:171
bool begin_I2C(uint8_t i2c_addr=MLX90393_DEFAULT_ADDR, TwoWire *wire=&Wire)
Sets up the hardware and initializes I2C.
Definition: Adafruit_MLX90393.cpp:33
bool getEvent(sensors_event_t *event)
Gets the most recent sensor event, Adafruit Unified Sensor format.
Definition: Adafruit_MLX90393.cpp:409
bool setOversampling(enum mlx90393_oversampling oversampling)
Definition: Adafruit_MLX90393.cpp:248
bool startSingleMeasurement(void)
Definition: Adafruit_MLX90393.cpp:297
bool setFilter(enum mlx90393_filter filter)
Definition: Adafruit_MLX90393.cpp:225
bool setGain(enum mlx90393_gain gain)
Definition: Adafruit_MLX90393.cpp:139
bool begin_SPI(uint8_t cs_pin, SPIClass *theSPI=&SPI)
Sets up the hardware and initializes hardware SPI.
Definition: Adafruit_MLX90393.cpp:56
Adafruit_MLX90393()
Definition: Adafruit_MLX90393.cpp:23