Pyboard 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 a Pyboard

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 the default I2C address differs from the sensor address that you buy

Layout

An example connecting a sensor to a Pyboard

 

Code

from pyb import I2C
 
i2c = I2C(1, I2C.MASTER, baudrate=100000)
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)
  print("Hex address: ",hex(device))

 

 

Output

All going well you should output like the following in uPyCraft – this is a TMP175 connected to a PyBoard (not a BMP180 as the layout earlier)

Ready to download this file,please wait!

download ok
exec(open(‘i2cscanner.py’).read(),globals())
Scan i2c bus…
i2c devices found: 1
Decimal address: 55
Hex address: 0x37
>>>

Share

Comments are closed.