tuupola / micropython-mpu9250

MicroPython I2C driver for MPU9250 9-axis motion tracking device
MIT License
145 stars 46 forks source link

Error on initializing MPU9250 I2C #30

Closed RuudKapteijn closed 11 months ago

RuudKapteijn commented 11 months ago

I am trying to read data from a MPU9250 from a MicroPyton program on a ESP32 WROOM.

Wiring (ESP - MPU): 3.3V - VCC, gnd - gnd, Pin(21) - SDA, Pin(22) - SCL.

Configuration: mpu9250.py, ak8963.py and mpu6500.py are on ESP32. No modifications to boot.py.

Code:

from machine import I2C, Pin, SoftI2C
from math import sqrt, atan2, pi, copysign, sin, cos
from mpu9250 import MPU9250
from time import sleep

i2c = I2C(scl=Pin(22), sda=Pin(21))
imu = MPU9250(i2c)

Error (on last line of code):

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "mpu9250.py", line 45, in __init__
  File "mpu6500.py", line 99, in __init__
  File "mpu6500.py", line 157, in whoami
  File "mpu6500.py", line 189, in _register_char
OSError: [Errno 116] ETIMEDOUT
tuupola commented 11 months ago

The error means Micropython code does not receive any answer from the I2C bus. I would first double check the wiring. You could also try scanning the bus.

i2c = I2C(scl=Pin(22), sda=Pin(21))
devices = map(hex, i2c.scan())
print("I2C devices found:", end=" ")
print(", ".join(devices))
RuudKapteijn commented 11 months ago

Thx for your quick answer. The i2c.scan results in an empty list/map.

I do not entirely trust the self soldered headers on the ESP32. I'll receive a pre-assembled one this week. I'll retry with that one and come back to you.

tuupola commented 11 months ago

Pull-up resistors for I2C might also help. Also double check that the pins are correct just in case.

RuudKapteijn commented 11 months ago

Once the right I2C pins are identified, it works fine on a pre-assembled Arduino nano ESP32.

Wiring: (ESP - MPU): 3.3V - VCC, gnd - gnd, Pin(A4) - SDA, Pin(A5) - SCL. Configuration: mpu9250.py, ak8963.py and mpu6500.py are on ESP32. No modifications to boot.py. Code:

from machine import I2C, Pin, SoftI2C
from math import sqrt, atan2, pi, copysign, sin, cos
from time import sleep
from mpu9250 import MPU9250
from lsm303d import LSM303D

# Arduino nano ESP32 pinout: https://docs.arduino.cc/hardware/nano-esp32
i2c = SoftI2C(scl=Pin(12), sda=Pin(11)) # GPIO pin 12 is A5/D22 on the board, GPIO pin 11 is A4/D21 on the board

print(f"I2C scan 1: {i2c.scan()}")
imu = MPU9250(i2c)
print("MPU9250 id: " + hex(imu.whoami))

Output:

MPY: soft reboot
I2C scan 1: [12, 104]
MPU9250 id: 0x71

Sorry for the confusion and thx for the support.

tuupola commented 11 months ago

No problem. Glad it worked.