Advertisement
sunu

I2C scanner

Apr 1st, 2024 (edited)
724
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Arduino 2.29 KB | Software | 0 0
  1.  // --------------------------------------
  2. // i2c_scanner
  3. // https://playground.arduino.cc/Main/sourceblock_1/index.txt?action=sourceblock&num=1
  4. // https://playground.arduino.cc/Main/I2cScanner/
  5. //
  6. // Version 1
  7. //    This program (or code that looks like it)
  8. //    can be found in many places.
  9. //    For example on the Arduino.cc forum.
  10. //    The original author is not know.
  11. // Version 2, Juni 2012, Using Arduino 1.0.1
  12. //     Adapted to be as simple as possible by Arduino.cc user Krodal
  13. // Version 3, Feb 26  2013
  14. //    V3 by louarnold
  15. // Version 4, March 3, 2013, Using Arduino 1.0.3
  16. //    by Arduino.cc user Krodal.
  17. //    Changes by louarnold removed.
  18. //    Scanning addresses changed from 0...127 to 1...119,
  19. //    according to the i2c scanner by Nick Gammon
  20. //    https://www.gammon.com.au/forum/?id=10896
  21. // Version 5, March 28, 2013
  22. //    As version 4, but address scans now to 127.
  23. //    A sensor seems to use address 120.
  24. // Version 6, November 27, 2015.
  25. //    Added waiting for the Leonardo serial communication.
  26. //
  27. //
  28. // This sketch tests the standard 7-bit addresses
  29. // Devices with higher bit address might not be seen properly.
  30. //
  31.  
  32. #include <Wire.h>
  33.  
  34.  
  35. void setup()
  36. {
  37.   Wire.begin();
  38.  
  39.   Serial.begin(9600);
  40.   while (!Serial);             // Leonardo: wait for serial monitor
  41.   Serial.println("\nI2C Scanner");
  42. }
  43.  
  44.  
  45. void loop()
  46. {
  47.   byte error, address;
  48.   int nDevices;
  49.  
  50.   Serial.println("Scanning...");
  51.  
  52.   nDevices = 0;
  53.   for(address = 1; address < 127; address++ )
  54.   {
  55.     // The i2c_scanner uses the return value of
  56.     // the Write.endTransmisstion to see if
  57.     // a device did acknowledge to the address.
  58.     Wire.beginTransmission(address);
  59.     error = Wire.endTransmission();
  60.  
  61.     if (error == 0)
  62.     {
  63.       Serial.print("I2C device found at address 0x");
  64.       if (address<16)
  65.         Serial.print("0");
  66.       Serial.print(address,HEX);
  67.       Serial.println("  !");
  68.  
  69.       nDevices++;
  70.     }
  71.     else if (error==4)
  72.     {
  73.       Serial.print("Unknown error at address 0x");
  74.       if (address<16)
  75.         Serial.print("0");
  76.       Serial.println(address,HEX);
  77.     }    
  78.   }
  79.   if (nDevices == 0)
  80.     Serial.println("No I2C devices found\n");
  81.   else
  82.     Serial.println("done\n");
  83.  
  84.   delay(5000);           // wait 5 seconds for next scan
  85. }
  86.  
Tags: lcd i2c scanner
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement