I2C
I2C implements a two-wire serial bus using an SDA data line and an SCL clock line. Use it to communicate with Unit, Hat, and other I2C peripherals.
MicroPython Example
Scan PORT.A on a CoreS3 or most S3-based controllers.
from hardware import I2C, Pin
i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
print(i2c0.scan())
Scan PORT.A on Tough.
from hardware import I2C, Pin
i2c0 = I2C(0, scl=Pin(33), sda=Pin(32), freq=100000)
print(i2c0.scan())
API
class I2C
- class I2C(id, *, scl, sda, freq=400000)
Construct an I2C bus object.
- 参数:
id (int) – I2C peripheral id.
- 关键字参数:
scl – SCL clock pin.
sda – SDA data pin.
freq (int) – I2C bus frequency in Hz.
MicroPython Code Block:
from hardware import I2C, Pin i2c0 = I2C(0, scl=Pin(1), sda=Pin(2), freq=100000)
- scan()
Scan all I2C addresses between 0x08 and 0x77 and return a list of responding addresses.
MicroPython Code Block:
print(i2c0.scan())
- readfrom_mem(addr, memaddr, nbytes, *, addrsize=8)
Read
nbytesfrom a device register.