Code Example - I2C Bus Scanner

Want to scan the I2C bus to see what devices respond? This app will do that.

`local i2c = hardware.i2c89;
i2c.configure(CLOCK_SPEED_100_KHZ);
local Address = 0;

function loop() {
if (Address == 0) server.log(“Starting I2C Scan”);
local e = i2c.write(Address, “\x00”); //Try writing some data
if (e != -2) { //Found something.
server.log(format(“Found something with address %3i (%#2x), result = %d”, Address, Address, e));
}
Address+=2; //Increment by 2.
if (Address == 256) Address = 0; //Start over
imp.wakeup(0.1, loop);
}

imp.configure(“I2C-Scanner”, [], []);
loop();`

You can monitor the output in the log window at the bottom of the planner. Output will look like this:

Starting I2C Scan Found something with address 48 (0x30), result = -4 Found something with address 50 (0x32), result = -4 Found something with address 192 (0xc0), result = 0