3

I've been trying to talk to my MPU6050 with a Pi Pico running MicroPython. I can verify the I2C is working because when running i2c.scan(), I get the MPU6050 address(0x68). However, when trying to read a specific register from the MPU6050, I always get 0x00, except for when I read the WHO_AM_I register, which just gives me the letter h??? I have my code below, I hope I'm just being an idiot that doesn't know I2C, because I've never even touched it until today. If anybody can help me, it would be greatly appreciated!

import machine

sda = machine.Pin(4)
scl = machine.Pin(5)
i2c = machine.I2C(0, sda = sda, scl = scl)

i2c.writeto(0x68, b'\x42') # Read the TEMP_OUT_L register
gyro = i2c.readfrom(0x68, 1)

print(gyro)

I've provided a link to the datasheet of my MPU6050 right here, again, any help would be appreciated!

Jonas
  • 121,568
  • 97
  • 310
  • 388
inzig0
  • 51
  • 1
  • 3
  • (1) If "i2c.(scan)" works, and you can read "WHO_AM_I", your Pico I2C hardware and software is basically setup OK. (2) Perhaps the MPU6050 is not initialized properly. (3) One troubleshooting trick is to test another I2C device that does not need any initialization and dead easy to read/write any register, eg PCF8574. – tlfong01 May 20 '22 at 03:10
  • 1
    @tlfong01, by changing my `print()` statement to `print(hex(int.from_bytes(gyro, "little")))`, I can see that the WHO_AM_I register is correct, and the h is to be expected! I originally tested the MPU6050 on an Arduino, so I'm just trying to adapt the Arduino example. I guess I'll have to read more into the datasheet, thanks for the help! – inzig0 May 20 '22 at 04:37
  • 1
    (1) Thank you for your update. (2) Two more references you might find useful: (a) Rpi4 I2C bus not working correctly - tlfong01 SO Asked 2020jul06 https://stackoverflow.com/questions/62757687/raspberry-pi-4-i2c-bus-not-working-correctly/62786449#62786449, (b) How to set I2C bus speed v0.2 (with ADXL345 Accelero working code and scope timing display) https://penzu.com/public/2450c7ae. – tlfong01 May 20 '22 at 04:54
  • BTW, I am also playing with Pico, with Thonny MicroPython. I have not played with MPU6050 yet. – tlfong01 May 20 '22 at 04:58
  • 1
    Think I found the issue, I have to tie the SLEEP bit down in the PWR_MGMT_1 register down, trying to figure out how to set the R/W bit now. I'll add an answer once I get it working. – inzig0 May 20 '22 at 05:08
  • Ha, setting/resetting sleep bit is the common way to initialize MPU6xxx/9xxx (and other I2C/SPI devices). Actually operating code for MPU6050 is indeed simple. So you are almost there. Good luck and cheers. – tlfong01 May 20 '22 at 06:42

1 Answers1

0

I found the issue, I did not tie the SLEEP bit low in the PWR_MGMT_1 register. When it's high, it will put the device into a sleeping state, where none of the registers return non-zero values. When its tied high, it will throw real values. Remember kids, always pull down the SLEEP bit on your MPU6050!

inzig0
  • 51
  • 1
  • 3