Guest User

Untitled

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