Problem working MPU9250

C programming, build, interpreter/VM.
Target audience: MicroPython Developers.
Post Reply
nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Problem working MPU9250

Post by nikhiledutech » Wed Apr 25, 2018 5:25 am

Hey,

I am interfacing IMU10DOF sensor ( Mpu9250 at addr 0x68 and BMP280 at addr 0x77) with STM32F4 disc board.

I referred this module: https://github.com/micropython-IMU/micropython-mpu9x50

For using MPU9250 we need to create instance of I2C, which i created below.
from pyb import I2C
from mpu9250 import MPU9250

x = I2C(1,I2C.MASTER)

Now to use MPU9250, I need to pass the above created I2C instance .

Code: Select all

from pyb import I2C
from mpu9250 import MPU9250
import micropython
micropython.alloc_emergency_exception_buf(100)

# Note: with a magnetometer read in the callback, a frequency of 1KHz hogged the CPU
tim = pyb.Timer(4, freq=20)            # freq in Hz

[b]x = I2C(1,I2C.MASTER)
imu = MPU9250(x)
[/b]
def cb(timer):                          # Callback: populate array members
    imu.get_gyro_irq()
    imu.get_accel_irq()
    imu.get_mag_irq()
#    print(imu.accel.ixyz)

tim.callback(cb)
#print("waiting one second")
print("You should see slightly different values on each pair of readings.")
print("            Accelerometer                               Gyro                                Magnetometer")
#pyb.delay(1)
for count in range(10):
    pyb.delay(400)                      # Ensure an interrupt occurs to re-populate integer values
    scale = 6.6666                      # Correction factors involve floating point
    mag = list(map(lambda x, y : x*y/scale, imu.mag.ixyz, imu.mag_correction))
    print("Interrupt:", [x/16384 for x in imu.accel.ixyz], [x/131 for x in imu.gyro.ixyz], mag)
    pyb.delay(100)
    print("Normal:   ", imu.accel.xyz, imu.gyro.xyz, imu.mag.xyz)
    print()

tim.callback(None)


In the above code, i get following error. ""ValueError: Invalid I2C instance"""
Can anyone help to resolve it.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Problem working MPU9250

Post by pythoncoder » Wed Apr 25, 2018 8:41 am

I don't have a discovery board but I would try the following which would work on a Pyboard:

Code: Select all

from pyb import I2C
x = I2C(1,I2C.MASTER) 
x.scan()
This should return the I2C addresses of the MPU9250 (12 and either 104 or 105 depending on the AD0 pin).
Peter Hinch
Index to my micropython libraries.

nikhiledutech
Posts: 118
Joined: Wed Dec 27, 2017 8:52 am

Re: Problem working MPU9250

Post by nikhiledutech » Wed Apr 25, 2018 9:12 am

Hey,

I have tried your method (below mention ) before and bumped me with same error of " No such I2C instance"


But I solved the above issue by importing I2C from machine.
from mpu9250 import MPU9250
from machine import I2C

x = I2C(1, I2C.MASTER)
imu = MPU9250 (x)
And the above code work properly.

Still confused why pyb.I2C gave me error :o

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Discovery board and pyb library.

Post by pythoncoder » Wed Apr 25, 2018 9:13 am

Anyone with a Discovery board care to comment?
Peter Hinch
Index to my micropython libraries.

Post Reply