ESP8266 Micropython I2C Scanner

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

 

Code

[codesyntax lang=”python”]

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))

[/codesyntax]

 

Output

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!

download ok
exec(open(‘i2cscanner.py’).read(),globals())
Scan i2c bus…
i2c devices found: 1
Decimal address: 60 | Hexa address: 0x3c
>>>
>>>

LEAVE A REPLY

Please enter your comment!
Please enter your name here