This is a simple very short code example in which we show how to create a basic I2CScanner in Micropython using the uPyCraft IDE for an ESp8266.
The arduino equivalent 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 teh default I2C address differs from the sensor you buy
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))
All going well you should output like the following in uPyCraft – this is a Wemos OLED shield connected to a Wemos Mini
Ready to download this file,please wait!
Scan i2c bus…
i2c devices found: 1
Decimal address: 60 | Hexa address: 0x3c