Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

81 lines
2.1KB

  1. // --------------------------------------
  2. // i2c_scanner
  3. // http://playground.arduino.cc/Main/I2cScanner
  4. //
  5. // Version 1
  6. // This program (or code that looks like it)
  7. // can be found in many places.
  8. // For example on the Arduino.cc forum.
  9. // The original author is not know.
  10. // Version 2, Juni 2012, Using Arduino 1.0.1
  11. // Adapted to be as simple as possible by Arduino.cc user Krodal
  12. // Version 3, Feb 26 2013
  13. // V3 by louarnold
  14. // Version 4, March 3, 2013, Using Arduino 1.0.3
  15. // by Arduino.cc user Krodal.
  16. // Changes by louarnold removed.
  17. // Scanning addresses changed from 0...127 to 1...119,
  18. // according to the i2c scanner by Nick Gammon
  19. // http://www.gammon.com.au/forum/?id=10896
  20. // Version 5, March 28, 2013
  21. // As version 4, but address scans now to 127.
  22. // A sensor seems to use address 120.
  23. // Version 6, November 27, 2015.
  24. // Added waiting for the Leonardo serial communication.
  25. //
  26. //
  27. // This sketch tests the standard 7-bit addresses
  28. // Devices with higher bit address might not be seen properly.
  29. //
  30. #include <Wire.h>
  31. void setup() {
  32. Wire.begin();
  33. Serial.begin(9600);
  34. while (!Serial); // Leonardo: wait for serial monitor
  35. Serial.println("\nI2C Scanner");
  36. }
  37. void loop() {
  38. byte error, address;
  39. int nDevices;
  40. Serial.println("Scanning...");
  41. nDevices = 0;
  42. for (address = 1; address < 127; address++) {
  43. // The i2c_scanner uses the return value of
  44. // the Write.endTransmisstion to see if
  45. // a device did acknowledge to the address.
  46. Wire.beginTransmission(address);
  47. error = Wire.endTransmission();
  48. if (error == 0) {
  49. Serial.print("Device found at address 0x");
  50. if (address < 16) {
  51. Serial.print("0");
  52. }
  53. Serial.print(address,HEX);
  54. Serial.println();
  55. nDevices++;
  56. } else if (error==4) {
  57. Serial.print("Unknown error at address 0x");
  58. if (address < 16) {
  59. Serial.print("0");
  60. }
  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(5000); // wait 5 seconds for next scan
  70. }