"""P9-2 I2C Bus Scanner""" from machine import Pin, I2C i2c = I2C(1, scl = Pin(27), sda = Pin(26), freq = 400_000) dev_list = [] for dev_addr in range(8, 0x7F): # exclude special addresses try: i2c.writevto(dev_addr, ()) except OSError: # if OSError encountered pass else: # if no OSError dev_list.append(dev_addr) # add the device address to the list finally: # regardless of errors pass print(dev_list)