How to use the MPU9250 IMU sensor with Arduino

  • The MPU9250 combines accelerometer, gyroscope and magnetometer in a single module.
  • It easily communicates with Arduino using the I2C protocol to get accurate readings.
  • Magnetometer calibration is crucial to eliminate magnetic errors and improve accuracy.
  • Filters such as the complementary filter can improve accuracy and eliminate drift.

mpu9250

The world of inertial sensors has evolved rapidly, and devices such as the MPU9250, which combines an accelerometer, gyroscope and magnetometer in a single module, have become a key piece for robotics projects, drones and systems that require accurately capturing small and large movements. In this article, we are going to explore how to use this sensor with Arduino, what are its most notable features, as well as some code examples to get started working with it.

Using the MPU9250 is not only useful for hobbyists, but also for professionals who need to accurately measure orientation and movement. This solution allows the development of stabilization systems, autonomous vehicles and robots that require knowledge of their movements in the different axes. The versatility of the sensor, together with its precision and low cost, has earned it a solid reputation among developers.

What is the MPU9250?

El MPU9250 It is a module that includes accelerometer, gyroscope and magnetometer in a single device. With this combination, the sensor is able to measure both linear acceleration and angular velocity, and the magnetic field of its environment. This Invensense sensor has 9 degrees of freedom, which means that it can measure in three different axes, both acceleration, rotation (gyroscope) and magnetic field (magnetometer), thus giving the possibility of calculating the complete orientation of the device.

The module is designed for communicate via SPI or I2C, which allows it to be easily connected to open source platforms such as Arduino or Raspberry Pi. In addition, thanks to the Digital Motion Processor (DMP), is capable of performing complex calculations to fuse the data obtained by the three sensors and provide more accurate measurements.

Main features of the MPU9250

The MPU9250 stands out for having a large number of features that make it a very interesting module for projects that require capturing precise movements, among which are:

  • Accelerometer: Adjustable acceleration range between ±2g, ±4g, ±8g, and ±16g.
  • Gyroscope: Programmable range of ±250°/s, ±500°/s, ±1000°/s, ±2000°/s.
  • Magnetometer: Sensitivity of 0.6µT/LSB and programmable range up to 4800µT.
  • Energy consumption: Very low, ideal for portable devices or those requiring operation for long periods (3.5 mA in active mode).

Connecting the MPU9250 module with Arduino

mpu9250 arduino

Connecting the module to your Arduino is a simple procedure thanks to the fact that it works using the I2C protocol. typical connection diagram between an MPU9250 and a Arduino Uno is

  • VCC: Connect it to 3.3V.
  • GND: To ground (GND).
  • SDA: Connect it to pin A4 of the Arduino.
  • SCL: Connect it to pin A5 of the Arduino.

It is important to make sure that the power supply is correct so that the sensor can work properly. Most modules already have a voltage regulator so that you can use the 5V from the Arduino without damaging it.

Code examples for the MPU9250

Here's how you can start programming the MPU9250 on Arduino, reading data from the accelerometer, gyroscope, and magnetometer. The library MPU9250.h It is very useful to facilitate programming, and in our example we detail how to read raw data:

#include <Wire.h>
#include <MPU9250.h>
MPU9250 imu(Wire, 0x68);

void setup() {
    Wire.begin();
    Serial.begin(115200);
    if (imu.begin() != 0) {
        Serial.println("Error al iniciar MPU9250");
    } else {
        Serial.println("MPU9250 iniciado");
    }
}

void loop() {
    imu.readSensor();
    Serial.print("Aceleracion: ");
    Serial.print(imu.getAccelX_mss());
    Serial.print(", ");
    Serial.print(imu.getAccelY_mss());
    Serial.print(", ");
    Serial.print(imu.getAccelZ_mss());
    Serial.println();
    delay(1000);
}

This code reads all three components of acceleration. Gyroscope and magnetometer readings can be performed similarly using the methods getGyroX_rads() y getMagX_uT() respectively.

Practical applications

There are multiple applications where the MPU9250 becomes an indispensable tool. Let's explore some of the most important ones:

  • Drones and robotics: One of the most common uses of the MPU9250 is in flight stabilization and robotics systems, where obtaining real-time orientation is essential.
  • Virtual reality: By accurately capturing orientation and motion, the sensor can be used for tracking in gaming applications or virtual reality simulators.
  • Navigation systems: In combination with other sensors, such as GPS, the MPU9250 is used in inertial navigation to understand movements and detect orientation.

Calibration of the magnetometer

One of the most important steps when using the MPU9250 is the magnetometer calibrationThe magnetometer is indispensable for eliminating errors generated by the magnetic environment (such as building footage or interference from other electronic equipment), so proper calibration is crucial for obtaining accurate measurements.

To properly calibrate the magnetometer, we can use the RTIMULib-Arduino library. Below is a simple calibration program:

#include <RTIMULib.h>
RTIMU *imu;
RTIMUSettings settings;

void setup() {
    Wire.begin();
    Serial.begin(115200);
    imu = RTIMU::createIMU(&settings);
    imu->IMUInit();
    imu->setCalibrationMode(true);
}

void loop() {
    if (imu->IMURead()) {
        RTVector3 mag = imu->getCompass();
        Serial.print("Magnetómetro: ");
        Serial.print(mag.x());
        Serial.print(", ");
        Serial.print(mag.y());
        Serial.print(", ");
        Serial.print(mag.z());
        Serial.println();
    }
}

The above code reads data from the magnetometer so you can perform axis movements to cover the full range of possible readings. This helps identify magnetic field distortions and improve orientation calculations.

Filters to improve accuracy

To improve the accuracy of the MPU9250 readings, one of the most common approaches is the Implementation of filters that combine data obtained from the gyroscope, accelerometer and magnetometer.

El complementary filter is an effective and easy-to-implement solution. This filter relies on the gyroscope to deliver fast results, while the accelerometer and magnetometer correct for long-term drift of the gyroscope. A simple code implementing this filter can be seen in the following example:

#include <ComplementaryFilter.h>
ComplementaryFilter cf;

void setup() {
    cf.setAccelerometerGain(0.02);
    cf.setMagnetometerGain(0.98);
}

void loop() {
    // Integrar lecturas de acelerómetro y giroscopio
    cf.update(sensorData.accelX, sensorData.gyroX);
    float pitch = cf.getPitch();
    float roll = cf.getRoll();
    Serial.print("Pitch: ");
    Serial.print(pitch);
    Serial.print(" Roll: ");
    Serial.println(roll);
}

This filter is essential to eliminate gyroscope drift and allows for a more stable orientation. It is also much faster to execute on microcontrollers such as the Arduino than more complex methods such as the Kalman filter, which consume more resources.

The MPU9250 is an incredibly versatile solution for a wide variety of projects that require accurate measurement of orientation and motion. Connecting it to an Arduino and getting basic readings is relatively easy, and with the implementation of some filters, very accurate and useful results can be obtained for a wide range of applications.