MPU-9250 Accelerometer, Gyroscope, and Magnetometer Code

MPU-9250 Accelerometer, Gyroscope, and Magnetometer Code

In this tutorial, we will explore how to use the MPU-9250 sensor, which combines an accelerometer, gyroscope, and magnetometer in one compact module. By the end of this guide, you'll be able to read sensor data and interpret its meaning in your projects. This sensor is particularly useful for applications like robotics and drones, where orientation and motion tracking are critical. You can refer to the video for additional clarification on the setup and code (in video at 00:20).

ELE_MPU6050_208
ELE_MPU6050_208

Hardware Explained

The MPU-9250 module integrates three sensors: an accelerometer, a gyroscope, and a magnetometer. The accelerometer measures acceleration forces, allowing you to determine the speed and orientation. The gyroscope provides angular velocity, helping you understand the orientation change over time. Lastly, the magnetometer acts as a compass, providing magnetic field data which helps in navigation.

This combination of sensors is particularly beneficial in applications where precise motion tracking is required, such as in drones or smartphones. The MPU-9250 communicates via I2C or SPI, making it versatile for different microcontroller setups.

Datasheet Details

Manufacturer InvenSense
Part number MPU-9250
Logic/IO voltage 1.8 V (I/O), 3.3 V (supply)
Supply voltage 2.4 - 3.6 V
Output current (per channel) 3.2 mA (normal operation)
Peak current (per channel) 19.8 mA (max)
PWM frequency guidance N/A
Input logic thresholds 0.3 * VDD (low), 0.7 * VDD (high)
Voltage drop / RDS(on) / saturation N/A
Thermal limits -40 to 85 °C
Package QFN
Notes / variants Includes internal voltage regulator
  • Ensure proper voltage supply (2.4 - 3.6 V) to avoid damaging the module.
  • Use a pull-up resistor for SDA and SCL lines if not already included on your breakout board.
  • Verify I2C address (default is 0x68) and adjust ADO pin accordingly for alternative addresses.
  • Check connections for stability to prevent erroneous readings.
  • Calibrate sensors regularly for accurate measurements.

Wiring Instructions

Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for MPU6050 using A4 and A4 for SDA and SCL
Arduino Wiring for MPU-6050
Arduino Wiring for MPU-6050
Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for MPU6050 using A4 and A4 for SDA and SCL

To wire the MPU-9250 to your Arduino, start by connecting the VCC pin on the MPU-9250 to the 5V pin on the Arduino. The module has an internal regulator, so it's safe to power it with 5V. Next, connect the GND pin to the ground (GND) on the Arduino. For data communication, connect the SDA pin to the A4 pin on the Arduino and the SCL pin to the A5 pin. This configuration is standard for many Arduino boards.

If you are using an Arduino Mega, the SDA and SCL pins are located at pins 20 and 21, respectively. Ensure that the connections are secure to avoid any communication errors. If you need to change the I2C address of the MPU-9250, connect the ADO pin to 5V to set it to 0x69. Disconnecting it will revert it back to 0x68.

Arduino Schematic for  MPU6050 using A4 and A4 for SDA and SCL
Arduino Schematic for MPU6050 using A4 and A4 for SDA and SCL

Code Examples & Walkthrough

In the Arduino sketch, we start by including the MPU9250 library and creating an instance of the sensor:

#include "MPU9250.h"
MPU9250 IMU(Wire,0x68);

The code initializes the sensor and begins communication in the setup() function:

void setup() {
  Serial.begin(115200);
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    while(1) {}
  }
}

Within the loop() function, we continuously read and print the sensor data:

void loop() {
  IMU.readSensor();
  Serial.print("AccelX: ");
  Serial.print(IMU.getAccelX_mss(),6);
  // More print statements for other axes
}

This section reads the accelerometer, gyroscope, and magnetometer values, displaying them in the Serial Monitor. Make sure to check the full code loaded below the article for the complete implementation.

Demonstration / What to Expect

When you run the code, you should see the sensor values updating in real-time on the Serial Monitor. The accelerometer values will fluctuate as you move the sensor, while the gyroscope values will show the rate of rotation. The magnetometer will give you the magnetic field strength in three axes, which can be used to determine orientation. Be cautious of floating inputs, as they can lead to inaccurate readings (in video at 12:30).

Chapters

  • Introduction (00:00)
  • Hardware Explained (01:30)
  • Wiring Instructions (04:00)
  • Code Examples & Walkthrough (06:00)
  • Demonstration (10:00)

Images

ELE_MPU6050_208
ELE_MPU6050_208
Arduino Wiring for MPU-6050
Arduino Wiring for MPU-6050
Arduino Schematic for  MPU6050 using A4 and A4 for SDA and SCL
Arduino Schematic for MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for MPU6050 using A4 and A4 for SDA and SCL
126-Arduino code for an MPU-9250 accelerometer, gyroscope, and magnetometer
Langue: C++
Copié !

Ressources et références

Fichiers📁

Aucun fichier disponible.