ESP32用MicroPython读GY6500 MPU6500遇ETIMEDOUT及读数为0问题
ESP32(MicroPython)读取MPU6500传感器故障排查求助
我正在使用ESP32开发板,通过MicroPython读取集成加速度计与陀螺仪的GY6500 MPU6500传感器数据,遇到两个核心问题:
- 运行时偶尔触发ETIMEDOUT错误,定位为I2C读写寄存器操作失败;
- 程序能正常执行时,即使物理移动传感器,读取到的加速度、陀螺仪数据始终为0。
所用代码
# main.py -- put your code here! import utime from time import sleep from machine import Timer, Pin, SoftI2C #from mpu9250 import MPU9250 from mpu6500 import MPU6500, SF_DEG_S, SF_M_S2 # MPU6500 module was adjusted from: # https://github.com/tuupola/micropython-mpu9250 # set up I2C serial communication protcol i2c = SoftI2C(scl=Pin(25), sda=Pin(27)) # create MPU6500 instance mpu6500 = MPU6500(i2c, accel_sf=SF_M_S2, gyro_sf=SF_DEG_S) #mpu6500 = MPU9250(i2c) # read sensor function def read_sensor(stringa): print(stringa, mpu6500.acceleration, mpu6500.gyro) # hardware timer setup #timer = Timer(0) #timer.init(period=500, mode=Timer.PERIODIC, callback=read_sensor) #for loop to read data every sleep time period for i in range(0, 1000): read_sensor(i * 10) sleep(10/1000)
ETIMEDOUT错误信息
File "/lib/mpu6500.py", line 101, in __init__ File "/lib/mpu6500.py", line 189, in _accel_fs File "/lib/mpu6500.py", line 186, in _register_char OSError: [Errno 116] ETIMEDOUT MicroPython v1.20.0 on 2023-04-26; ESP32 module with ESP32 Type "help()" for more information.
查看mpu6500.py源码,错误出在self.i2c.readfrom_mem_into(self.address, register, buf)或self.i2c.writeto_mem(self.address, register, buf)的I2C操作环节。
数据全为0的输出示例
>>> ets Jun 8 2016 00:22:57 rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:2 load:0x3fff0030,len:4656 load:0x40078000,len:13284 ho 0 tail 12 room 4 load:0x40080400,len:3712 entry 0x4008064c 0 (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) 10 (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) 20 (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) 30 (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) 40 (0.0, 0.0, 0.0) (0.0, 0.0, 0.0) [...]
已尝试的排查步骤
- 核对接线正确性,按MPU6500文档将FSYNC引脚接GND;
- 分别尝试3.3V和5V两种供电方式;
- 在代码中交换SCL与SDA引脚配置;
- 调整传感器读取周期为5ms、10ms、100ms、1000ms;
- 将mpu6500.py中的原生I2C替换为SoftI2C(原生I2C已弃用)。
以上尝试均未解决问题,恳请协助排查。
内容的提问来源于stack exchange,提问作者Andrea Zanni
相关产品推荐
相关产品推荐

