i2c 设备地址查找 arduino

#include 
void setup() {
  // put your setup code here, to run once:
Wire.begin();
Serial.begin(9600);
Serial.println("nI2C Scanner");
}

void loop() {
  // put your main code here, to run repeatedly:
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for(address = 1; address <127; address++ )
{
  
  Wire.beginTransmission(address);
  error = Wire.endTransmission();
  if(error ==0)
  {
    Serial.print("i2c device found at addresss 0x");
    Serial.print(address,HEX);
    nDevices++;
  }
  else if (error ==4 )
  {
    Serial.print("Unknow error at address 0x");
    Serial.print(address,HEX);
  }
}
if (nDevices ==0)
Serial.println("No I2C devices foundn");
delay(5000);
}
i2c.JPG

你可能感兴趣的:(i2c 设备地址查找 arduino)