elbear_arduino_bsp/libraries/Wire/examples/i2c_scanner/i2c_scanner.ino
khristolyubov e36b851783 ready to alpha
подготовка к альфа-тестированию
2024-08-19 22:44:04 +07:00

49 lines
926 B
C++

// i2c_scanner
#include <Wire.h>
void setup()
{
Serial.begin(115200);
while (!Serial);
Serial.println("I2C scanner start");
Wire.begin();
}
void loop()
{
int devicesQty = 0;
Serial.println("Scanning...");
for (byte address = 1; address < 127; address++)
{
// transmitting address to I2C bus
Wire.beginTransmission(address);
byte error = Wire.endTransmission();
// Wire.endTransmission() will return 0 only if an ACK is received
// from the subscriber with the current address
if (error == 0)
{
Serial.print("Device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
devicesQty++;
}
delayMicroseconds(1);
}
if (devicesQty == 0)
Serial.println("No device found");
else
{
Serial.print(devicesQty);
Serial.println(" devices found");
}
Serial.println();
delay(5000);
}