Example of i2c scanner for RHOMBIO_L476DMW1K

This example is known to work well on the following platforms: RHOMBIO_L476DMW1K

The example code scans any I2C peripheral attached to the kit, whether it is any rhomb.io slave module attached to Deimos carrier board or any external peripheral wired to SDA, SCL and GND pins available on Deimos pin header H2.

The code also prints out the result, showing the address of any device found on the I2C bus.

This example code should give you an output similar to this:


Rhomb.io example code - I2C Scanner

Searching for I2C devices...

I2C device found at address 0x60 (0xC0 in 8-bit)

1 device. found


PS: RHOMBIO_L476DMW1K IoT kit comes with a ATECC608A crypto authentication memory on board, which is connected to the I2C bus. Its I2C slave address is 0x60, that's the device found by the example code when no other I2C device has been connected to the kit.

Committer:
galonso@rhomb.io
Date:
Tue Sep 10 15:06:32 2019 +0200
Revision:
0:dc5bc0efc11d
Child:
1:9281da0f55a2
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
galonso@rhomb.io 0:dc5bc0efc11d 1 #include "mbed.h"
galonso@rhomb.io 0:dc5bc0efc11d 2
galonso@rhomb.io 0:dc5bc0efc11d 3
galonso@rhomb.io 0:dc5bc0efc11d 4 I2C i2c(I2C_SDA, I2C_SCL);
galonso@rhomb.io 0:dc5bc0efc11d 5 DigitalOut led(LED1);
galonso@rhomb.io 0:dc5bc0efc11d 6
galonso@rhomb.io 0:dc5bc0efc11d 7
galonso@rhomb.io 0:dc5bc0efc11d 8 int main()
galonso@rhomb.io 0:dc5bc0efc11d 9 {
galonso@rhomb.io 0:dc5bc0efc11d 10 led = 1;
galonso@rhomb.io 0:dc5bc0efc11d 11
galonso@rhomb.io 0:dc5bc0efc11d 12 i2c.frequency(100000);
galonso@rhomb.io 0:dc5bc0efc11d 13 printf("Rhomb.io example code - I2C Scanner\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 14 printf("-----------------------------------\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 15
galonso@rhomb.io 0:dc5bc0efc11d 16 while (1) {
galonso@rhomb.io 0:dc5bc0efc11d 17 printf("Searching for I2C devices...\n\r");
galonso@rhomb.io 0:dc5bc0efc11d 18
galonso@rhomb.io 0:dc5bc0efc11d 19 int count = 0;
galonso@rhomb.io 0:dc5bc0efc11d 20 for (int address = 0; address < 255; address +=2) { // check only for device's read addres
galonso@rhomb.io 0:dc5bc0efc11d 21 if (!i2c.write(address, NULL, 0)) { // 0 returned is ok
galonso@rhomb.io 0:dc5bc0efc11d 22 printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n\r", address >> 1, address); // the address is without LSB, which is R/W flag. shoft it right once
galonso@rhomb.io 0:dc5bc0efc11d 23 count++;
galonso@rhomb.io 0:dc5bc0efc11d 24 led = 1;
galonso@rhomb.io 0:dc5bc0efc11d 25 }
galonso@rhomb.io 0:dc5bc0efc11d 26 wait(0.02);
galonso@rhomb.io 0:dc5bc0efc11d 27 }
galonso@rhomb.io 0:dc5bc0efc11d 28 if (count)
galonso@rhomb.io 0:dc5bc0efc11d 29 printf("%d", count);
galonso@rhomb.io 0:dc5bc0efc11d 30 else
galonso@rhomb.io 0:dc5bc0efc11d 31 printf("No");
galonso@rhomb.io 0:dc5bc0efc11d 32 printf(" device%c found\n\r\n", count == 1?'\0':'s');
galonso@rhomb.io 0:dc5bc0efc11d 33 led = 0;
galonso@rhomb.io 0:dc5bc0efc11d 34 wait (1.8);
galonso@rhomb.io 0:dc5bc0efc11d 35 }
galonso@rhomb.io 0:dc5bc0efc11d 36 }