You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cytron Maker Pi RP2040双VL53L1X传感器EIO错误求助

问题分析与解决方案

核心问题

你的代码存在两个关键问题:

  • I2C通道引脚未正确指定:RP2040的MicroPython中,I2C(channel)默认使用该I2C通道的标准引脚(I2C0默认GPIO0/GPIO1,I2C1默认GPIO2/GPIO3),但你的传感器实际连接的是替代引脚,导致I2C通信链路未正确建立。
  • 重复初始化硬件资源:每次调用读取函数时都重新创建I2C和VL53L1X实例,频繁的硬件初始化操作会触发I2C总线通信错误(OSError: [Errno 5] EIO)。

修复步骤

1. 正确初始化I2C通道

根据你的硬件连接,显式指定每个I2C通道对应的引脚:

  • Sensor0(前方):I2C1,SDA=GPIO26,SCL=GPIO27
  • Sensor1(后方):I2C0,SDA=GPIO16,SCL=GPIO17

2. 一次性初始化传感器实例

将I2C和VL53L1X实例的初始化放在类的初始化方法中,避免每次读取都重复初始化硬件。

修正后的代码

from machine import I2C, Pin
from vl53l1x import VL53L1X
from utime import sleep_ms, sleep

class DistanceSensor:
    def __init__(self):
        # 初始化前方传感器的I2C通道(I2C1)
        self.i2c_front = I2C(1, sda=Pin(26), scl=Pin(27))
        self.sensor_front = VL53L1X(self.i2c_front)
        # 初始化后方传感器的I2C通道(I2C0)
        self.i2c_rear = I2C(0, sda=Pin(16), scl=Pin(17))
        self.sensor_rear = VL53L1X(self.i2c_rear)
        
    def read_front(self):
        front_distance = self.sensor_front.read()
        print(f"{front_distance}mm")
        sleep_ms(50)
        return front_distance
        
    def read_rear(self):
        rear_distance = self.sensor_rear.read()
        print(f"{rear_distance}mm")
        sleep_ms(50)
        return rear_distance

# 实例化传感器对象
distance_sensor = DistanceSensor()

# 主循环
while True:
    distance_sensor.read_front()
    sleep(1)
    distance_sensor.read_rear()

额外注意事项

  • 确保传感器的供电稳定:VL53L1X需要3.3V供电,若供电不足也可能导致通信错误。
  • 检查硬件接线:确认SDA/SCL引脚没有接反,引脚焊接或杜邦线连接牢固。

内容的提问来源于stack exchange,提问作者Edward Gonell Rodríguez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 22:25:13