This is a simple very short code example in which we show how to create a basic I2CScanner in Micropython using the uPyCraft IDE. In this example we tested it on a Wemos Mini which is an ESP8266 based board
The arduino equivalent of this code snippet is one that I use frequently and is one of the most useful sketches for figuring out why a sensor may not be working correctly with a library or code – usually because the default I2C address differs from the sensor you buy
Code
The SCL and SDA pins may change on other boards, this was for a Wemos Mini
import machine
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
print('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))
Output
All going well you should output like the following in uPyCraft – this was a Wemos OLED shield which was connected to a Wemos Mini
Ready to download this file,please wait!
…
download ok
exec(open(‘i2cscanner.py’).read(),globals())
Scan i2c bus…
i2c devices found: 1
Decimal address: 60 | Hexa address: 0x3c
>>>
>>>