mpmcws

I2C scanner

Aug 31st, 2018
126
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //I2C scanner
  2. #include <Wire.h>
  3. void setup()
  4. {
  5.   Wire.begin();
  6.   Serial.begin(9600);
  7.   while (!Serial);             // Leonardo: wait for serial monitor
  8.   Serial.println("\nI2C Scanner");
  9. }
  10. void loop()
  11. {
  12.   byte error, address;
  13.   int nDevices;
  14.   Serial.println("Scanning...");
  15.   nDevices = 0;
  16.   for (address = 1; address < 127; address++ )
  17.   {
  18.     Wire.beginTransmission(address);
  19.     error = Wire.endTransmission();
  20.     if (error == 0)
  21.     {
  22.       Serial.print("I2C device found at address 0x");
  23.       if (address < 16)
  24.         Serial.print("0");
  25.       Serial.print(address, HEX);
  26.       Serial.println("  !");
  27.       nDevices++;
  28.     }
  29.     else if (error == 4)
  30.     {
  31.       Serial.print("Unknown error at address 0x");
  32.       if (address < 16)
  33.         Serial.print("0");
  34.       Serial.println(address, HEX);
  35.     }
  36.   }
  37.   if (nDevices == 0)
  38.     Serial.println("No I2C devices found\n");
  39.   else
  40.     Serial.println("done\n");
  41.   delay(5000);
  42.    }
RAW Paste Data