MPU9250 is not detecting magnetometer

I recently bought an MPU9250 sensor, but I’m facing an issue where the magnetometer is not working. The accelerometer and gyroscope are working fine, but I’m unable to get any data from the magnetometer. Has anyone faced this issue or can suggest possible reasons? Currently im using Raspberry pi 4. using at 400khz ,tried through by pass mode as well, the i2c bus just doesnt detect the magnetometer.

1 Like

This is a common issue actually. There are a lot of complaints about 9250s that are only good as 6500s.

What driver have you installed?

What OS exactly?

What does i2c detect -y 1 report?

Put the response between three back quotes.

BTW, the pypi.org imusensor is old (2020) you may want to try a newer driver.
I have one at LyricalDave/systests/imu/mpu9250/imusensor at main · slowrunner/LyricalDave · GitHub
that does not throw the name error, but it mine is also quite old.

Also try:

sudo pip3 install smbus2

nano smbus_name.py
  paste the following into the file:

#!/usr/bin/env python3

from smbus2 import SMBus

# Constants for MPU9250
I2C_BUS = 1            # 1 for Raspberry Pi, adjust as needed
DEVICE_ADDRESS = 0x68  # or 0x69 depending on ADO pin
WHO_AM_I_REG = 0x75    # WHO_AM_I register address
EXPECTED_ID = 0x71     # Default MPU9250 ID

# Connect to the I2C bus and read the register
with SMBus(I2C_BUS) as bus:
    device_id = bus.read_byte_data(DEVICE_ADDRESS, WHO_AM_I_REG)

print(f"Device ID returned: {hex(device_id)}")

if device_id == EXPECTED_ID:
    print("Success: MPU9250 detected!")
else:
    print("Warning: ID does not match MPU9250 (0x71).")
    print("Note: 0x70 indicates MPU6500, 0x73 indicates MPU9255.")

# -- end of file

then:

python3 smbus_name.py

Should see:

(gopigo3) ubuntu@U26LDave:~/LyricalDave/systests/imu/mpu9250$ ./smbus_name.py 
Device ID returned: 0x71
Success: MPU9250 detected!

For the record I tried pip3 install imusensor and it returns:

systests/imu/mpu9250$ ./basic.py 
The name is wrong [113]
roll: 179.5240509919115 ; pitch : -4.204397495575813 ; yaw : 95.8233859168819
roll: 179.2195657465436 ; pitch : -4.158256038577004 ; yaw : 94.13181615557843
roll: 179.3294960696544 ; pitch : -4.029941466920147 ; yaw : 92.83700684093053
roll: 179.55300783883763 ; pitch : -3.9746851074124003 ; yaw : 95.21146079256697
roll: 179.41438783613847 ; pitch : -4.13279260821899 ; yaw : 95.85528486414862

basic.py:

#!/usr/bin/env python3

"""
  PREREQ:  pip3 install imusensor
           pip3 install smbus

  2026 Output:

The name is wrong [113]
roll: 179.5240509919115 ; pitch : -4.204397495575813 ; yaw : 95.8233859168819
roll: 179.2195657465436 ; pitch : -4.158256038577004 ; yaw : 94.13181615557843

"""

import os
import sys
import time
import smbus

from imusensor.MPU9250 import MPU9250

address = 0x68
bus = smbus.SMBus(1)
imu = MPU9250.MPU9250(bus, address)

imu.begin()
# imu.caliberateGyro()
# imu.caliberateAccelerometer()
# or load your own caliberation file
#imu.loadCalibDataFromFile("/home/pi/calib_real_bolder.json")

while True:
	imu.readSensor()
	imu.computeOrientation()

	print ("roll: {0} ; pitch : {1} ; yaw : {2}".format(imu.roll, imu.pitch, imu.yaw))
	time.sleep(0.1)

You can also try:

wget https://github.com/slowrunner/LyricalDave/raw/refs/heads/main/systests/imu/mpu9250/mputrial.py

then:

python3 mputrial.py

It attempts to read and print everything:

(gopigo3) ubuntu@U26LDave:~/LyricalDave/systests/imu/mpu9250$ python3 mputrial.py 
Magnemometer Calibration
Gyros and Accelerometers Calibration
0
1
2
3
4

==== READ AND PRINT ====
MagXYZ:  230.4  -465.1   196.3 | GyroXYZ:  -0.0    0.0    0.0 | AccelXYZ:  -0.0   -0.0   -0.0 | Temp:45.0C | Time 11:58:09.823 

MagXYZ:  230.4   388.3   196.3 | GyroXYZ:  -0.0   -0.0   -0.0 | AccelXYZ:  -0.0   -0.0    0.0 | Temp:45.8C | Time 11:58:09.833 

MagXYZ: -196.3   -38.4  -230.4 | GyroXYZ:   0.0   -0.0    0.0 | AccelXYZ:   0.0   -0.0    0.0 | Temp:46.3C | Time 11:58:09.843 

MagXYZ: -622.9   -38.4   622.9 | GyroXYZ:   0.0   -0.0   -0.0 | AccelXYZ:   0.0   -0.0    0.0 | Temp:44.9C | Time 11:58:09.853 

MagXYZ: -196.3   814.9   622.9 | GyroXYZ:  -0.0   -0.0   -0.0 | AccelXYZ:   0.0    0.0   -0.0 | Temp:45.6C | Time 11:58:09.863 


==== 100 DIRECT READS AS FAST AS POSSIBLE ====
100 DIRECT READINGS TOOK 1.08 SECONDS, ACHIEVED 92 Hz 


==== THREADED DIRECT READS AS FAST AS POSSIBLE ====
17 THREADED READINGS ( 1.00 SECONDS at 17 Hz ) 

/n==== SLEEPING  ====
^C

This post was flagged by the community and is temporarily hidden.

1 Like