Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1.  
  2.  
  3. #include <Wire.h>
  4.  
  5.  
  6.  
  7. void setup()
  8. {
  9.  
  10. Wire.begin(); //SDA=4, SCL=5
  11. Wire.setClock(100000);
  12. Serial.begin(9600);
  13. Serial.println("\nI2C Scanner");
  14. }
  15.  
  16.  
  17. void loop()
  18. {
  19. byte error, address;
  20. int nDevices;
  21.  
  22. Serial.println("Scanning...");
  23.  
  24. nDevices = 0;
  25. for(address = 1; address < 127; address++ )
  26. {
  27.  
  28. Wire.beginTransmission(address);
  29. error = Wire.endTransmission();
  30.  
  31. if (error == 0)
  32. {
  33. Serial.print("I2C device found at address 0x");
  34. if (address<16)
  35. Serial.print("0");
  36. Serial.print(address,HEX);
  37. Serial.println(" !");
  38.  
  39. nDevices++;
  40. }
  41. else if (error==4)
  42. {
  43. Serial.print("Unknow error at address 0x");
  44. if (address<16)
  45. Serial.print("0");
  46. Serial.println(address,HEX);
  47. }
  48. }
  49. if (nDevices == 0)
  50. Serial.println("No I2C devices found\n");
  51. else
  52. Serial.println("done\n");
  53.  
  54. delay(5000); // wait 5 seconds for next scan
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement