Skip to main content

Knowledgebase

Search knowledgebase

Hints & Tips
How to scan an I2C bus for devices

The following code snippet provides a very simple way of determining the addresses of devices connected to any one of an imp’s I²C buses. The code here assumes the bus in use is the imp001’s hardware.i2c89, but can easily be changed to, for example, hardware.i2cAB or hardware.i2cFG on the imp003, or one of the imp004m’s or imp005’s I²C buses:

i2c <- hardware.i2c89;   // Set to desired I2C bus
i2c.configure(CLOCK_SPEED_100_KHZ);

for (local i = 2 ; i < 256 ; i+=2) {
  if (i2c.read(i, "", 1) != null) {
    server.log(format("Device at address: 0x%02X", i));
  }
}