Hello everyone, for the past month or so I have been attempting to make a working and accurate IMU (Inertial Measurement Unit) using the GY-91 sensor, which packs the BMP280 and MPU9250 sensor into one model. I plan on using this sensor for a personalized drone controller which I will power using an Arduino pro mini. The only problem is that I only have basic programming knowledge so I have to copy/paste a lot of the code I write. I have been all over the internet looking for code that works, but none of the library's or YouTube videos have yet to fully explain all the details. I have used duckduckgo's chat bot to help me along the way, but still AI can only do so much. Here is the code that I have come up with so far for reading the raw values of the MPU9250 sensor:
#include <SoftwareWire.h>
SoftwareWire myWire(3,2); SDA pin 3, SCL pin 2
const int MPU9250_ADDRESS = 0x68; // MPU9250 I2C address
const int ACCEL_XOUT_H = 0x3B; // Register address for accelerometer X-axis high byte
const int GYRO_XOUT_H = 0x43; // Register address for gyroscope X-axis high byte
const int MAG_XOUT_L = 0x03; // Register address for magnetometer X-axis low byte
void setup() {
myWire.begin();
Serial.begin(9600);
myWire.beginTransmission(MPU9250_ADDRESS);
myWire.write(0x6B); // PWR_MGMT_1 register
myWire.write(0); // Wake up the MPU-9250
myWire.endTransmission(true);
}
void loop() {
int16_t accelX, accelY, accelZ;
int16_t gyroX, gyroY, gyroZ;
int16_t magX, magY, magZ;
// Read accelerometer data
myWire.beginTransmission(MPU9250_ADDRESS);
myWire.write(ACCEL_XOUT_H);
myWire.endTransmission(false);
myWire.requestFrom(MPU9250_ADDRESS, 6, true);
accelX = myWire.read() << 8 | myWire.read();
accelY = myWire.read() << 8 | myWire.read();
accelZ = myWire.read() << 8 | myWire.read();
// Read gyroscope data
myWire.beginTransmission(MPU9250_ADDRESS);
myWire.write(GYRO_XOUT_H);
myWire.endTransmission(false);
myWire.requestFrom(MPU9250_ADDRESS, 6, true);
gyroX = myWire.read() << 8 | myWire.read();
gyroY = myWire.read() << 8 | myWire.read();
gyroZ = myWire.read() << 8 | myWire.read();
// Read magnetometer data
myWire.beginTransmission(MPU9250_ADDRESS);
myWire.write(MAG_XOUT_L);
myWire.endTransmission(false);
myWire.requestFrom(MPU9250_ADDRESS, 6, true);
magX = myWire.read() | myWire.read() << 8;
magY = myWire.read() | myWire.read() << 8;
magZ = myWire.read() | myWire.read() << 8;
Serial.print("Accelerometer Data - X: ");
Serial.print(accelX);
Serial.print(", Y: ");
Serial.print(accelY);
Serial.print(", Z: ");
Serial.println(accelZ);
Serial.print("Gyroscope Data - X: ");
Serial.print(gyroX);
Serial.print(", Y: ");
Serial.print(gyroY);
Serial.print(", Z: ");
Serial.println(gyroZ);
Serial.print("Magnetometer Data - X: ");
Serial.print(magX);
Serial.print(", Y: ");
Serial.print(magY);
Serial.print(", Z: ");
Serial.println(magZ);
delay(1000); // Delay for 1 second
}
here are the results I get on the Serial monitor:
Accelerometer Data - X: -476, Y: -112, Z: 16224
Gyroscope Data - X: -330, Y: 439, Z: -98
Magnetometer Data - X: -24577, Y: -278, Z: 6624
Accelerometer Data - X: -532, Y: -96, Z: 16168
Gyroscope Data - X: -268, Y: 455, Z: -71
Magnetometer Data - X: -24577, Y: -278, Z: 6624
Accelerometer Data - X: -552, Y: -148, Z: 16132
Gyroscope Data - X: -257, Y: 416, Z: -66
Magnetometer Data - X: -24577, Y: -278, Z: 6624
As you can see, the Gyroscope and Accelerometer have error while at rest which mean they are working. But the Magnetometer data stays the same meaning it is not working. Do I need to initialize the Magnetometer sensor? Do I need to write a different address to the Magnetometer sensor?
What am I missing here?