树莓派通过Python连接MPU6050时出现I2C错误121求助
树莓派MPU6050初始化OSError: [Errno 121] Remote I/O error问题排查
问题背景
我用树莓派搭配MPU6050传感器做自制无人机的pitch、roll、yaw检测,之前传感器工作正常,软件改动后初始化时出现I/O错误,硬件连接未做任何改动。
错误日志
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
I2C检测结果
通过i2cdetect -y 1能正常检测到传感器地址0x68:
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: -- -- -- -- -- -- -- --
相关代码片段
MPU类初始化
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
setUp初始化方法
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.
已尝试的解决方法
- 添加初始化延时(无效果)
- 修改总线编号(改为0会触发文件不存在错误)
- 修改激活指令参数(无效)
- 使用sudo运行脚本(无效)
- 确认I2C已启用(已开启)
传感器曾随机短暂恢复工作,无软硬件改动,说明传感器未完全失效,仅存在不稳定问题,求解决建议。
内容的提问来源于stack exchange,提问作者Mblizzard
相关产品推荐
相关产品推荐

