Skip to content

Instantly share code, notes, and snippets.

@tzechienchu
Created October 25, 2022 10:01
Show Gist options
  • Save tzechienchu/e5c2069a50df75c7f522faac5b4bc3f9 to your computer and use it in GitHub Desktop.
Save tzechienchu/e5c2069a50df75c7f522faac5b4bc3f9 to your computer and use it in GitHub Desktop.
Pico Micropython as I2C Test for Scan and Read/Write
from machine import Pin, I2C
import binascii
def i2c_read(i2c,reg):
rx_buf = bytearray(1)
reg_add = bytearray(1)
reg_add[0] = reg
i2c.writeto(0x55, reg_add)
rx_buf = i2c.readfrom(0x55, 1)
return binascii.hexlify(rx_buf)
def i2c_write(i2c, reg, val):
tx_buf = bytearray(2)
tx_buf[0] = reg
tx_buf[1] = val
i2c.writeto(0x55, tx_buf)
i2c = I2C(0, scl=Pin(1), sda=Pin(0), freq=400_000)
print(i2c.scan())
if (True):
buf = i2c_read(i2c, 0x01)
print(buf)
buf = i2c_read(i2c, 0x02)
print(buf)
buf = i2c_read(i2c, 0x00)
print(buf)
buf = i2c_read(i2c, 0x03)
print(buf)
buf = i2c_write(i2c, 0x00, 0xaa)
buf = i2c_read(i2c, 0x00)
print(buf)
buf = i2c_write(i2c, 0x01, 0xbb)
buf = i2c_read(i2c, 0x01)
print(buf)
buf = i2c_write(i2c, 0x02, 0xcc)
buf = i2c_read(i2c, 0x02)
print(buf)
buf = i2c_write(i2c, 0x03, 0xdd)
buf = i2c_read(i2c, 0x03)
print(buf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment