MPU9250 I2C problem

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
rockjail
Posts: 2
Joined: Fri Apr 28, 2017 3:43 pm

MPU9250 I2C problem

Post by rockjail » Fri Apr 28, 2017 4:08 pm

Hi all,

I've been trying to access the magnetometer on the MPU9250 via I2C for a while now, but didn't get anywhere close. Its address doesn't show up on i2c.scan() (but the one for acc/gyro does).

I got pythoncoders IMU classes working on the ESP8266 it's just the magnetometer that is a bit stubborn.

I'm fairly new to the world of microcontrollers and appreciate any help :)

Thanks!

woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Re: MPU9250 I2C problem

Post by woodat » Fri Apr 28, 2017 11:37 pm

I've actually had exactly the same issue (though don't know how to fix it, sorry). I can scan() my PFC8574 port expander (so I know I2C is working) but it doesn't pick up the sensor (or, for that matter, a few other items). Any recommendations are welcome.

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

Re: MPU9250 I2C problem

Post by pythoncoder » Sat Apr 29, 2017 5:31 am

@rockjail It sounds as if the I2C implementation on the ESP8266 is flaky. As you've undoubtedly figured out an I2C scan should show the main chip on addresses 104 and 105 and the magnetometer chip on address 12 (decimal). If the mag doesn't appear, we have a problem.

I haven't studied porting the library to the ESP8266. Clearly the pyb stuff needs replacing with machine; a revised library would need testing on ESP8266 and Pyboard, also with the MPU9150.

I'll investigate the I2C issue. If I can figure out what's going on I'll raise an issue. It may be a week or so before I find time to look at any of this.
Peter Hinch
Index to my micropython libraries.

rockjail
Posts: 2
Joined: Fri Apr 28, 2017 3:43 pm

Re: MPU9250 I2C problem

Post by rockjail » Sat Apr 29, 2017 10:51 am

@woodat I'm a bit relieved now that i'm not the only one.

@pythoncoder Thanks for your help!
Regarding the port to ESP8266, there isn't a lot that needs changing. As you mentioned, pyb needs replacing with machine, mainly the construction of the I2C buss: ( and the delay before that, but I've removed that for now.)
self._mpu_i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
For the fusion class it's time instead of pyb.
Importing both MPU9250 and fusion will lead to a memory error. I haven't looked into cross compiling yet, afaik that should help to get around that.

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

Re: MPU9250 I2C problem

Post by pythoncoder » Sun May 14, 2017 5:55 pm

Re the magnetometer issue I've now looked at this and can't replicate the fault. However here is some jiggery-pokery required to make the mag respond, and it's possible that you haven't spotted everything that has to happen first. I wired the MPU9250 with data on pin 12 and clock on pin 14, both pins with 1K pullups to 3.3V. I ran the following code:

Code: Select all

from machine import I2C, Pin
buf3 = bytearray('\x00\x00\x00')
buf1 = bytearray('\x00')

scl = Pin(14, Pin.OUT)
sda = Pin(12, Pin.OUT)
i2c = I2C(-1, scl, sda)  # Clk 14 Data 12
i2c.scan()  # will show only 105
i2c.readfrom_mem_into(105, 0x75, buf1)
if buf1[0] != 113:
    print('Bad chip ID')
i2c.writeto_mem(105, 0x6B, b'\x01')  # wake
i2c.writeto_mem(105, 0x37, b'\x02')  # passthrough
i2c.writeto_mem(105, 0x6a, b'\x00')
i2c.scan()  # should now show 12

i2c.writeto_mem(12, 0x0A, b'\x0f')  # ROM access
i2c.readfrom_mem_into(12, 0x10, buf3)
print(buf3)
The final print statement produces the magnetometer correction values.

So, for the MPU9250 at least, the ESP8266 I2C implementation comes out with flying colours :D
Peter Hinch
Index to my micropython libraries.

Post Reply