Scans the I2C bus and outputs the addresses that were found

Dependencies:   mbed

Revision:
0:c801d9233d33
Child:
1:1d28de04ff26
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Jun 27 13:36:38 2017 +0000
@@ -0,0 +1,26 @@
+#include "mbed.h"
+
+I2C i2c(I2C_SDA, I2C_SCL);
+Serial pc(SERIAL_TX, SERIAL_RX);
+
+int main()
+{
+    pc.baud(115200);
+    pc.printf("I2C scanner starting\n");
+
+    i2c.frequency(100e3); // default freq (100kHz)
+
+    const char *data = "1";
+
+    int ack;
+    unsigned char address;
+    for (address = 1; address < 127; address++) {
+        ack = i2c.write(address << 1, data, 1); // address is shifted to the left to leave room for the last R/W bit
+        if (ack == 0) {
+            pc.printf("Sent 0x%X; 0x%X ACK \r\n", address << 1, address); // Send command string
+        }
+        wait_ms(2);
+    }
+
+    pc.printf("I2C scanner done\n");
+}
\ No newline at end of file