### MPU6050 Manual Offset Configuration in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt Applies previously determined offset values to the MPU6050 sensor for consistent calibration across power cycles. This method requires obtaining the offset values beforehand, for example, by running the 'IMU_Zero' example. It then outputs the configured offsets and calibrated motion data. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Apply offsets obtained from calibration // (Run IMU_Zero example first to get these values) mpu.setXAccelOffset(-2241); mpu.setYAccelOffset(1647); mpu.setZAccelOffset(1399); mpu.setXGyroOffset(36); mpu.setYGyroOffset(-80); mpu.setZGyroOffset(16); Serial.println("Offsets applied:"); Serial.print("XAccel: "); Serial.println(mpu.getXAccelOffset()); Serial.print("YAccel: "); Serial.println(mpu.getYAccelOffset()); Serial.print("ZAccel: "); Serial.println(mpu.getZAccelOffset()); Serial.print("XGyro: "); Serial.println(mpu.getXGyroOffset()); Serial.print("YGyro: "); Serial.println(mpu.getYGyroOffset()); Serial.print("ZGyro: "); Serial.println(mpu.getZGyroOffset()); } void loop() { int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); Serial.print("Calibrated motion: "); 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(100); } ``` -------------------------------- ### MPU6050 DMP Initialization and Yaw/Pitch/Roll Output in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt Initializes the Digital Motion Processor (DMP) of the MPU6050 to obtain orientation data as yaw, pitch, and roll angles. This example includes sensor calibration and enables the DMP for processing. It outputs the calculated orientation angles in degrees. ```cpp #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" MPU6050 mpu; bool DMPReady = false; uint8_t devStatus; uint16_t packetSize; uint8_t FIFOBuffer[64]; Quaternion q; VectorFloat gravity; float ypr[3]; // yaw, pitch, roll void setup() { Wire.begin(); Wire.setClock(400000); // 400kHz I2C for better performance Serial.begin(115200); mpu.initialize(); if (!mpu.testConnection()) { Serial.println("MPU6050 connection failed"); while(1); } Serial.println("Initializing DMP..."); devStatus = mpu.dmpInitialize(); if (devStatus == 0) { // Calibrate sensor mpu.CalibrateAccel(6); mpu.CalibrateGyro(6); mpu.PrintActiveOffsets(); // Enable DMP Serial.println("Enabling DMP..."); mpu.setDMPEnabled(true); DMPReady = true; packetSize = mpu.dmpGetFIFOPacketSize(); Serial.println("DMP ready!"); } else { Serial.print("DMP Initialization failed (code "); Serial.print(devStatus); Serial.println(")"); } } void loop() { if (!DMPReady) return; // Get current FIFO packet (handles overflow automatically) if (mpu.dmpGetCurrentFIFOPacket(FIFOBuffer)) { // Get quaternion from DMP mpu.dmpGetQuaternion(&q, FIFOBuffer); // Calculate gravity vector mpu.dmpGetGravity(&gravity, &q); // Calculate yaw/pitch/roll mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); Serial.print("Yaw: "); Serial.print(ypr[0] * 180/M_PI); Serial.print("° Pitch: "); Serial.print(ypr[1] * 180/M_PI); Serial.print("° Roll: "); Serial.print(ypr[2] * 180/M_PI); Serial.println("°"); } } ``` -------------------------------- ### Initialize MPU6050 and Test Connection (Arduino) Source: https://context7.com/electroniccats/mpu6050/llms.txt Initializes the MPU6050 sensor using the I2C protocol and verifies if the device is connected and communicating correctly. It outputs the device ID to confirm successful initialization. This function is essential for starting any interaction with the sensor. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; // Default address 0x68 void setup() { Wire.begin(); Serial.begin(115200); // Initialize with default settings mpu.initialize(); // Verify connection if (mpu.testConnection()) { Serial.println("MPU6050 connection successful"); } else { Serial.println("MPU6050 connection failed"); while(1); } // Get device ID (should return 0x34 for MPU6050) uint8_t deviceID = mpu.getDeviceID(); Serial.print("Device ID: 0x"); Serial.println(deviceID, HEX); } void loop() { // Ready for sensor operations } ``` -------------------------------- ### MPU6050 FIFO Buffer Management in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt This example demonstrates how to enable and manage the FIFO buffer on the MPU6050 sensor. It configures the sensor to capture data at a 100Hz sample rate, reads data in batches from the FIFO, parses the motion6 data (accelerometer and gyroscope readings), and handles FIFO overflow conditions. Dependencies include the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Configure FIFO mpu.setFIFOEnabled(true); mpu.resetFIFO(); // Set sample rate (1kHz / (1 + rate)) mpu.setRate(9); // 100Hz sample rate Serial.println("FIFO buffer ready"); } void loop() { // Check FIFO count uint16_t fifoCount = mpu.getFIFOCount(); // Each motion6 reading is 12 bytes (6 int16_t values) if (fifoCount >= 12) { Serial.print("FIFO has "); Serial.print(fifoCount / 12); Serial.println(" complete readings"); uint8_t fifoBuffer[12]; mpu.getFIFOBytes(fifoBuffer, 12); // Parse data (big-endian format) int16_t ax = (fifoBuffer[0] << 8) | fifoBuffer[1]; int16_t ay = (fifoBuffer[2] << 8) | fifoBuffer[3]; int16_t az = (fifoBuffer[4] << 8) | fifoBuffer[5]; int16_t gx = (fifoBuffer[6] << 8) | fifoBuffer[7]; int16_t gy = (fifoBuffer[8] << 8) | fifoBuffer[9]; int16_t gz = (fifoBuffer[10] << 8) | fifoBuffer[11]; Serial.print("Motion6: "); Serial.print(ax); Serial.print(" "); Serial.print(ay); Serial.print(" "); Serial.print(az); Serial.print(" "); Serial.print(gx); Serial.print(" "); Serial.print(gy); Serial.print(" "); Serial.println(gz); } // Check for overflow if (fifoCount == 1024) { Serial.println("FIFO overflow! Resetting..."); mpu.resetFIFO(); } delay(100); } ``` -------------------------------- ### MPU6050 Power Management and Sleep Mode in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt This C++ example configures the MPU6050 for low power consumption. It sets the clock source to PLL for accuracy and reduces the sample rate to 50Hz to save power. The code demonstrates entering and exiting sleep mode, with a 5-second sleep duration. It also includes a commented-out section for setting up wake-on-motion detection. Dependencies include the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Configure for low power operation mpu.setClockSource(MPU6050_CLOCK_PLL_ZGYRO); // Use PLL for better accuracy // Set low sample rate for power saving mpu.setRate(19); // 50Hz sample rate Serial.println("Power management configured"); Serial.print("Sleep mode: "); Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); Serial.print("Clock source: "); Serial.println(mpu.getClockSource()); } void loop() { // Take a reading int16_t ax, ay, az; mpu.getAcceleration(&ax, &ay, &az); Serial.print("Reading: "); Serial.print(ax); Serial.print(" "); Serial.print(ay); Serial.print(" "); Serial.println(az); // Enter sleep mode Serial.println("Entering sleep mode..."); mpu.setSleepEnabled(true); delay(5000); // Sleep for 5 seconds // Wake up Serial.println("Waking up..."); mpu.setSleepEnabled(false); delay(10); // Wait for sensor to stabilize } // For wake-on-motion: void setupWakeOnMotion() { mpu.setWakeCycleEnabled(true); mpu.setMotionDetectionThreshold(20); mpu.setMotionDetectionDuration(20); mpu.setIntMotionEnabled(true); // Connect INT pin to wake-up interrupt } ``` -------------------------------- ### MPU6050 Digital Low Pass Filter (DLPF) Configuration in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt This code configures the Digital Low Pass Filter (DLPF) on the MPU6050 sensor to reduce noise and set a specific bandwidth. It sets the DLPF to 42Hz, which increases latency but improves signal quality. The sample rate is also configured to 100Hz. The example prints the current DLPF mode and sample rate divider. Dependencies include the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Set DLPF bandwidth to 42Hz // This reduces noise but increases latency mpu.setDLPFMode(MPU6050_DLPF_BW_42); // Set sample rate: 1kHz / (1 + 9) = 100Hz mpu.setRate(9); Serial.print("DLPF Mode: "); Serial.println(mpu.getDLPFMode()); Serial.print("Sample Rate Divider: "); Serial.println(mpu.getRate()); // Available DLPF settings: // MPU6050_DLPF_BW_256 - 256Hz bandwidth, 0.98ms delay // MPU6050_DLPF_BW_188 - 188Hz bandwidth, 1.9ms delay // MPU6050_DLPF_BW_98 - 98Hz bandwidth, 2.8ms delay // MPU6050_DLPF_BW_42 - 42Hz bandwidth, 4.8ms delay // MPU6050_DLPF_BW_20 - 20Hz bandwidth, 8.3ms delay // MPU6050_DLPF_BW_10 - 10Hz bandwidth, 13.4ms delay // MPU6050_DLPF_BW_5 - 5Hz bandwidth, 18.6ms delay } void loop() { int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); Serial.print("Filtered motion: "); Serial.print(ax); Serial.print(" "); Serial.print(ay); Serial.print(" "); Serial.print(az); Serial.print(" "); Serial.print(gx); Serial.print(" "); Serial.print(gy); Serial.print(" "); Serial.println(gz); delay(100); } ``` -------------------------------- ### Convert to world-frame acceleration for ROS integration Source: https://context7.com/electroniccats/mpu6050/llms.txt Transforms sensor-frame acceleration to world-frame coordinates for robotics navigation systems. Applies pre-calibrated offsets, computes orientation as yaw/pitch/roll and quaternions, and converts measurements to ROS-standard units (m/s² and radians). Streams comprehensive IMU data suitable for ROS node integration. ```cpp #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" #define EARTH_GRAVITY_MS2 9.80665 #define DEG_TO_RAD 0.017453292519943295 MPU6050 mpu; bool DMPReady = false; uint8_t FIFOBuffer[64]; Quaternion q; VectorInt16 aa; // Raw acceleration VectorInt16 aaWorld; // World-frame acceleration VectorFloat gravity; float ypr[3]; void setup() { Wire.begin(); Wire.setClock(400000); Serial.begin(115200); mpu.initialize(); if (mpu.dmpInitialize() == 0) { // Apply pre-calibrated offsets mpu.setXAccelOffset(-2241); mpu.setYAccelOffset(1647); mpu.setZAccelOffset(1399); mpu.setXGyroOffset(36); mpu.setYGyroOffset(-80); mpu.setZGyroOffset(16); mpu.setDMPEnabled(true); DMPReady = true; Serial.println("Publishing IMU data for ROS..."); } } void loop() { if (!DMPReady) return; if (mpu.dmpGetCurrentFIFOPacket(FIFOBuffer)) { // Get orientation mpu.dmpGetQuaternion(&q, FIFOBuffer); mpu.dmpGetGravity(&gravity, &q); mpu.dmpGetYawPitchRoll(ypr, &q, &gravity); // Get world-frame acceleration (gravity removed) mpu.dmpGetAccel(&aa, FIFOBuffer); mpu.dmpConvertToWorldFrame(&aaWorld, &aa, &q); // Convert to m/s² (ROS standard units) float accelX = aaWorld.x * mpu.get_acce_resolution() * EARTH_GRAVITY_MS2; float accelY = aaWorld.y * mpu.get_acce_resolution() * EARTH_GRAVITY_MS2; float accelZ = aaWorld.z * mpu.get_acce_resolution() * EARTH_GRAVITY_MS2; Serial.print("World Accel (m/s²): "); Serial.print(accelX, 4); Serial.print(" "); Serial.print(accelY, 4); Serial.print(" "); Serial.println(accelZ, 4); // Orientation in radians (ROS standard) Serial.print("YPR (rad): "); Serial.print(ypr[0], 4); Serial.print(" "); Serial.print(ypr[1], 4); Serial.print(" "); Serial.println(ypr[2], 4); // Quaternion for ROS Serial.print("Quaternion: "); Serial.print(q.w, 4); Serial.print(" "); Serial.print(q.x, 4); Serial.print(" "); Serial.print(q.y, 4); Serial.print(" "); Serial.println(q.z, 4); } } ``` -------------------------------- ### Retrieve quaternion data from MPU6050 DMP Source: https://context7.com/electroniccats/mpu6050/llms.txt Initializes MPU6050 sensor with Digital Motion Processor to stream quaternion orientation data. Calibrates accelerometer and gyroscope, enables DMP, and extracts normalized quaternion values (w, x, y, z) from FIFO buffer. Outputs orientation data to serial console with optional normalization for gimbal-lock-free representation. ```cpp #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" MPU6050 mpu; bool DMPReady = false; uint16_t packetSize; uint8_t FIFOBuffer[64]; Quaternion q; void setup() { Wire.begin(); Wire.setClock(400000); Serial.begin(115200); mpu.initialize(); if (mpu.dmpInitialize() == 0) { mpu.CalibrateAccel(6); mpu.CalibrateGyro(6); mpu.setDMPEnabled(true); DMPReady = true; packetSize = mpu.dmpGetFIFOPacketSize(); Serial.println("DMP ready - streaming quaternions"); } } void loop() { if (!DMPReady) return; if (mpu.dmpGetCurrentFIFOPacket(FIFOBuffer)) { // Get quaternion (w, x, y, z) mpu.dmpGetQuaternion(&q, FIFOBuffer); Serial.print("quat w:"); Serial.print(q.w, 4); Serial.print(" x:"); Serial.print(q.x, 4); Serial.print(" y:"); Serial.print(q.y, 4); Serial.print(" z:"); Serial.println(q.z, 4); // Normalize quaternion (optional, usually already normalized) float magnitude = sqrt(q.w*q.w + q.x*q.x + q.y*q.y + q.z*q.z); q.w /= magnitude; q.x /= magnitude; q.y /= magnitude; q.z /= magnitude; } } ``` -------------------------------- ### Motion Detection with Interrupts on MPU6050 Source: https://context7.com/electroniccats/mpu6050/llms.txt Configures the MPU6050 to trigger an interrupt when motion is detected, suitable for wake-on-motion applications. It sets a threshold and duration for motion detection and uses an external interrupt pin. Requires the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; const int INTERRUPT_PIN = 2; volatile bool motionDetected = false; void dmpDataReady() { motionDetected = true; } void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Configure motion detection mpu.setMotionDetectionThreshold(20); // Threshold (1mg/LSB) mpu.setMotionDetectionDuration(20); // Duration (1ms/LSB) // Configure interrupt pinMode(INTERRUPT_PIN, INPUT); mpu.setInterruptMode(false); // Active high mpu.setInterruptDrive(false); // Push-pull mpu.setInterruptLatch(true); // Latch until read mpu.setIntMotionEnabled(true); // Enable motion interrupt attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), dmpDataReady, RISING); Serial.println("Motion detection ready"); } void loop() { if (motionDetected) { motionDetected = false; // Read interrupt status to clear it uint8_t intStatus = mpu.getIntStatus(); if (intStatus & 0x40) { // Motion interrupt bit Serial.println("Motion detected!"); // Get motion status details uint8_t motionStatus = mpu.getMotionStatus(); Serial.print("Motion on axes: "); if (mpu.getXPosMotionDetected()) Serial.print("X+ "); if (mpu.getXNegMotionDetected()) Serial.print("X- "); if (mpu.getYPosMotionDetected()) Serial.print("Y+ "); if (mpu.getYNegMotionDetected()) Serial.print("Y- "); if (mpu.getZPosMotionDetected()) Serial.print("Z+ "); if (mpu.getZNegMotionDetected()) Serial.print("Z- "); Serial.println(); } } delay(10); } ``` -------------------------------- ### MPU6050 Automatic Calibration in C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt Automatically calculates and applies offset values to compensate for sensor bias using the MPU6050 library. This requires placing the sensor on a flat surface and keeping it still during calibration. It outputs calibrated motion data. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(9600); mpu.initialize(); if (!mpu.testConnection()) { Serial.println("Connection failed"); while(1); } Serial.println("Place sensor on flat surface and keep still..."); delay(3000); Serial.println("\nCalibrating accelerometer..."); mpu.CalibrateAccel(6); // 6 iterations = 600 readings Serial.println("Calibrating gyroscope..."); mpu.CalibrateGyro(6); Serial.println("\nCalibration complete!"); Serial.println("Active offsets:"); mpu.PrintActiveOffsets(); // Get offsets programmatically int16_t* offsets = mpu.GetActiveOffsets(); Serial.println("\nOffset array:"); Serial.print("XAccel: "); Serial.println(offsets[0]); Serial.print("YAccel: "); Serial.println(offsets[1]); Serial.print("ZAccel: "); Serial.println(offsets[2]); Serial.print("XGyro: "); Serial.println(offsets[3]); Serial.print("YGyro: "); Serial.println(offsets[4]); Serial.print("ZGyro: "); Serial.println(offsets[5]); } void loop() { // Sensor is now calibrated and ready int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); Serial.print("Calibrated data - "); Serial.print("ax: "); Serial.print(ax); Serial.print(" ay: "); Serial.print(ay); Serial.print(" az: "); Serial.print(az); Serial.print(" gx: "); Serial.print(gx); Serial.print(" gy: "); Serial.print(gy); Serial.print(" gz: "); Serial.println(gz); delay(200); } ``` -------------------------------- ### Extract gravity-removed linear acceleration with MPU6050 Source: https://context7.com/electroniccats/mpu6050/llms.txt Calculates linear acceleration by removing gravity components from raw accelerometer readings using DMP quaternion data. Retrieves gravity vector orientation, extracts sensor acceleration, and computes world-relative linear motion. Converts results to m/s² for physics applications and streams processed data via serial. ```cpp #include "I2Cdev.h" #include "MPU6050_6Axis_MotionApps20.h" MPU6050 mpu; bool DMPReady = false; uint8_t FIFOBuffer[64]; Quaternion q; VectorInt16 aa; // Raw acceleration VectorInt16 aaReal; // Gravity-removed acceleration VectorFloat gravity; void setup() { Wire.begin(); Wire.setClock(400000); Serial.begin(115200); mpu.initialize(); if (mpu.dmpInitialize() == 0) { mpu.CalibrateAccel(6); mpu.CalibrateGyro(6); mpu.setDMPEnabled(true); DMPReady = true; Serial.println("Streaming linear acceleration..."); } } void loop() { if (!DMPReady) return; if (mpu.dmpGetCurrentFIFOPacket(FIFOBuffer)) { // Get quaternion and gravity mpu.dmpGetQuaternion(&q, FIFOBuffer); mpu.dmpGetGravity(&gravity, &q); // Get raw acceleration mpu.dmpGetAccel(&aa, FIFOBuffer); // Remove gravity to get linear acceleration mpu.dmpGetLinearAccel(&aaReal, &aa, &gravity); Serial.print("Linear accel X:"); Serial.print(aaReal.x); Serial.print(" Y:"); Serial.print(aaReal.y); Serial.print(" Z:"); Serial.println(aaReal.z); // Convert to m/s² for physics applications float accelX_ms2 = aaReal.x * mpu.get_acce_resolution() * 9.80665; Serial.print("X: "); Serial.print(accelX_ms2); Serial.println(" m/s²"); } } ``` -------------------------------- ### Freefall Detection on MPU6050 Source: https://context7.com/electroniccats/mpu6050/llms.txt Detects freefall conditions using the MPU6050's dedicated feature, triggering an interrupt when acceleration drops below a set threshold for a specified duration. Useful for safety systems. Requires the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; const int INTERRUPT_PIN = 2; volatile bool freefallDetected = false; void freefallISR() { freefallDetected = true; } void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Configure freefall detection // Threshold: 0.5g (datasheet recommends 0.3-0.6g) mpu.setFreefallDetectionThreshold(10); // 10 * 0.05g = 0.5g // Duration: 50ms minimum mpu.setFreefallDetectionDuration(50); // 50ms // Configure interrupt pinMode(INTERRUPT_PIN, INPUT); mpu.setInterruptMode(false); mpu.setInterruptDrive(false); mpu.setInterruptLatch(true); mpu.setIntFreefallEnabled(true); attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), freefallISR, RISING); Serial.println("Freefall detection active"); } void loop() { if (freefallDetected) { freefallDetected = false; uint8_t intStatus = mpu.getIntStatus(); if (intStatus & 0x80) { // Freefall bit Serial.println("*** FREEFALL DETECTED ***"); // Read current acceleration to verify int16_t ax, ay, az; mpu.getAcceleration(&ax, &ay, &az); float totalAccel = sqrt(ax*ax + ay*ay + az*az); Serial.print("Total acceleration: "); Serial.println(totalAccel); } } delay(10); } ``` -------------------------------- ### Set MPU6050 Sensitivity Ranges and Read Resolution (Arduino) Source: https://context7.com/electroniccats/mpu6050/llms.txt Configures the full-scale measurement ranges for both the accelerometer and gyroscope on the MPU6050. It also retrieves and displays the resolution (sensitivity in physical units per LSB) based on the selected ranges. This allows for balancing measurement precision and the maximum detectable values. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Set accelerometer range to ±8g mpu.setFullScaleAccelRange(MPU6050_ACCEL_FS_8); // Set gyroscope range to ±500°/s mpu.setFullScaleGyroRange(MPU6050_GYRO_FS_500); // Get current resolutions float accelRes = mpu.get_acce_resolution(); float gyroRes = mpu.get_gyro_resolution(); Serial.print("Accel resolution: "); Serial.print(accelRes, 6); Serial.println(" g per LSB"); Serial.print("Gyro resolution: "); Serial.print(gyroRes, 6); Serial.println(" °/s per LSB"); // Verify settings uint8_t accelRange = mpu.getFullScaleAccelRange(); uint8_t gyroRange = mpu.getFullScaleGyroRange(); Serial.print("Accel range: ±"); Serial.print(2 << accelRange); Serial.println("g"); Serial.print("Gyro range: ±"); Serial.print(250 << gyroRange); Serial.println("°/s"); } void loop() { int16_t ax, ay, az, gx, gy, gz; mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); // Convert to physical units float accelX_g = ax * mpu.get_acce_resolution(); float gyroX_dps = gx * mpu.get_gyro_resolution(); Serial.print("Accel X: "); Serial.print(accelX_g); Serial.println(" g"); Serial.print("Gyro X: "); Serial.print(gyroX_dps); Serial.println(" °/s"); delay(500); } ``` -------------------------------- ### Read Multiple MPU6050 Sensors - C++ Source: https://context7.com/electroniccats/mpu6050/llms.txt This code snippet demonstrates how to read data from multiple MPU6050 sensors connected to the same I2C bus. By configuring different I2C addresses for each sensor, they can be accessed independently. This is useful for applications requiring data from multiple IMUs or for redundancy. ```cpp #include "I2Cdev.h" #include "MPU6050.h" // First sensor at default address (AD0 = LOW) MPU6050 mpu1(0x68); // Second sensor at alternate address (AD0 = HIGH) MPU6050 mpu2(0x69); void setup() { Wire.begin(); Serial.begin(115200); // Initialize both sensors Serial.println("Initializing MPU6050 #1..."); mpu1.initialize(); if (mpu1.testConnection()) { Serial.println("MPU6050 #1 connected"); } else { Serial.println("MPU6050 #1 failed"); } Serial.println("Initializing MPU6050 #2..."); mpu2.initialize(); if (mpu2.testConnection()) { Serial.println("MPU6050 #2 connected"); } else { Serial.println("MPU6050 #2 failed"); } // Configure both identically mpu1.setFullScaleAccelRange(MPU6050_ACCEL_FS_4); mpu2.setFullScaleAccelRange(MPU6050_ACCEL_FS_4); } void loop() { int16_t ax1, ay1, az1, gx1, gy1, gz1; int16_t ax2, ay2, az2, gx2, gy2, gz2; // Read from first sensor mpu1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1); // Read from second sensor mpu2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2); Serial.print("Sensor 1 - Accel: "); Serial.print(ax1); Serial.print(" "); Serial.print(ay1); Serial.print(" "); Serial.println(az1); Serial.print("Sensor 2 - Accel: "); Serial.print(ax2); Serial.print(" "); Serial.print(ay2); Serial.print(" "); Serial.println(az2); Serial.println("---"); delay(500); } ``` -------------------------------- ### Read Raw Accelerometer and Gyroscope Data (Arduino) Source: https://context7.com/electroniccats/mpu6050/llms.txt Reads raw data from all six axes (accelerometer: X, Y, Z; gyroscope: X, Y, Z) of the MPU6050 sensor simultaneously. The data is returned as 16-bit signed integers. This snippet is useful for direct sensor value acquisition without complex processing. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; int16_t ax, ay, az; // Accelerometer values int16_t gx, gy, gz; // Gyroscope values void setup() { Wire.begin(); Serial.begin(38400); mpu.initialize(); if (!mpu.testConnection()) { Serial.println("Connection failed"); while(1); } } void loop() { // Read all 6 axes at once mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz); Serial.print("Accel X: "); Serial.print(ax); Serial.print(" Y: "); Serial.print(ay); Serial.print(" Z: "); Serial.println(az); Serial.print("Gyro X: "); Serial.print(gx); Serial.print(" Y: "); Serial.print(gy); Serial.print(" Z: "); Serial.println(gz); delay(100); } ``` -------------------------------- ### Read Temperature from MPU6050 Source: https://context7.com/electroniccats/mpu6050/llms.txt Reads the internal temperature sensor of the MPU6050 and converts the raw value to Celsius and Fahrenheit. This is useful for environmental monitoring or temperature compensation. Requires the I2Cdev and MPU6050 libraries. ```cpp #include "I2Cdev.h" #include "MPU6050.h" MPU6050 mpu; void setup() { Wire.begin(); Serial.begin(115200); mpu.initialize(); // Enable temperature sensor (enabled by default) mpu.setTempSensorEnabled(true); } void loop() { // Read raw temperature value int16_t rawTemp = mpu.getTemperature(); // Convert to Celsius using datasheet formula float tempC = (rawTemp / 340.0) + 36.53; // Convert to Fahrenheit float tempF = (tempC * 9.0 / 5.0) + 32.0; Serial.print("Temperature: "); Serial.print(tempC, 2); Serial.print("°C ("); Serial.print(tempF, 2); Serial.println("°F)"); delay(1000); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.