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