The MPU9250/GY-91 is a multi-faceted motion tracking device that integrates a 3-axis gyroscope, a 3-axis accelerometer, and a 3-axis compass. This 9-axis sensor is widely used in the field of robotics, wearable devices, and motion-based game controllers, providing precise data for orientation and motion sensing.
Pin Number | Pin Name | Description |
---|---|---|
1 | VCC | Power supply (2.4V - 3.6V) |
2 | GND | Ground |
3 | SCL/SCLK | I2C clock/SPI clock |
4 | SDA/SDI | I2C data/SPI data input |
5 | AD0/SDO | I2C address selection/SPI data output |
6 | NCS | SPI chip select (active low) |
7 | INT | Interrupt output |
8 | FSYNC | Frame synchronization digital input (optional) |
#include <Wire.h>
// MPU9250 I2C address (depends on AD0 pin, check datasheet)
const int MPU9250_ADDRESS = 0x68;
void setup() {
Wire.begin(); // Initialize I2C
Serial.begin(9600); // Start serial communication at 9600 baud
setupMPU9250(); // Setup MPU9250 registers for operation
}
void loop() {
// Read sensor data and print it
readSensorData();
delay(100); // Delay for readability
}
void setupMPU9250() {
// Write to the power management register to wake up the MPU9250
writeMPU9250Register(MPU9250_ADDRESS, 0x6B, 0x00);
}
void writeMPU9250Register(byte address, byte reg, byte data) {
Wire.beginTransmission(address);
Wire.write(reg);
Wire.write(data);
Wire.endTransmission();
}
void readSensorData() {
Wire.beginTransmission(MPU9250_ADDRESS);
Wire.write(0x3B); // Starting register for accelerometer data
Wire.endTransmission(false);
Wire.requestFrom(MPU9250_ADDRESS, 14, true); // Request 14 bytes from the MPU9250
// Read and process accelerometer, gyroscope, and temperature data
// Accelerometer data
int16_t ax = Wire.read() << 8 | Wire.read();
int16_t ay = Wire.read() << 8 | Wire.read();
int16_t az = Wire.read() << 8 | Wire.read();
// Temperature data (not used in this example)
Wire.read(); Wire.read();
// Gyroscope data
int16_t gx = Wire.read() << 8 | Wire.read();
int16_t gy = Wire.read() << 8 | Wire.read();
int16_t gz = Wire.read() << 8 | Wire.read();
// Print the sensor data
Serial.print("Accel: ");
Serial.print("X="); Serial.print(ax);
Serial.print(" Y="); Serial.print(ay);
Serial.print(" Z="); Serial.println(az);
Serial.print("Gyro: ");
Serial.print("X="); Serial.print(gx);
Serial.print(" Y="); Serial.print(gy);
Serial.print(" Z="); Serial.println(gz);
}
Q: Can the MPU9250/GY-91 be used with both 3.3V and 5V microcontrollers? A: Yes, but ensure that the voltage levels for communication are compatible or use level shifters if necessary.
Q: How can I change the I2C address of the MPU9250? A: The I2C address can be changed by connecting the AD0/SDO pin to either VCC or GND.
Q: What is the default I2C address of the MPU9250? A: The default I2C address is 0x68 when AD0/SDO is connected to GND. It changes to 0x69 when connected to VCC.
Q: How do I interpret the raw data from the MPU9250? A: The raw data needs to be converted using the sensitivity levels specified in the datasheet. This will give you the actual measurements in the appropriate units (e.g., g for acceleration, °/sec for gyroscope, μT for magnetometer).