AdSense

Wednesday, 23 April 2014

Arduino - I2C scanner

(Deutsche Version) Today, I want to present a simple I2C scanner. The basic functionality is simple: A counter starts at 0 and tries to reach an I2C device. If this fails, the counter increases and checks at the next address. The source code is the following:

for (int i = 0; i < 120; i++)
{
  Wire.beginTransmission(i);
  error = Wire.endTransmission();
  sprintf(buf, "scan %i - %i",i,error);
  lcd.setCursor(0,1);
  lcd.print(buf);
  if (error == 0)
  {
    //success!
    delay(1000);
  }
}

Instead of lcd.print, you can use any other kind of output. As soon as a device is found, the Arduino waits a second and then continues scanning.