safwan092

Untitled

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