Skip to content
This repository has been archived by the owner on Aug 25, 2023. It is now read-only.

Magnetometer always zero, Accel + Gyro working fine #72

Closed
darrenrahnemoon opened this issue Nov 14, 2021 · 9 comments
Closed

Magnetometer always zero, Accel + Gyro working fine #72

darrenrahnemoon opened this issue Nov 14, 2021 · 9 comments

Comments

@darrenrahnemoon
Copy link

darrenrahnemoon commented Nov 14, 2021

Hi guys,

I have the following code:

#include <MPU9250.h>
#include <Wire.h>

MPU9250 sensor;

void setup() {
	Serial.begin(115200);
	Serial.println("Setup Started.");
	Wire.begin();

	delay(2000);
	setupSensor();

	Serial.println("Setup Finished.");
	delay(1000);
}

void loop() {
	if (sensor.update()) {
		clearScreen();
		Serial.println("Roll: " + (String)sensor.getRoll() + ", Pitch: " + (String)sensor.getPitch() + ", Yaw: " + (String)sensor.getYaw());
		Serial.println("Accelerometer X: " + (String)sensor.getAccX() + ", Y: " + (String)sensor.getAccY() + ", Z: " + (String)sensor.getAccZ());
		Serial.println("Gyroscope X: " + (String)sensor.getGyroX() + ", Y: " + (String)sensor.getGyroY() + ", Z: " + (String)sensor.getGyroZ());
		Serial.println("Compass X: " + (String)sensor.getMagX() + ", Y: " + (String)sensor.getMagY() + ", Z: " + (String)sensor.getMagZ());
		Serial.println("Temperature: " + (String)sensor.getTemperature());
	}
	delay(100);
}

// Helper functions
void clearScreen() {
	Serial.write(27);		// ESC command
	Serial.print("[2J");	// clear screen command
	Serial.write(27);
	Serial.print("[H");	 // cursor to home command
}

void setupSensor() {
	sensor.setup(0x68);
	delay(5000);
	calibrateSensor();
}

void calibrateSensor() {
	Serial.println("Calibrating Accelerometer and Gyroscope.");
	sensor.calibrateAccelGyro();

	Serial.println("Calibrating Compass.");
	sensor.calibrateMag();
}

When I run this I do get some values for the accelerometer and gyroscope. However my magnetometer doesn't return anything (always returns to zero).

Sample:

Roll: 0.00, Pitch: -0.00, Yaw: -7.51
Accelerometer X: -0.03, Y: -0.07, Z: 1.01
Gyroscope X: -2.56, Y: -2.26, Z: 1.28
Compass X: 0.00, Y: 0.00, Z: 0.00
Temperature: 28.61

My roll, pitch and yaw are also always stuck at 0, 0, -7.51.

I am running the calibration sequence (for mag moving it around the path 8 when calibrating) every time but still no results. Any clue as to what I am doing wrong?

I am also getting some values for the bias when I print them so it's still doing something.

Mag Bias [mG]: -234.26, 55.20, 446.37 
Mag scale []: 1.20, 1.02, 0.85 

My hardware setup is an Arduino Nano with its A4, A5 being connected to SDA, SCL of the module

@Chrisrdouglas
Copy link
Contributor

Chrisrdouglas commented Nov 28, 2021

I had that issue with the magnetometer on one of the ones I ordered. The device I ordered was defective. ordered a new one and ran my same code a few days later and it worked

@a7-github
Copy link

I had that issue with the magnetometer on one of the ones I ordered. The device I ordered was defective. ordered a new one and ran my same code a few days later and it worked

I will have to disagree with you on that @Chrisrdouglas , I managed to write a code that works consistently well, and another that has that problem on the exact same setup, I have no idea why that really happens but it seems to be a timing issue. I am curious tho to see if the working code works constantly on multiple platforms as I'm using the esp8266 rather than the Arduino nano so @darrenrahnemoon I would like you to try it and see if it does work for you and tell us about it.

working code

#include <Arduino.h>
#include <MPU9250.h>

#define I2CADDRESS 0x68

MPU9250 mpu;

float ypr[3];

void setup() {

Serial.begin(115200);
Wire.begin();
if (!mpu.setup(I2CADDRESS)) {
while (1) {
Serial.println("MPU connection failed. Please check your connection with connection_check example.");
digitalWrite(LED_BUILTIN, LOW);
delay(100);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
}

}

Serial.println("Accel Gyro calibration will start in a sec.");
Serial.println("Please leave the device still on the flat plane.");
mpu.verbose(true);
delay(1000);
mpu.calibrateAccelGyro();
Serial.println("Mag calibration will start in a sec.");
Serial.println("Please Wave device in a figure eight five times.");
delay(1000);
mpu.calibrateMag();
mpu.verbose(false);

}

void loop() {

if (mpu.update()) {
ypr[0] = mpu.getEulerZ();
ypr[1] = mpu.getEulerY();
ypr[2] = mpu.getEulerX();
Serial.printf("yaw: %f, pitch: %f, roll: %f\n", ypr[0], ypr[1], ypr[2]);
}

}

@Chrisrdouglas
Copy link
Contributor

Oh, that's interesting! So if it was a timing issue then would that only affect the values for the magnetometer? Or would the values for the gyro and accelerometer be affected as well?

@a7-github
Copy link

@Chrisrdouglas that is a great point.
I have no clue why only the magnetometer value is the only one that gets affected, but I think I pinned it down the issue happens if I delay mpu.update() with more than 15mSec, no clear idea why and if it is only on some version of the sensor or not. It is different than what I thought initially I thought it had to be something with the calibration but no it's not. I also would love to know if you experience the same issue @Chrisrdouglas on your setup.

@Chrisrdouglas
Copy link
Contributor

So when I ran the calibration code it was on a Teensy 3.2 and I used the calibration.ino file. I didn't do anything fancy with it at that point. That's when I discovered that the magnetometer was just reading all zeros. The second one I got worked flawlessly. I think that's all the information I have for ya.

Here's the one I purchased: http://www.hiletgo.com/ProductDetail/1953399.html

@hideakitai
Copy link
Owner

Maybe related to #52

@hideakitai
Copy link
Owner

I am closing this is not a problem for the library (the device is not real MPU9250).

@Nahida-Majeed
Copy link

hi how to check, the value (x,y,z) from the magnetometer sensor is correct reading or not?

@Chrisrdouglas
Copy link
Contributor

@Nahida-Majeed,

Yse your_mpu.getMagX(), your_mpu.getMagY(), and your_mpu.getMagZ(). if you're getting zeros then you most likely got sold a defective/fake copy.

I'd verify the output by moving a magnet near one of the sides and seeing how it affects the value. if it goes up while the magnet gets closer then i would assume that means you got a good one

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants