【MicroPython ESP32】扫描I2C设备地址

【MicroPython ESP32】扫描I2C设备地址


  • 本示例基于Thonny平台开发。

通过I2C扫描获取挂载在外设上的设备地址,已方便管理和了解I2C设备是否正常运行状态。

  • 有关Micropython esp32 i2c外设功能介绍可以参考官方说明文档:
  • http://docs.micropython.org/en/latest/esp32/quickref.html#hardware-i2c-bus

ESP32 硬件I2C介绍

有两个标识符为01I2C硬件外设。任何可用的支持输出的引脚都可以用于SCL和SDA,但下面给出了默认值。

I2C(0)I2C(0)
SCL1825
SDA1926
  • 引用默认的I2C引脚方式:
from machine import Pin, I2C
i2c = I2C(0)# 默认的i2c0:scl:18 sda:19
i2c = I2C(1)# 默认的i2c1:scl:25 sda:26
  • 自定义引脚引用方式
from machine import Pin, I2C
i2c = I2C(1, scl=Pin(5), sda=Pin(4), freq=400000)

示例代码


from utime import sleep # 延时函数在utime库中
from machine import I2C,Pin
#i2c = I2C(scl=Pin(18), sda=Pin(19),freq=400000)
led = Pin(2,Pin.OUT)
i2c = I2C(0)
if __name__ == '__main__':
    while True:             # 无限循环
        print("helloworld")  # 打印"helloworld"字串到console中
        sleep(5)      # 打印完之后休眠1print('Scan i2c bus...')
        devices = i2c.scan()

        if len(devices) == 0:
            print("No i2c device !")
        else:
            print('i2c devices found:',len(devices))
            
            for device in devices:
                print("Decimal address: ",device," | Hexa address: ",hex(device))
                led.value(led.value()^1)

  • Shell调试窗口输出扫描到的I2C地址:
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值