Advertisement
Guest User

I2C Scanner http://darylrobotproject.wordpress.com

a guest
Oct 12th, 2012
455
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 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.  
  21. void setup()
  22.  {
  23.    Wire.begin();
  24.  
  25.   Serial.begin(9600);
  26.    Serial.println("\nI2C Scanner");
  27.  }
  28.  
  29.  
  30. void loop()
  31.  {
  32.    byte error, address;
  33.    int nDevices;
  34.  
  35.   Serial.println("Scanning...");
  36.  
  37.   nDevices = 0;
  38.    for(address = 0; address <= 127; address++ )
  39.   {
  40.      // The i2c_scanner uses the return value of
  41.      // the Write.endTransmisstion to see if
  42.      // a device did acknowledge to the address.
  43.      Wire.beginTransmission(address);
  44.      error = Wire.endTransmission();
  45.  
  46.     if (error == 0)
  47.      {
  48.        Serial.print("I2C device found at address 0x");
  49.        if (address<16)
  50.         Serial.print("0");
  51.        Serial.print(address,HEX);
  52.        Serial.println(" !");
  53.  
  54.       nDevices++;
  55.      }
  56.      else if (error==4)
  57.     {
  58.        Serial.print("Unknow error at address 0x");
  59.        if (address<16)
  60.         Serial.print("0");
  61.        Serial.println(address,HEX);
  62.      }    
  63.    }
  64.    if (nDevices == 0)
  65.      Serial.println("No I2C devices found\n");
  66.    else
  67.      Serial.println("done\n");
  68.  
  69.   delay(8000);           // wait 8 seconds for next scan
  70.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement