Skip to content

Instantly share code, notes, and snippets.

@SweiLz
Last active June 4, 2020 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SweiLz/a97d290aa0f1ac40dffd5d7ef7faa3d7 to your computer and use it in GitHub Desktop.
Save SweiLz/a97d290aa0f1ac40dffd5d7ef7faa3d7 to your computer and use it in GitHub Desktop.
I2C address scanner for mbed os
#include <mbed.h>
I2C i2c(PB_11, PB_10); // SDA pin, SCL pin
Serial pc(PB_6, PB_7); // TX pin, RX pin
int main()
{
pc.baud(115200);
wait_ms(2000);
pc.printf("Hello World!\n");
int count = 0;
for (int address = 0; address < 127; address++)
{
if (!i2c.write(address << 1, NULL, 0)) // 0 returned is ok
{
pc.printf("I2C device found at address 0x%02X (0x%02X in 8-bit)\n", address, address << 1);
count++;
}
wait_ms(20);
}
if (count)
pc.printf("%d", count);
else
pc.printf("No");
pc.printf(" device found\n\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment