MicroPython I2C Scanner

MicroPython I2C Scanner, tested on Raspberry Pi Pico running CircuitPython v1.19.1.

mpy_i2c_scanner.py
import machine
import os

print(os.uname())
print()
# How many hardware I2C supported
# and show the I2C info
print("number of I2C supported:")
print("========================")
i=0
while True:
    try:
        print(machine.I2C(i))
    except ValueError as e:
        print(e)
        break
    i += 1
print()

#Using machine.SoftI2C
#scl = machine.Pin(9, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
#sda = machine.Pin(8, mode=machine.Pin.OUT, pull=machine.Pin.PULL_UP)
#i2c = machine.SoftI2C(scl, sda)

#Using machine.I2C
i2c = machine.I2C(0)
print(i2c)
print()

print('=== I2C Scanner ===')
devices = i2c.scan()

if len(devices) == 0:
    print("No I2C device found!")
else:
    print('I2C devices found:',len(devices))
    for device in devices:
        print(device, ":", hex(device))



Comments

Popular posts from this blog

MicroPython/ESP32-C3 + 1.8" 128x160 TFT ST7735 SPI, using boochow/MicroPython-ST7735 library.

CameraWebServe: ESP32-S3 (arduino-esp32) + OV5640 camera module