Encountering an I2C error 121 when interfacing an MPU6050 with a Raspberry Pi using Python

425 views Asked by At

I am attempting to use an MPU6050 sensor with a Raspberry Pi, for the purpose of dectecting hte pitch, roll, and yaw of a DIY drone I'm building.

The MPU has worked in the past, somehow I've managed to change something in software, and I am now recieving the following error from my python script when I try to initialise the MPU:

Traceback (most recent call last):
  File "/home/pi/open-drone/Droneside/mpu.py", line 145, in <module>
    mpu.setUp() # Run MPU setuf functions
  File "/home/pi/open-drone/Droneside/mpu.py", line 56, in setUp
    self.bus.write_byte_data(self.address, 0x6B, 0x01)
OSError: [Errno 121] Remote I/O error

I have not changed the hardware connection to the Raspberry Pi's GPIO pins since the sensor was last working, and the sensor is correctly detected on address 0x68 by i2cdetect -y 1:

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --             

The relevant sections of code are shown below. I've trimmed these down a little to remove irrelevant print statements, and the multitude of variable initializations for the current pitch / yaw / roll etc.

class MPU: # Handels MPU
    def __init__(self, gyro, acc, tau):

        self.bus = smbus.SMBus(1) # Changing this to a '0' causes file not found error.
        self.address = 0x68
    def setUp(self):

        # Activate the MPU-6050
        self.bus.write_byte_data(self.address, 0x6B, 0x01) # Error occurs here.

        # Configure the accelerometer
        self.bus.write_byte_data(self.address, 0x1C, self.accHex)

        # Configure the gyro
        self.bus.write_byte_data(self.address, 0x1B, self.gyroHex)

mpu = MPU(gyro, acc, tau)
print("Waiting for I2C")
time.sleep(1) # This was a suggestion I saw from another post, didn't help.
print("Done waiting.")
mpu.setUp() # Run MPU setup functions

# Program does not get this far.

mpu.calibrateGyro(500) # Calibrate gyro w/ 500 pts, DRONE MUST BE LEVEL.

So far, I've tried inserting that one second delay shown in the last code block, which I saw suggested here. Didn't help. Also tried a 10s delay, which made no difference.

I've tried changing the bus in self.bus = smbus.SMBus(1) to a '0', as I saw in a few other code examples, which just caused an file not found error.

I've also tried changing self.bus.write_byte_data(self.address, 0x6B, 0x01) to self.bus.write_byte_data(self.address, 0x6B, 0x00), because I saw a couple of examples that had it that way, but that didn't work either.

Tried running everything with sudo, which didn't change anything, and I've checked that the I2C interface is enabled in sudo raspiconfig, it was already enabled.

Any suggestions would be greatly appreciated. Thanks in advance!

EDIT: MPU6050 randomly started working again, briefly. I have made no changes to software of hardware. Whatever this problem is, it's causing the MPU to be unreliable, but not entirely non functional.

0

There are 0 answers