I2C Bus Scan

I2C and SPI are protocols that can link the microbit to other devices, such as an LED matrix or an accelerometer.

There is more about connecting I2C devices to you microbit in this how to article.

There are devices already on the I2c bus: the microbit’s onboard accelerometer and magnetometer (or compass).

The I2C bus is exposed on the microbit’s pin19 & pin20. Additional devices can be attached to the bus with these pins.

Connecting components in this way results in fewer wires for communication: SPI uses three wires, I2C requires two.

As mentioned, there are already two devices connected to the microbit on the I2C bus:

DeviceI2C Address
Accelerometer0x1D
Magnetometer (compass)0x0E

Each component added to pin19 & pin20 requires an address. Each device has an address ‘hard coded’ into the component. These are usually these are written in the datasheet of the I2C device. Often the address of the device can be changed with jumpers or soldering contact points.

Scan the Bus

It is also possible to scan the I2C bus for the device if you cannot find it in the datasheet. This program will return a list of the connected devices when button_a is pressed. You’ll need to be connected over serial to the microbit to read it.

It’s based on micropython’s .scan method.

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)
    

See also

Share On
Facebook Twitter Google Plus

Published: December 23, 2016 Updated: December 23, 2016

I2C Bus Scan

Scan the I2C bus for slave devices.

Jez Dean
Teacher