I2C failure when communicating with IMU (MPU6050 and RPi Pico)

I recently bought an MPU 6050 here: MPU-6050 Module 3 Axis Gyroscope + Accelerometer | Buy in Australia | 018-MPU-6050 | Core Electronics

I hooked it up with my Raspberry Pi Pico W where:
VCC → 3V3 pin
GND → GND pin
SCL → GP17 pin
SDA → GP16 pin

I downloaded two modules imu.py and vector3d.py from this Library as suggested by the product page above.

I then copied the sample code from the product page into Thonny:

from imu import MPU6050
from time import sleep
from machine import Pin, I2C

import math

i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)
imu = MPU6050(i2c)

while True:
    ax=round(imu.accel.x,2)
    ay=round(imu.accel.y,2)
    az=round(imu.accel.z,2)
    gx=round(imu.gyro.x)
    gy=round(imu.gyro.y)
    gz=round(imu.gyro.z)
    tem=round(imu.temperature,2)
        
    pitch = math.atan2(ax,az)
    roll = math.atan2(ay,az)
    
    print("Pitch",pitch,"/t","Roll",roll)
    
    sleep(0.2)

What I got consistently is this error:

Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "imu.py", line 106, in __init__
  File "imu.py", line 161, in chip_id
MPUException: I2C failure when communicating with IMU

Any idea what I’m doing wrong?

Cheers,
Hiep.

1 Like

Hi Hiep,

Can you share an image of your wiring? This error can arise if your device doesn’t detect the MPU.

1 Like

Hi Jack,

I don’t know if it is the culprit but I’ve found out that it may be the short circuit from the flux spread between the fresh solder joints on the MPU. Because I’m very new to soldering, I didn’t know that I had to clean the joints afterwards.

So I used isopropyl alcohol to clean the flux. And after wiring the MPU to the Pico again it all worked well!

As for the wiring, it looks something like this:


The wiring is as I described, and the Pico is powered by the USB connected to my PC.

2 Likes

Hi Help

I very much doubt that the flux would provide a short circuit, it is a pretty good insulator (that is if it is resin). It does not hurt to clean it though. Helps prevent other contaminants from collecting.

I think it may have been some other insidious problem you have corrected by rewiring. Like one of your wires making a poor connection.
Working now anyway.
Cheers Bob

2 Likes

Its a little hard to tell in your wiring pic.
On the MPU the Brown wire is SDA and the Grey is SCL.
Brown should go to GP16 & Grey to GP17 as per the Pico Pinouts.
A common problem with I2C is getting the pins the wrong way, first check to do in my book if it does not work.
Your pin soldering looks ok, cannot see any solder bridges.

Regards
Jim

PS I have had problems with that type of bread board too, with connections not working. The internal metal leafs are not the highest quality.

2 Likes

The soldering of the header to that little board looks very good actually. Excellent for a self confessed novice.

If you are going to use this sort of thing it really pays to go the extra mile and buy quality. Far less frustration on the long run. These things seem to be very much you get what you pay for.

A good policy.
While experimenting your breadboard etc should be first class. No good experimenting with intermittent connections and unknown quantities just because it is cheap. You could finish up with no hair.
Cheers Bob

1 Like

Straight after

i2c = I2C(0, sda=Pin(16), scl=Pin(17), freq=400000)

try to scan the bus first using

i2c.scan()

In my opinion this should be the first step to debugging prior to importing any libraries or trying to read any registers.

I was using the AltIMU-10 v6 carrier, that has three chips on board (LSM6DSO / LPS22DF / LIS3MDL), and using scan I could verify that only one device had failed.

3 Likes

Hey Mark,

Absolutely agree with this one, adding a scan function, even for a specific device or address being present is a great way to debug if you are having connection errors pop up.

It can save a lot of hassle as if it isn’t able to be communicated with at all, it minimises the scope of the issue.

Cheers,
Blayden

2 Likes

I’m also getting the same error and haven’t had a solution

Example code for PiicoDev Motion Sensor MPU6050

from machine import I2C,Pin
from PiicoDev_MPU6050 import PiicoDev_MPU6050
from PiicoDev_Unified import sleep_ms # Cross-platform compatible sleep function
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
i2c.scan()
motion = PiicoDev_MPU6050()

while True:

# Accelerometer data
accel = motion.read_accel_data() # read the accelerometer [ms^-2]
aX = accel["x"]
aY = accel["y"]
aZ = accel["z"]
print("x:" + str(aX) + " y:" + str(aY) + " z:" + str(aZ))

# Gyroscope Data

gyro = motion.read_gyro_data() # read the gyro [deg/s]

gX = gyro[“x”]

gY = gyro[“y”]

gZ = gyro[“z”]

print(“x:” + str(gX) + " y:" + str(gY) + " z:" + str(gZ))

# Rough temperature

temp = motion.read_temperature() # read the device temperature [degC]

print("Temperature: " + str(temp) + “°C”)

# G-Force

gforce = motion.read_accel_abs(g=True) # read the absolute acceleration magnitude

print("G-Force: " + str(gforce))

sleep_ms(100)

MPY: soft reboot
PiicoDev could not communicate with module at address 0x68, check wiring
Traceback (most recent call last):
File “main.py”, line 7, in
File “PiicoDev_MPU6050.py”, line 79, in init
File “PiicoDev_MPU6050.py”, line 75, in init
OSError: [Errno 5] EIO
MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040
Type “help()” for more information.

Hey there @Trnh280303,

When creating an I2C class, you need to assign it the associated I2C address, I see you have created the I2C object with the line below.

i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)

But when you create your MPU6050 class, you never actually assign the i2c object to this class, thus the Pico assumes an address, 0x68 in this case.

Hope this helps!

1 Like

The default for the PiicoDev libraries use pin 8 & 9 for I2C as they are the pins used on the Expansion Board. self.i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=freq)
This is defined in PiicoDev_Unified.py.

Your code uses pin 0 & 1 for the i2c.scan() function but is not passed to
motion = PiicoDev_MPU6050()

Try changing this to
motion = PiicoDev_MPU6050(bus=0, freq=400000, sda=Pin(0), scl=Pin(1))

Regards
Jim

1 Like