Adafruit MPU6050 Sensor Library
Adafruit_MPU6050.h
Go to the documentation of this file.
1 
17 #ifndef _ADAFRUIT_MPU6050_H
18 #define _ADAFRUIT_MPU6050_H
19 
20 #include "Arduino.h"
21 #include <Adafruit_BusIO_Register.h>
22 #include <Adafruit_I2CDevice.h>
23 #include <Adafruit_Sensor.h>
24 #include <Wire.h>
25 
26 #define MPU6050_I2CADDR_DEFAULT \
27  0x68
28 #define MPU6050_DEVICE_ID 0x68
29 
30 #define MPU6050_SELF_TEST_X \
31  0x0D
32 #define MPU6050_SELF_TEST_Y \
33  0x0E
34 #define MPU6050_SELF_TEST_Z \
35  0x0F
36 #define MPU6050_SELF_TEST_A \
37  0x10
38 #define MPU6050_SMPLRT_DIV 0x19
39 #define MPU6050_CONFIG 0x1A
40 #define MPU6050_GYRO_CONFIG 0x1B
41 #define MPU6050_ACCEL_CONFIG \
42  0x1C
43 #define MPU6050_INT_PIN_CONFIG 0x37
44 #define MPU6050_INT_ENABLE 0x38
45 #define MPU6050_INT_STATUS 0x3A
46 #define MPU6050_WHO_AM_I 0x75
47 #define MPU6050_SIGNAL_PATH_RESET 0x68
48 #define MPU6050_USER_CTRL 0x6A
49 #define MPU6050_PWR_MGMT_1 0x6B
50 #define MPU6050_PWR_MGMT_2 0x6C
51 #define MPU6050_TEMP_H 0x41
52 #define MPU6050_TEMP_L 0x42
53 #define MPU6050_ACCEL_OUT 0x3B
54 #define MPU6050_MOT_THR 0x1F
55 #define MPU6050_MOT_DUR \
56  0x20
57 
58 
63 typedef enum fsync_out {
64  MPU6050_FSYNC_OUT_DISABLED,
65  MPU6050_FSYNC_OUT_TEMP,
66  MPU6050_FSYNC_OUT_GYROX,
67  MPU6050_FSYNC_OUT_GYROY,
68  MPU6050_FSYNC_OUT_GYROZ,
69  MPU6050_FSYNC_OUT_ACCELX,
70  MPU6050_FSYNC_OUT_ACCELY,
71  MPU6050_FSYNC_OUT_ACCEL_Z,
73 
79 typedef enum clock_select {
80  MPU6050_INTR_8MHz,
81  MPU6050_PLL_GYROX,
82  MPU6050_PLL_GYROY,
83  MPU6050_PLL_GYROZ,
84  MPU6050_PLL_EXT_32K,
85  MPU6050_PLL_EXT_19MHz,
86  MPU6050_STOP = 7,
88 
94 typedef enum {
100 
106 typedef enum {
112 
118 typedef enum {
127 
133 typedef enum {
134  MPU6050_HIGHPASS_DISABLE,
135  MPU6050_HIGHPASS_5_HZ,
136  MPU6050_HIGHPASS_2_5_HZ,
137  MPU6050_HIGHPASS_1_25_HZ,
138  MPU6050_HIGHPASS_0_63_HZ,
139  MPU6050_HIGHPASS_UNUSED,
140  MPU6050_HIGHPASS_HOLD,
142 
148 typedef enum {
154 
155 class Adafruit_MPU6050;
156 
158 class Adafruit_MPU6050_Temp : public Adafruit_Sensor {
159 public:
162  Adafruit_MPU6050_Temp(Adafruit_MPU6050 *parent) { _theMPU6050 = parent; }
163  bool getEvent(sensors_event_t *);
164  void getSensor(sensor_t *);
165 
166 private:
167  int _sensorID = 0x650;
168  Adafruit_MPU6050 *_theMPU6050 = NULL;
169 };
170 
172 class Adafruit_MPU6050_Accelerometer : public Adafruit_Sensor {
173 public:
178  _theMPU6050 = parent;
179  }
180  bool getEvent(sensors_event_t *);
181  void getSensor(sensor_t *);
182 
183 private:
184  int _sensorID = 0x651;
185  Adafruit_MPU6050 *_theMPU6050 = NULL;
186 };
187 
189 class Adafruit_MPU6050_Gyro : public Adafruit_Sensor {
190 public:
193  Adafruit_MPU6050_Gyro(Adafruit_MPU6050 *parent) { _theMPU6050 = parent; }
194  bool getEvent(sensors_event_t *);
195  void getSensor(sensor_t *);
196 
197 private:
198  int _sensorID = 0x652;
199  Adafruit_MPU6050 *_theMPU6050 = NULL;
200 };
201 
206 class Adafruit_MPU6050 final {
207 public:
209  ~Adafruit_MPU6050();
210 
211  bool begin(uint8_t i2c_addr = MPU6050_I2CADDR_DEFAULT, TwoWire *wire = &Wire,
212  int32_t sensorID = 0);
213 
214  // Adafruit_Sensor API/Interface
215  bool getEvent(sensors_event_t *accel, sensors_event_t *gyro,
216  sensors_event_t *temp);
217 
218  mpu6050_accel_range_t getAccelerometerRange(void);
219  void setAccelerometerRange(mpu6050_accel_range_t);
220 
221  mpu6050_gyro_range_t getGyroRange(void);
222  void setGyroRange(mpu6050_gyro_range_t);
223 
224  void setInterruptPinPolarity(bool active_low);
225  void setInterruptPinLatch(bool held);
226  void setFsyncSampleOutput(mpu6050_fsync_out_t fsync_output);
227 
228  mpu6050_highpass_t getHighPassFilter(void);
229  void setHighPassFilter(mpu6050_highpass_t bandwidth);
230 
231  void setMotionInterrupt(bool active);
232  void setMotionDetectionThreshold(uint8_t thr);
233  void setMotionDetectionDuration(uint8_t dur);
234  bool getMotionInterruptStatus(void);
235 
236  mpu6050_fsync_out_t getFsyncSampleOutput(void);
237  void setI2CBypass(bool bypass);
238 
239  void setClock(mpu6050_clock_select_t);
240  mpu6050_clock_select_t getClock(void);
241 
242  void setFilterBandwidth(mpu6050_bandwidth_t bandwidth);
243  mpu6050_bandwidth_t getFilterBandwidth(void);
244 
245  void setSampleRateDivisor(uint8_t);
246  uint8_t getSampleRateDivisor(void);
247 
248  bool enableSleep(bool enable);
249  bool enableCycle(bool enable);
250 
251  void setCycleRate(mpu6050_cycle_rate_t rate);
252  mpu6050_cycle_rate_t getCycleRate(void);
253 
254  bool setGyroStandby(bool xAxisStandby, bool yAxisStandby, bool zAxisStandby);
255  bool setAccelerometerStandby(bool xAxisStandby, bool yAxisStandby,
256  bool zAxisStandby);
257  bool setTemperatureStandby(bool enable);
258 
259  void reset(void);
260 
261  Adafruit_Sensor *getTemperatureSensor(void);
262  Adafruit_Sensor *getAccelerometerSensor(void);
263  Adafruit_Sensor *getGyroSensor(void);
264 
265 private:
266  void _getRawSensorData(void);
267  void _scaleSensorData(void);
268 
269 protected:
270  float temperature,
271  accX,
272  accY,
273  accZ,
274  gyroX,
275  gyroY,
276  gyroZ;
277 
278  Adafruit_I2CDevice *i2c_dev = NULL;
279 
280  Adafruit_MPU6050_Temp *temp_sensor = NULL;
282  NULL;
283  Adafruit_MPU6050_Gyro *gyro_sensor = NULL;
284 
285  uint16_t _sensorid_accel,
286  _sensorid_gyro,
288 
289  void _read(void);
290  virtual bool _init(int32_t sensor_id);
291 
292 private:
293  friend class Adafruit_MPU6050_Temp;
294  friend class Adafruit_MPU6050_Accelerometer;
296  friend class Adafruit_MPU6050_Gyro;
299 
301  int16_t rawAccX, rawAccY, rawAccZ, rawTemp, rawGyroX, rawGyroY, rawGyroZ;
302 
303  void fillTempEvent(sensors_event_t *temp, uint32_t timestamp);
304  void fillAccelEvent(sensors_event_t *accel, uint32_t timestamp);
305  void fillGyroEvent(sensors_event_t *gyro, uint32_t timestamp);
306 };
307 
308 #endif
Adafruit_MPU6050_Temp(Adafruit_MPU6050 *parent)
Create an Adafruit_Sensor compatible object for the temp sensor.
Definition: Adafruit_MPU6050.h:162
enum clock_select mpu6050_clock_select_t
Clock source options.
mpu6050_cycle_rate_t
Periodic measurement options.
Definition: Adafruit_MPU6050.h:148
+/- 16g
Definition: Adafruit_MPU6050.h:98
Definition: Adafruit_MPU6050.h:189
+/- 250 deg/s (default value)
Definition: Adafruit_MPU6050.h:107
fsync_out
FSYNC output values.
Definition: Adafruit_MPU6050.h:63
+/- 2000 deg/s
Definition: Adafruit_MPU6050.h:110
mpu6050_highpass_t
Accelerometer high pass filter options.
Definition: Adafruit_MPU6050.h:133
bool getEvent(sensors_event_t *)
Gets the temperature as a standard sensor event.
Definition: Adafruit_MPU6050.cpp:875
Class that stores state and functions for interacting with the MPU6050 I2C Digital Potentiometer...
Definition: Adafruit_MPU6050.h:206
5 Hz
Definition: Adafruit_MPU6050.h:125
20 Hz
Definition: Adafruit_MPU6050.h:151
uint16_t _sensorid_temp
ID number for temperature.
Definition: Adafruit_MPU6050.h:285
Docs imply this disables the filter.
Definition: Adafruit_MPU6050.h:119
184 Hz
Definition: Adafruit_MPU6050.h:120
enum fsync_out mpu6050_fsync_out_t
FSYNC output values.
Adafruit_MPU6050_Gyro(Adafruit_MPU6050 *parent)
Create an Adafruit_Sensor compatible object for the gyro sensor.
Definition: Adafruit_MPU6050.h:193
Adafruit_MPU6050_Accelerometer(Adafruit_MPU6050 *parent)
Create an Adafruit_Sensor compatible object for the accelerometer sensor.
Definition: Adafruit_MPU6050.h:177
Definition: Adafruit_MPU6050.h:172
40 Hz
Definition: Adafruit_MPU6050.h:152
+/- 2g (default value)
Definition: Adafruit_MPU6050.h:95
10 Hz
Definition: Adafruit_MPU6050.h:124
Definition: Adafruit_MPU6050.h:158
+/- 1000 deg/s
Definition: Adafruit_MPU6050.h:109
float temperature
Last reading&#39;s temperature (C)
Definition: Adafruit_MPU6050.h:270
mpu6050_bandwidth_t
Digital low pass filter bandthwidth options.
Definition: Adafruit_MPU6050.h:118
void getSensor(sensor_t *)
Gets the sensor_t data for the MPU6050&#39;s tenperature.
Definition: Adafruit_MPU6050.cpp:852
#define MPU6050_I2CADDR_DEFAULT
MPU6050 default i2c address w/ AD0 high.
Definition: Adafruit_MPU6050.h:26
mpu6050_gyro_range_t
Gyroscope range options.
Definition: Adafruit_MPU6050.h:106
44 Hz
Definition: Adafruit_MPU6050.h:122
mpu6050_accel_range_t
Accelerometer range options.
Definition: Adafruit_MPU6050.h:94
+/- 4g
Definition: Adafruit_MPU6050.h:96
94 Hz
Definition: Adafruit_MPU6050.h:121
clock_select
Clock source options.
Definition: Adafruit_MPU6050.h:79
5 Hz
Definition: Adafruit_MPU6050.h:150
1.25 Hz
Definition: Adafruit_MPU6050.h:149
+/- 8g
Definition: Adafruit_MPU6050.h:97
21 Hz
Definition: Adafruit_MPU6050.h:123
+/- 500 deg/s
Definition: Adafruit_MPU6050.h:108