micro:bit and searching i2c

Whilst this is more of a bookmark for myself it may be handy for others who are playing with the i2c bus.

If your unsure if the i2c device you have connected up is working then it's a good idea to scan the i2c bus and see whats available.

The following code, from our friends at the microbit playground will scan the address space and display any discovered devices in REPL.


from microbit import *
start = 0x08
end = 0x77
while True:
    display.show(Image.ARROW_W)
    if button_a.was_pressed():
        display.show(Image.MEH)
        print("Scanning I2C bus...")
        for i in range(start, end + 1):
            try:
                i2c.read(i, 1)
            except OSError:
                pass
            else:
                print("Found:  [%s]" % hex(i))
        print("Scanning complete")
        print("Magnetometer [0x0e] Accelerometer [0x1d]")
    sleep(10)