NittyGritty

Ic2_Scanner.ino

Jan 6th, 2019
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.54 KB | None | 0 0
  1. // i2c_scanner
  2.  //
  3.  // This program (or code that looks like it)
  4.  // can be found in many places.
  5.  // For example on the Arduino.cc forum.
  6.  // The original author is not know.
  7.  //
  8.  // This sketch tests the standard 7-bit addresses
  9.  // from 0 to 127. Devices with higher bit address
  10.  // might not be seen properly.
  11.  //
  12.  // Adapted to be as simple as possible by Arduino.cc user Krodal
  13.  //
  14.  // June 2012
  15.  // Using Arduino 1.0.1
  16.  //
  17.  
  18. #include <Wire.h>
  19.  
  20. void setup()
  21.  {
  22.    Wire.begin();
  23.  
  24.   Serial.begin(9600);
  25.    Serial.println("\nI2C Scanner");
  26.  }
  27.  
  28. void loop()
  29.  {
  30.    byte error, address;
  31.    int nDevices;
  32.  
  33.   Serial.println("Scanning...");
  34.  
  35.   nDevices = 0;
  36.    for(address = 0; address <= 127; address++ )
  37.   {
  38.      // The i2c_scanner uses the return value of
  39.      // the Write.endTransmisstion to see if
  40.      // a device did acknowledge to the address.
  41.      Wire.beginTransmission(address);
  42.      error = Wire.endTransmission();
  43.  
  44.     if (error == 0)
  45.      {
  46.        Serial.print("I2C device found at address 0x");
  47.        if (address<16)
  48.         Serial.print("0");
  49.        Serial.print(address,HEX);
  50.        Serial.println(" !");
  51.  
  52.       nDevices++;
  53.      }
  54.      else if (error==4)
  55.     {
  56.        Serial.print("Unknow error at address 0x");
  57.        if (address<16)
  58.         Serial.print("0");
  59.        Serial.println(address,HEX);
  60.      }
  61.    }
  62.    if (nDevices == 0)
  63.      Serial.println("No I2C devices found\n");
  64.    else
  65.      Serial.println("done\n");
  66.  
  67.   delay(8000);           // wait 8 seconds for next scan
  68.  }
Add Comment
Please, Sign In to add comment