### Calibration setup example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Example setup that initializes the MPU6050 and runs accelerometer and gyro calibration with 6 loops, then prints the active offsets. ```cpp void setup() { Wire.begin(); mpu.initialize(); mpu.CalibrateAccel(6); // 6 loops, ~600-700 readings mpu.CalibrateGyro(6); mpu.PrintActiveOffsets(); } ``` -------------------------------- ### MPU6050 usage example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Shows how to create an MPU6050 object and initialize it in setup(). The commented lines show alternative constructors for AD0 high and custom Wire objects. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; // address 0x68, global Wire // MPU6050 mpu(0x69); // AD0 high // MPU6050 mpu(0x68, &Wire1); // custom Wire object void setup() { Wire.begin(); mpu.initialize(); } ``` -------------------------------- ### DMP initialization and interrupt example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Example sketch that initializes the MPU6050, calls dmpInitialize(), enables the DMP on success, and attaches an interrupt. On failure, prints the error code to Serial. ```cpp MPU6050 mpu; void setup() { Wire.begin(); mpu.initialize(); uint8_t devStatus = mpu.dmpInitialize(); if (devStatus == 0) { mpu.setDMPEnabled(true); attachInterrupt(digitalPinToInterrupt(2), DMPDataReady, RISING); } else { Serial.print("DMP init failed, code "); Serial.println(devStatus); } } ``` -------------------------------- ### Quaternion usage example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/helper-3dmath.md Shows how to obtain a quaternion from the DMP FIFO, normalize it, and verify its magnitude is approximately 1.0f. The example uses mpu.dmpGetQuaternion to fill the quaternion from the FIFO buffer. ```cpp Quaternion q; // identity: w=1, x=y=z=0 mpu.dmpGetQuaternion(&q, fifoBuffer); Quaternion q2 = q.getNormalized(); float m = q2.getMagnitude(); // ≈ 1.0f ``` -------------------------------- ### Usage example for dmpInitialize() Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-9axis-motionapps41.md Shows how to initialize the MPU6050 and enable DMP. Prints an error code if initialization fails. ```cpp MPU6050 mpu; void setup() { Wire.begin(); mpu.initialize(); uint8_t devStatus = mpu.dmpInitialize(); if (devStatus == 0) { mpu.setDMPEnabled(true); } else { Serial.print("DMP init failed, code "); Serial.println(devStatus); } } ``` -------------------------------- ### Rotate accelerometer into world frame and get yaw/pitch/roll Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/helper-3dmath.md Demonstrates using VectorFloat and Quaternion with DMP methods: get raw acceleration, get quaternion, rotate acceleration into world frame, and compute yaw/pitch/roll in degrees. The example prints yaw in degrees. ```cpp VectorFloat gravity; Quaternion q; mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); // world-frame gravity vector float ypr[3]; mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); Serial.print(ypr[0] * 180 / PI); // yaw in degrees ``` -------------------------------- ### MPU6050 DMP gravity-free linear acceleration usage example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Example of using dmpGetCurrentFIFOPacket, dmpGetQuaternion, dmpGetAccel, dmpGetGravity, and dmpGetLinearAccel to obtain gravity-free acceleration. Requires a FIFO packet and the DMP running. ```cpp VectorInt16 aa; VectorInt16 aaReal; VectorFloat gravity; Quaternion q; if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetAccel(&aa, fifoBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); // gravity-free accel } ``` -------------------------------- ### MPU6050 getMotion6 Usage Example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Example of reading accelerometer and gyroscope data using getMotion6 in the loop function. It prints the raw values to the serial monitor with a 50ms delay between readings. ```cpp int16_t ax, ay, az, gx, gy, gz; void loop() { mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); Serial.print("a/g:\t"); Serial.print(ax); Serial.print("\t"); Serial.print(ay); Serial.print("\t"); Serial.print(az); Serial.print("\t"); Serial.print(gx); Serial.print("\t"); Serial.print(gy); Serial.print("\t"); Serial.println(gz); delay(50); } ``` -------------------------------- ### Usage example for MPU6050_6Axis_MotionApps612 Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps612.md Shows how to include the header, create an MPU6050 object, initialize the DMP, and enable it. If initialization fails, prints an error code. ```cpp #include "MPU6050_6Axis_MotionApps612.h" MPU6050 mpu; void setup() { Wire.begin(); mpu.initialize(); uint8_t devStatus = mpu.dmpInitialize(); // default rate divisor 0x01 if (devStatus == 0) { mpu.setDMPEnabled(true); } else { Serial.print("DMP init failed, code "); Serial.println(devStatus); } } ``` -------------------------------- ### Example usage of FIFO packet decoding Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Demonstrates retrieving a packet and decoding quaternion and accelerometer data into local variables. ```cpp Quaternion q; VectorInt16 aa; uint8_t fifoBuffer[42]; if (mpu.dmpGetCurrentFIFOPacket(fifoBuffer)) { mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetAccel(&aa, fifoBuffer); } ``` -------------------------------- ### dmpInitialize() consumer pattern from MPU6050_DMP6.ino Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/errors.md Checks the return code of dmpInitialize() and enables DMP on success, or prints an error and halts on failure. The code uses the global devStatus variable and the mpu object from the example. On failure, it prints the code and enters an infinite loop. ```cpp uint8_t devStatus = mpu.dmpInitialize(); if (devStatus == 0) { mpu.setDMPEnabled(true); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), DMPDataReady, RISING); packetSize = mpu.dmpGetFIFOPacketSize(); } else { Serial.print("DMP Initialization failed (code "); Serial.print(devStatus); Serial.println(")"); while (1); // halt } ``` -------------------------------- ### GetCurrentFIFOPacket Usage Example Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Example of using GetCurrentFIFOPacket in a loop to read a 42-byte FIFO packet. The timeout is in microseconds, and the function returns 1 on success, 2 after overflow recovery, or 0 if no data is available within the timeout. ```cpp uint8_t fifoBuffer[42]; void loop() { // timeout is in microseconds here if (mpu.GetCurrentFIFOPacket(fifoBuffer, 42)) { // process one 42-byte packet } else { Serial.println("No FIFO packet available yet"); } } ``` -------------------------------- ### FIFO_EN (0x23) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set FIFO enable flags for temperature, gyroscope, accelerometer, and slave data. ```APIDOC ## FIFO_EN (0x23) ### Description Get and set FIFO enable flags for temperature, gyroscope axes, accelerometer, and auxiliary slave data. ### Methods - `bool getTempFIFOEnabled()` - Returns whether temperature FIFO is enabled. - `void setTempFIFOEnabled(bool enabled)` - Enables/disables temperature FIFO. - `bool getXGyroFIFOEnabled()` - Returns whether X-axis gyroscope FIFO is enabled. - `void setXGyroFIFOEnabled(bool enabled)` - Enables/disables X-axis gyroscope FIFO. - `bool getYGyroFIFOEnabled()` - Returns whether Y-axis gyroscope FIFO is enabled. - `void setYGyroFIFOEnabled(bool enabled)` - Enables/disables Y-axis gyroscope FIFO. - `bool getZGyroFIFOEnabled()` - Returns whether Z-axis gyroscope FIFO is enabled. - `void setZGyroFIFOEnabled(bool enabled)` - Enables/disables Z-axis gyroscope FIFO. - `bool getAccelFIFOEnabled()` - Returns whether accelerometer FIFO is enabled. - `void setAccelFIFOEnabled(bool enabled)` - Enables/disables accelerometer FIFO. - `bool getSlave2FIFOEnabled()` - Returns whether slave 2 FIFO is enabled. - `void setSlave2FIFOEnabled(bool enabled)` - Enables/disables slave 2 FIFO. - `bool getSlave1FIFOEnabled()` - Returns whether slave 1 FIFO is enabled. - `void setSlave1FIFOEnabled(bool enabled)` - Enables/disables slave 1 FIFO. - `bool getSlave0FIFOEnabled()` - Returns whether slave 0 FIFO is enabled. - `void setSlave0FIFOEnabled(bool enabled)` - Enables/disables slave 0 FIFO. ### Parameters #### Setter methods - **enabled** (bool) - Required - Whether to enable the corresponding FIFO. ### Returns Each getter returns a boolean indicating whether the corresponding FIFO is enabled. ``` -------------------------------- ### I2C_MST_CTRL (0x24) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set I2C master control settings. ```APIDOC ## I2C_MST_CTRL (0x24) ### Description Get and set I2C master control settings including multi-master, wait-for-external-sensor, slave 3 FIFO, read/write transition, and master clock speed. ### Methods - `bool getMultiMasterEnabled()` - Returns whether multi-master mode is enabled. - `void setMultiMasterEnabled(bool enabled)` - Enables/disables multi-master mode. - `bool getWaitForExternalSensorEnabled()` - Returns whether wait-for-external-sensor is enabled. - `void setWaitForExternalSensorEnabled(bool enabled)` - Enables/disables wait-for-external-sensor. - `bool getSlave3FIFOEnabled()` - Returns whether slave 3 FIFO is enabled. - `void setSlave3FIFOEnabled(bool enabled)` - Enables/disables slave 3 FIFO. - `bool getSlaveReadWriteTransitionEnabled()` - Returns whether slave read/write transition is enabled. - `void setSlaveReadWriteTransitionEnabled(bool enabled)` - Enables/disables slave read/write transition. - `uint8_t getMasterClockSpeed()` - Returns the master clock speed. - `void setMasterClockSpeed(uint8_t speed)` - Sets the master clock speed. ### Parameters #### Setter methods - **enabled** (bool) - Required - Whether to enable the corresponding setting. - **speed** (uint8_t) - Required - The master clock speed value. ### Returns - `getMultiMasterEnabled`, `getWaitForExternalSensorEnabled`, `getSlave3FIFOEnabled`, `getSlaveReadWriteTransitionEnabled` return boolean. - `getMasterClockSpeed` returns the master clock speed (uint8_t). ``` -------------------------------- ### SMPLRT_DIV / CONFIG (0x19, 0x1A) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set sample rate, external frame sync, and DLPF mode registers. ```APIDOC ## SMPLRT_DIV / CONFIG (0x19, 0x1A) ### Description Get and set the sample rate divider, external frame sync, and digital low-pass filter (DLPF) mode. ### Methods - `uint8_t getRate()` - Returns the current sample rate divider. - `void setRate(uint8_t rate)` - Sets the sample rate divider. - `uint8_t getExternalFrameSync()` - Returns the external frame sync setting. - `void setExternalFrameSync(uint8_t sync)` - Sets the external frame sync. - `uint8_t getDLPFMode()` - Returns the current DLPF mode. - `void setDLPFMode(uint8_t bandwidth)` - Sets the DLPF bandwidth. ### Parameters #### setRate - **rate** (uint8_t) - Required - The sample rate divider value. #### setExternalFrameSync - **sync** (uint8_t) - Required - The external frame sync setting. #### setDLPFMode - **bandwidth** (uint8_t) - Required - The DLPF bandwidth constant (e.g., `MPU6050_DLPF_BW_256/188/98/42/20/10/5`). ### Notes Sample Rate = Gyroscope Output Rate / (1 + SMPLRT_DIV) — 8 kHz with DLPF off, 1 kHz with DLPF on. ### Returns - `getRate` returns the sample rate divider (uint8_t). - `getExternalFrameSync` returns the external frame sync setting (uint8_t). - `getDLPFMode` returns the DLPF mode (uint8_t). ``` -------------------------------- ### Reading FIFO packet with quaternion and magnetometer Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-9axis-motionapps41.md Example usage that checks for an available FIFO packet, reads it into a buffer, and decodes quaternion and magnetometer data. Uses GetCurrentFIFOPacket with the FIFO packet size for overflow-proof reads. ```cpp Quaternion q; int16_t mag[3]; uint8_t fifoBuffer[48]; if (mpu.dmpPacketAvailable()) { mpu.GetCurrentFIFOPacket(fifoBuffer, mpu.dmpGetFIFOPacketSize()); mpu.dmpGetQuaternion(&q, fifoBuffer); mpu.dmpGetMag(mag, fifoBuffer); } ``` -------------------------------- ### Free-fall / motion / zero-motion detection (0x1D..0x22) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set thresholds and durations for free-fall, motion, and zero-motion detection. ```APIDOC ## Free-fall / motion / zero-motion detection (0x1D..0x22) ### Description Get and set the detection thresholds and durations for free-fall, motion, and zero-motion detection. ### Methods - `uint8_t getFreefallDetectionThreshold()` - Returns the free-fall detection threshold. - `void setFreefallDetectionThreshold(uint8_t threshold)` - Sets the free-fall detection threshold. - `uint8_t getFreefallDetectionDuration()` - Returns the free-fall detection duration. - `void setFreefallDetectionDuration(uint8_t duration)` - Sets the free-fall detection duration. - `uint8_t getMotionDetectionThreshold()` - Returns the motion detection threshold. - `void setMotionDetectionThreshold(uint8_t threshold)` - Sets the motion detection threshold. - `uint8_t getMotionDetectionDuration()` - Returns the motion detection duration. - `void setMotionDetectionDuration(uint8_t duration)` - Sets the motion detection duration. - `uint8_t getZeroMotionDetectionThreshold()` - Returns the zero-motion detection threshold. - `void setZeroMotionDetectionThreshold(uint8_t threshold)` - Sets the zero-motion detection threshold. - `uint8_t getZeroMotionDetectionDuration()` - Returns the zero-motion detection duration. - `void setZeroMotionDetectionDuration(uint8_t duration)` - Sets the zero-motion detection duration. ### Parameters #### Setter methods - **threshold** (uint8_t) - Required - The detection threshold value. - **duration** (uint8_t) - Required - The detection duration value. ### Returns Each getter returns the corresponding threshold or duration value (uint8_t). ``` -------------------------------- ### dmpInitialize() Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Documentation for the dmpInitialize() method of the MPU6050_6Axis_MotionApps20 class. ```APIDOC ## `dmpInitialize()` ### Description Performs the full DMP bring-up sequence: resets the device, loads the 1929-byte firmware image into DMP memory banks, patches the FIFO-rate divisor, and prepares the DMP for use. Returns 0 on success or 1 if the firmware write/verify fails. ### Method `uint8_t dmpInitialize()` ### Parameters None. ### Returns | Code | Meaning | |------|---------| | `0` | Success: firmware loaded, verified, DMP prepared | | `1` | `writeProgMemoryBlock(dmpMemory, 1929)` failed (firmware image write/verify error) | ### Example ```cpp MPU6050 mpu; void setup() { Wire.begin(); mpu.initialize(); uint8_t devStatus = mpu.dmpInitialize(); if (devStatus == 0) { mpu.setDMPEnabled(true); attachInterrupt(digitalPinToInterrupt(2), DMPDataReady, RISING); } else { Serial.print("DMP init failed, code "); Serial.println(devStatus); } } ``` ``` -------------------------------- ### GYRO_CONFIG (0x1B) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set the full-scale gyroscope range. ```APIDOC ## GYRO_CONFIG (0x1B) ### Description Get and set the full-scale gyroscope range. The setter also recomputes `gyroscopeResolution`. ### Methods - `uint8_t getFullScaleGyroRange()` - Returns the current full-scale gyro range. - `void setFullScaleGyroRange(uint8_t range)` - Sets the full-scale gyro range. ### Parameters #### setFullScaleGyroRange - **range** (uint8_t) - Required - The gyro full-scale range constant (e.g., `MPU6050_GYRO_FS_250/500/1000/2000`). ### Returns - `getFullScaleGyroRange` returns the current full-scale gyro range (uint8_t). ``` -------------------------------- ### Constructor for MPU6050_6Axis_MotionApps612 Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps612.md Constructor that initializes the base class with the given address and wire object. Same contract as MPU6050_Base constructor. ```cpp MPU6050_6Axis_MotionApps612(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0) : MPU6050_Base(address, wireObj) { } ``` -------------------------------- ### MPU6050_6Axis_MotionApps612 Class Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps612.md Documentation for the MPU6050_6Axis_MotionApps612 class, including its constructor and the dmpInitialize method. ```APIDOC ## `MPU6050_6Axis_MotionApps612` (DMP firmware v6.12) ### Description 6-axis DMP wrapper for InvenSense MotionApps firmware v6.12, with runtime auto-calibration and a larger flash footprint. Inherits `MPU6050_Base`. ### Constructor ```cpp MPU6050_6Axis_MotionApps612(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); ``` #### Parameters - **address** (uint8_t) - Optional - I2C address of the device. Default: `MPU6050_DEFAULT_ADDRESS`. - **wireObj** (void*) - Optional - Pointer to the Wire object. Default: `0`. ### Compile-time constant ```cpp #ifndef MPU6050_DMP_FIFO_RATE_DIVISOR #define MPU6050_DMP_FIFO_RATE_DIVISOR 0x01 #endif ``` `MPU6050_DMP_FIFO_RATE_DIVISOR` (default `0x01`) is pre-configured into the firmware image; it is the default `rateDivisor` argument of `dmpInitialize`. ### `dmpInitialize(rateDivisor, mpuAddr)` ```cpp uint8_t dmpInitialize(uint8_t rateDivisor = MPU6050_DMP_FIFO_RATE_DIVISOR, uint8_t mpuAddr = 0x68); ``` #### Parameters - **rateDivisor** (uint8_t) - Optional - `SMPLRT_DIV` value written to register 0x19. Default: `MPU6050_DMP_FIFO_RATE_DIVISOR` (`0x01`). - **mpuAddr** (uint8_t) - Optional - Unused by the implementation; the driver always uses the constructor's `devAddr`. Default: `0x68`. #### Returns - **uint8_t** - Status code: `0` on success, `1` if `writeProgMemoryBlock` (firmware image write/verify) failed. #### Usage ```cpp #include "MPU6050_6Axis_MotionApps612.h" MPU6050 mpu; void setup() { Wire.begin(); mpu.initialize(); uint8_t devStatus = mpu.dmpInitialize(); // default rate divisor 0x01 if (devStatus == 0) { mpu.setDMPEnabled(true); } else { Serial.print("DMP init failed, code "); Serial.println(devStatus); } } ``` ``` -------------------------------- ### initialize() Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Initializes the MPU6050 with default or specified full-scale ranges. The first overload sets clock source, gyro range, accel range, and disables sleep. The second overload accepts ACCEL_FS and GYRO_FS enum values. ```APIDOC ## initialize() ### Description Initializes the MPU6050. The first overload sets clock source to MPU6050_CLOCK_PLL_XGYRO, gyro full-scale to MPU6050_GYRO_FS_250, accel full-scale to MPU6050_ACCEL_FS_2, and disables sleep. The second overload uses the provided ACCEL_FS and GYRO_FS enum values for full-scale ranges. ### Method void initialize(); void initialize(ACCEL_FS accelRange, GYRO_FS gyroRange); ### Parameters #### Parameters - **accelRange** (ACCEL_FS) - Optional - Acceleration full-scale range. Valid values: A2G, A4G, A8G, A16G. Invalid values print an error and fall back to maximum range. - **gyroRange** (GYRO_FS) - Optional - Gyroscope full-scale range. Valid values: G250DPS, G500DPS, G1000DPS, G2000DPS. Invalid values print an error and fall back to maximum range. ### Request Example ```cpp mpu.initialize(); // or mpu.initialize(MPU6050::A4G, MPU6050::G500DPS); ``` ### Response #### Success Response No return value. ``` -------------------------------- ### AUX_VDDIO (0x01 YG_OFFS_TC) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set the AUX_VDDIO level register. ```APIDOC ## AUX_VDDIO (0x01 YG_OFFS_TC) ### Description Get and set the AUX_VDDIO level register. ### Methods - `uint8_t getAuxVDDIOLevel()` - Returns the current AUX_VDDIO level. - `void setAuxVDDIOLevel(uint8_t level)` - Sets the AUX_VDDIO level. ### Parameters #### setAuxVDDIOLevel - **level** (uint8_t) - Required - The AUX_VDDIO level to set. ### Returns - `getAuxVDDIOLevel` returns the current AUX_VDDIO level (uint8_t). ``` -------------------------------- ### writeBytes Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Writes multiple bytes to a device starting at a register address. ```APIDOC ## writeBytes ### Description Writes multiple bytes to a device starting at a register address. ### Method static bool writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, void *wireObj=0) ### Parameters - **devAddr** (uint8_t) - Required - I2C device address - **regAddr** (uint8_t) - Required - Starting register address - **length** (uint8_t) - Required - Number of bytes to write - **data** (uint8_t*) - Required - Pointer to the data to write - **wireObj** (void*) - Optional - Pointer to a custom Wire object (default: 0) ### Returns - **bool** - true on success, false on failure ``` -------------------------------- ### readBytes Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Reads multiple bytes from a device starting at a register address. ```APIDOC ## readBytes ### Description Reads multiple bytes from a device starting at a register address. ### Method static int8_t readBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout=I2Cdev::readTimeout, void *wireObj=0) ### Parameters - **devAddr** (uint8_t) - Required - I2C device address - **regAddr** (uint8_t) - Required - Starting register address - **length** (uint8_t) - Required - Number of bytes to read - **data** (uint8_t*) - Required - Pointer to store the read bytes - **timeout** (uint16_t) - Optional - Timeout in microseconds (default: I2Cdev::readTimeout) - **wireObj** (void*) - Optional - Pointer to a custom Wire object (default: 0) ### Returns - **int8_t** - Status: 0 on success, negative on error ``` -------------------------------- ### Constructor for MPU6050_6Axis_MotionApps20 Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Constructor that initializes the base class with the given I2C address and wire object. Same contract as MPU6050_Base. ```cpp MPU6050_6Axis_MotionApps20(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0) : MPU6050_Base(address, wireObj) { } ``` -------------------------------- ### dmpInitialize() declaration Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-9axis-motionapps41.md Declares the dmpInitialize() method that initializes the DMP and magnetometer. Returns 0 on success, 1 on firmware write failure, 2 on configuration write failure. ```cpp uint8_t dmpInitialize(); // src/MPU6050_9Axis_MotionApps41.cpp:352 ``` -------------------------------- ### writeWords Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Writes multiple 16-bit words to a device starting at a register address. ```APIDOC ## writeWords ### Description Writes multiple 16-bit words to a device starting at a register address. ### Method static bool writeWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, void *wireObj=0) ### Parameters - **devAddr** (uint8_t) - Required - I2C device address - **regAddr** (uint8_t) - Required - Starting register address - **length** (uint8_t) - Required - Number of words to write - **data** (uint16_t*) - Required - Pointer to the data to write - **wireObj** (void*) - Optional - Pointer to a custom Wire object (default: 0) ### Returns - **bool** - true on success, false on failure ``` -------------------------------- ### testConnection() usage Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Verifies wiring after initialize(). Prints an error and halts if the connection fails. ```cpp void setup() { Wire.begin(); mpu.initialize(); if (!mpu.testConnection()) { Serial.println("MPU6050 connection failed"); while (true); } } ``` -------------------------------- ### readWords Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Reads multiple 16-bit words from a device starting at a register address. ```APIDOC ## readWords ### Description Reads multiple 16-bit words from a device starting at a register address. ### Method static int8_t readWords(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint16_t *data, uint16_t timeout=I2Cdev::readTimeout, void *wireObj=0) ### Parameters - **devAddr** (uint8_t) - Required - I2C device address - **regAddr** (uint8_t) - Required - Starting register address - **length** (uint8_t) - Required - Number of words to read - **data** (uint16_t*) - Required - Pointer to store the read words - **timeout** (uint16_t) - Optional - Timeout in microseconds (default: I2Cdev::readTimeout) - **wireObj** (void*) - Optional - Pointer to a custom Wire object (default: 0) ### Returns - **int8_t** - Status: 0 on success, negative on error ``` -------------------------------- ### ACCEL_CONFIG (0x1C) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set accelerometer self-test, full-scale range, and high-pass filter mode. ```APIDOC ## ACCEL_CONFIG (0x1C) ### Description Get and set accelerometer self-test enables, full-scale range, and digital high-pass filter (DHPF) mode. ### Methods - `bool getAccelXSelfTest()` - Returns whether X-axis accelerometer self-test is enabled. - `void setAccelXSelfTest(bool enabled)` - Enables/disables X-axis accelerometer self-test. - `bool getAccelYSelfTest()` - Returns whether Y-axis accelerometer self-test is enabled. - `void setAccelYSelfTest(bool enabled)` - Enables/disables Y-axis accelerometer self-test. - `bool getAccelZSelfTest()` - Returns whether Z-axis accelerometer self-test is enabled. - `void setAccelZSelfTest(bool enabled)` - Enables/disables Z-axis accelerometer self-test. - `uint8_t getFullScaleAccelRange()` - Returns the current full-scale accelerometer range. - `void setFullScaleAccelRange(uint8_t range)` - Sets the full-scale accelerometer range. - `uint8_t getDHPFMode()` - Returns the current DHPF mode. - `void setDHPFMode(uint8_t mode)` - Sets the DHPF mode. ### Parameters #### setAccelXSelfTest / setAccelYSelfTest / setAccelZSelfTest - **enabled** (bool) - Required - Whether to enable self-test. #### setFullScaleAccelRange - **range** (uint8_t) - Required - The accelerometer full-scale range constant. #### setDHPFMode - **mode** (uint8_t) - Required - The DHPF mode value. ### Returns - `getAccelXSelfTest`, `getAccelYSelfTest`, `getAccelZSelfTest` return boolean indicating self-test enabled. - `getFullScaleAccelRange` returns the current full-scale range (uint8_t). - `getDHPFMode` returns the current DHPF mode (uint8_t). ``` -------------------------------- ### dmpInitialize() declaration Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Declares the dmpInitialize() method that performs the full DMP bring-up sequence. Returns 0 on success or 1 if the firmware write/verify fails. ```cpp uint8_t dmpInitialize(); // src/MPU6050_6Axis_MotionApps20.cpp:287 ``` -------------------------------- ### Constructor for MPU6050_9Axis_MotionApps41 Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-9axis-motionapps41.md Constructor that delegates to MPU6050_Base with the same contract. Takes an optional I2C address and wire object. ```cpp MPU6050_9Axis_MotionApps41(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0) : MPU6050_Base(address, wireObj) { } ``` -------------------------------- ### MPU6050_Base constructor Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Creates an MPU6050_Base instance with an optional I2C address and Wire object. The device is not touched until initialize() is called. ```APIDOC ## Constructor ### Description Creates an MPU6050_Base instance. The constructor only stores the address and wire object; the device is not touched until initialize() is called. ### Method Constructor ### Endpoint MPU6050_Base(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); ### Parameters #### Parameters - **address** (uint8_t) - Optional - I2C slave address; default is MPU6050_DEFAULT_ADDRESS (0x68, AD0 low). Use 0x69 when AD0 pin is high. - **wireObj** (void *) - Optional - Pointer to a TwoWire instance to use instead of the default Wire. Default is 0 (nullptr → global Wire). ### Request Example ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; // address 0x68, global Wire // MPU6050 mpu(0x69); // AD0 high // MPU6050 mpu(0x68, &Wire1); // custom Wire object void setup() { Wire.begin(); mpu.initialize(); } ``` ### Response #### Success Response No return value. ``` -------------------------------- ### Retrieve current FIFO packet Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-6axis-motionapps20.md Functions to get the current FIFO packet and the required packet size. ```cpp uint8_t dmpGetCurrentFIFOPacket(uint8_t *data); // src/MPU6050_6Axis_MotionApps20.cpp:630 uint16_t dmpGetFIFOPacketSize(); // src/MPU6050_6Axis_MotionApps20.cpp:624 — returns 42 ``` -------------------------------- ### MPU6050 class constructor signatures Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/configuration.md Every public class shares this constructor signature. The address defaults to MPU6050_DEFAULT_ADDRESS (0x68) and wireObj defaults to null, which uses the global Wire backend. ```cpp MPU6050_Base(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); MPU6050_6Axis_MotionApps20(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); MPU6050_6Axis_MotionApps612(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); MPU6050_9Axis_MotionApps41(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj= ``` -------------------------------- ### SELF_TEST factory trims (0x0D..0x10) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get factory trim values for accelerometer and gyroscope self-test. ```APIDOC ## SELF_TEST factory trims (0x0D..0x10) ### Description Get the factory trim values for accelerometer and gyroscope self-test registers. ### Methods - `uint8_t getAccelXSelfTestFactoryTrim()` - Returns the X-axis accelerometer factory trim. - `uint8_t getAccelYSelfTestFactoryTrim()` - Returns the Y-axis accelerometer factory trim. - `uint8_t getAccelZSelfTestFactoryTrim()` - Returns the Z-axis accelerometer factory trim. - `uint8_t getGyroXSelfTestFactoryTrim()` - Returns the X-axis gyroscope factory trim. - `uint8_t getGyroYSelfTestFactoryTrim()` - Returns the Y-axis gyroscope factory trim. - `uint8_t getGyroZSelfTestFactoryTrim()` - Returns the Z-axis gyroscope factory trim. ### Returns Each method returns the corresponding factory trim value (uint8_t). ``` -------------------------------- ### AUX I2C slave 4 (0x31..0x35) Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Get and set configuration for auxiliary I2C slave 4. ```APIDOC ## AUX I2C slave 4 (0x31..0x35) ### Description Get and set configuration for auxiliary I2C slave 4. ### Methods - `uint8_t getSlave4Address()` - Returns the I2C address of slave 4. - `void setSlave4Address(uint8_t address)` - Sets the I2C address of slave 4. - `uint8_t getSlave4Register()` - Returns the register address for slave 4. - `void setSlave4Register(uint8_t reg)` - Sets the register address for slave 4. - `void setSlave4OutputByte(uint8_t data)` - Sets the output byte for slave 4. - `bool getSlave4Enabled()` - Returns whether slave 4 is enabled. - `void setSlave4Enabled(bool enabled)` - Enables/disables slave 4. - `bool getSlave4InterruptEnabled()` - Returns whether slave 4 interrupt is enabled. - `void setSlave4InterruptEnabled(bool enabled)` - Enables/disables slave 4 interrupt. - `bool getSlave4WriteMode()` - Returns whether write mode is enabled for slave 4. - `void setSlave4WriteMode(bool mode)` - Sets write mode for slave 4. - `uint8_t getSlave4MasterDelay()` - Returns the master delay for slave 4. - `void setSlave4MasterDelay(uint8_t delay)` - Sets the master delay for slave 4. - `uint8_t getSlate4InputByte()` - Returns the input byte for slave 4 (note the spelling "Slate" in the source). ### Parameters #### Setter methods - **address** (uint8_t) - Required - The I2C address. - **reg** (uint8_t) - Required - The register address. - **data** (uint8_t) - Required - The output byte data. - **enabled** (bool) - Required - Whether to enable the setting. - **mode** (bool) - Required - The write mode. - **delay** (uint8_t) - Required - The master delay value. ### Returns - `getSlave4Address`, `getSlave4Register`, `getSlave4MasterDelay`, `getSlate4InputByte` return uint8_t. - `getSlave4Enabled`, `getSlave4InterruptEnabled`, `getSlave4WriteMode` return bool. ``` -------------------------------- ### testConnection() Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Verifies the MPU6050 connection by reading the WHO_AM_I register and checking the device ID. ```APIDOC ## testConnection() ### Description Reads the WHO_AM_I register (getDeviceID()) and returns true when the 6-bit device ID equals 0x34, 0x0C, or 0x3A. Use after initialize() to verify wiring. ### Method bool testConnection(); ### Parameters None ### Request Example ```cpp if (!mpu.testConnection()) { Serial.println("MPU6050 connection failed"); while (true); } ``` ### Response #### Success Response - **bool** - true if the device ID matches, false otherwise. ``` -------------------------------- ### I2Cdev::readWords Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Burst read of a specified number of 16-bit words, big-endian (MSB first), from a starting register address. ```APIDOC ## Method: I2Cdev::readWords ### Description Burst read of a specified number of 16-bit words, big-endian (MSB first), from a starting register address. ### Method READ ### Endpoint I2Cdev::readWords ### Parameters #### Path Parameters - **devAddr** (uint8_t) - Required - I2C slave device address (7-bit, e.g. `0x68`). - **regAddr** (uint8_t) - Required - Starting register address. - **data** (pointer) - Required - Buffer to store the read words. - **length** (uint8_t) - Required - Number of 16-bit words to read. - **timeout** (uint16_t) - Optional - Read timeout in ms; `0` disables it. Default: current `I2Cdev::readTimeout`. - **wireObj** (void *) - Optional - When non-zero, cast to `TwoWire*` and used in place of the global `Wire`. ### Response #### Success Response (0) - **data** (uint16_t*) - Buffer containing the read words. - **Return value** (int8_t) - Number of words read; `-1` on timeout/failure. #### Response Example ```cpp uint8_t devAddr = MPU6050_DEFAULT_ADDRESS; uint16_t buffer[3]; int8_t n = I2Cdev::readWords(devAddr, MPU6050_RA_GYRO_XOUT_H, buffer, 3); if (n < 0) { Serial.println("I2C read timed out"); } ``` ``` -------------------------------- ### MPU6050_Base constructor Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Constructs an MPU6050_Base object with an optional I2C address and TwoWire instance. The device is not touched until initialize() is called. ```cpp MPU6050_Base(uint8_t address=MPU6050_DEFAULT_ADDRESS, void *wireObj=0); ``` -------------------------------- ### initialize() overloads Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/mpu6050-base.md Two overloads of initialize(). The first sets clock source to MPU6050_CLOCK_PLL_XGYRO, gyro full-scale to MPU6050_GYRO_FS_250, accel full-scale to MPU6050_ACCEL_FS_2, and disables sleep. The second takes ACCEL_FS and GYRO_FS enum values; invalid values print an error and fall back to maximum range. ```cpp void initialize(); void initialize(ACCEL_FS accelRange, GYRO_FS gyroRange); ``` -------------------------------- ### I2Cdev::readBytes Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Burst read of a specified number of bytes from a starting register address. Reads are chunked by the Wire buffer length for compatibility. ```APIDOC ## Method: I2Cdev::readBytes ### Description Burst read of a specified number of bytes from a starting register address. Reads are chunked by the Wire buffer length for compatibility. ### Method READ ### Endpoint I2Cdev::readBytes ### Parameters #### Path Parameters - **devAddr** (uint8_t) - Required - I2C slave device address (7-bit, e.g. `0x68`). - **regAddr** (uint8_t) - Required - Starting register address. - **data** (pointer) - Required - Buffer to store the read bytes. - **length** (uint8_t) - Required - Number of bytes to read. - **timeout** (uint16_t) - Optional - Read timeout in ms; `0` disables it. Default: current `I2Cdev::readTimeout`. - **wireObj** (void *) - Optional - When non-zero, cast to `TwoWire*` and used in place of the global `Wire`. ### Response #### Success Response (0) - **data** (uint8_t*) - Buffer containing the read bytes. - **Return value** (int8_t) - Number of bytes read; `-1` indicates failure/timeout. #### Response Example ```cpp uint8_t devAddr = MPU6050_DEFAULT_ADDRESS; uint8_t buffer[6]; int8_t n = I2Cdev::readBytes(devAddr, MPU6050_RA_ACCEL_XOUT_H, buffer, 6); if (n < 0) { Serial.println("I2C read timed out"); } ``` ``` -------------------------------- ### I2Cdev::readBits / readBitsW Source: https://github.com/electroniccats/mpu6050/blob/master/_autodocs/api-reference/i2cdev.md Reads a bit field of specified length starting at the most significant bit position. The field is right-aligned into the data pointer. ```APIDOC ## Method: I2Cdev::readBits / readBitsW ### Description Reads a bit field of specified length starting at the most significant bit position. The field is right-aligned into the data pointer. ### Method READ ### Endpoint I2Cdev::readBits / readBitsW ### Parameters #### Path Parameters - **devAddr** (uint8_t) - Required - I2C slave device address (7-bit, e.g. `0x68`). - **regAddr** (uint8_t) - Required - Starting register address. - **bitStart** (uint8_t) - Required - Most-significant bit of the bit field. - **length** (uint8_t) - Required - Number of bits (≤ 8 for byte, ≤ 16 for word). - **data** (pointer) - Required - Container/buffer for the value (must be right-aligned). - **timeout** (uint16_t) - Optional - Read timeout in ms; `0` disables it. Default: current `I2Cdev::readTimeout`. - **wireObj** (void *) - Optional - When non-zero, cast to `TwoWire*` and used in place of the global `Wire`. ### Response #### Success Response (0) - **data** (uint8_t/uint16_t) - The right-aligned bit field value. #### Response Example ```cpp uint8_t devAddr = MPU6050_DEFAULT_ADDRESS; uint8_t b; int8_t n = I2Cdev::readBits(devAddr, MPU6050_RA_ACCEL_XOUT_H, 15, 5, &b); ``` ```