ESP32 and MPU-9250 sensor example

The MPU-9250 is the company’s second generation 9-axis Motion Processing Unit™ for smartphones, tablets, wearable sensors, and other consumer markets. The MPU-9250, delivered in a 3x3x1mm QFN package, is the world’s smallest 9-axis MotionTracking device and incorporates the latest InvenSense design innovations, enabling dramatically reduced chip size and power consumption, while at the same time improving performance and cost.

The MPU-9250 MotionTracking device sets a new benchmark for 9-axis performance with power consumption only 9.3µA and a size that is 44% smaller than the company’s first-generation device. Gyro noise performance is 3x better, and compass full scale range is over 4x better than competitive offerings.
The MPU-9250 is a System in Package (SiP) that combines two chips: the MPU-6500, which contains a 3-axis gyroscope, a 3-axis accelerometer, and an onboard Digital Motion Processor™ (DMP™) capable of processing complex MotionFusion algorithms; and the AK8963, the market leading 3-axis digital compass. The MPU-9250 supports InvenSense’s market proven MotionFusion. A single design can support the MPU-9250 or MPU-6500, providing customers the flexibility to support either device in different product SKUs.

Improvements include supporting the accelerometer low power mode with as little as 6.4µA of and it provides improved compass data resolution of 16-bits (0.15 µT per LSB). The full scale measurement range of ±4800µT helps alleviate compass placement challenges on complex pcb’s

The MPU-9250 software drivers are fully compliant with Google’s Android 4.1 Jelly Bean release, and support new low-power DMP capabilities that offload the host processor to reduce power consumption and simplify application development. The MPU-9250 includes MotionFusion and run-time calibration firmware that enables consumer electronics manufacturers to commercialize cost effective motion-based functionality.

More info –


LOLIN32 Connection MPU-9250 connection
3v3 Vcc
Gnd Gnd
SDA – 21 SDA
SCL – 22 SCL


I used the – it was the easiest to use, I had to change the SDA and SCL defines for my LOLIN32 board from the default

#define SDA_PIN 21
#define SCL_PIN 22

#include <MPU9250_asukiaaa.h>

#ifdef _ESP32_HAL_I2C_H_
#define SDA_PIN 21
#define SCL_PIN 22

MPU9250 mySensor;

void setup() {


#ifdef _ESP32_HAL_I2C_H_
// for esp32
Wire.begin(SDA_PIN, SCL_PIN); //sda, scl



// you can set your own offset for mag values
// mySensor.magXOffset = -50;
// mySensor.magYOffset = -55;
// mySensor.magZOffset = -10;

void loop() {
Serial.println("print accel values");
Serial.println("accelX: " + String(mySensor.accelX()));
Serial.println("accelY: " + String(mySensor.accelY()));
Serial.println("accelZ: " + String(mySensor.accelZ()));
Serial.println("accelSqrt: " + String(mySensor.accelSqrt()));

Serial.println("print mag values");
Serial.println("magX: " + String(mySensor.magX()));
Serial.println("maxY: " + String(mySensor.magY()));
Serial.println("magZ: " + String(mySensor.magZ()));
Serial.println("horizontal direction: " + String(mySensor.magHorizDirection()));

Serial.println("at " + String(millis()) + "ms");



open the serial monitor and you should get something like this

print accel values
accelX: 0.91
accelY: -0.04
accelZ: 0.39
accelSqrt: 0.99
print mag values
magX: -17
maxY: 71
magZ: -46
horizontal direction: -13.47
at 156214ms



MPU-9250 GY-9250 9-axis sensor module I2C/SPI Communications Thriaxis gyroscope + triaxial accelerometer+triaxial magnetic field

Previous articleESP32 and a L3GD20 sensor example
Next articleESP32 and DHT11 sensor example