Unable to interface mpu6050 with RPI PICO with micropython.
import machine
import utime
MPU6050_ADDR = 0x68 # MPU6050 I2C address
def read_i2c_word(i2c, address):
# Read a 16-bit word from the specified I2C address
return (i2c.readfrom_mem(address, 0x3B, 2)[0] << 8) | i2c.readfrom_mem(address, 0x3C, 2)[1]
def read_accel_gyro(i2c):
# Read accelerometer and gyroscope data
accel_x = read_i2c_word(i2c, MPU6050_ADDR)
accel_y = read_i2c_word(i2c, MPU6050_ADDR + 2)
accel_z = read_i2c_word(i2c, MPU6050_ADDR + 4)
gyro_x = read_i2c_word(i2c, MPU6050_ADDR + 8)
gyro_y = read_i2c_word(i2c, MPU6050_ADDR + 10)
gyro_z = read_i2c_word(i2c, MPU6050_ADDR + 12)
return (accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z)
def main():
i2c = machine.I2C(0, sda=machine.Pin(0), scl=machine.Pin(1), freq=400000) # I2C bus on Pico
while True:
accel_data = read_accel_gyro(i2c)
print("Accelerometer (X, Y, Z):", accel_data[:3])
print("Gyroscope (X, Y, Z):", accel_data[3:])
print("")
utime.sleep(1) # Sleep for 1 second
if __name__ == "__main__":
main()
Throwed this error on Thony ide:
Traceback (most recent call last):
File "<stdin>", line 34, in <module>
File "<stdin>", line 26, in main
File "<stdin>", line 13, in read_accel_gyro
File "<stdin>", line 8, in read_i2c_word OSError: [Errno 5] EIO
tried with different Pico boards and different mpu6050 modules. Same issue