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
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!
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
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.
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
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.
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.
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.
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))