Advertisement
sspence65

ESP8266 I2C Scanner

Mar 31st, 2018
3,458
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Wire.h>
  2.  
  3.  
  4. void setup()
  5. {
  6. Wire.begin();
  7.  
  8. Serial.begin(9600);
  9. Serial.println("\nI2C Scanner");
  10. }
  11.  
  12.  
  13. void loop()
  14. {
  15. byte error, address;
  16. int nDevices;
  17.  
  18. Serial.println("Scanning...");
  19.  
  20. nDevices = 0;
  21. for(address = 1; address < 127; address++ )
  22. {
  23. Wire.beginTransmission(address);
  24. error = Wire.endTransmission();
  25.  
  26. if (error == 0)
  27. {
  28. Serial.print("I2C device found at address 0x");
  29. if (address<16)
  30. Serial.print("0");
  31. Serial.print(address,HEX);
  32. Serial.println(" !");
  33.  
  34. nDevices++;
  35. }
  36. else if (error==4)
  37. {
  38. Serial.print("Unknow error at address 0x");
  39. if (address<16)
  40. Serial.print("0");
  41. Serial.println(address,HEX);
  42. }
  43. }
  44. if (nDevices == 0)
  45. Serial.println("No I2C devices found\n");
  46. else
  47. Serial.println("done\n");
  48.  
  49. delay(2000);
  50. }
Advertisement
RAW Paste Data Copied
Advertisement