Advertisement
Guest User

Untitled

a guest
Nov 9th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include <Wire.h>
  2.  
  3. void setup() {
  4.   Serial.begin(115200);
  5.   Wire.begin();
  6.   Serial.println("I2C Scanner - Searching for devices...");
  7.  
  8.   byte error, address;
  9.   int devicesFound = 0;
  10.  
  11.   for (address = 1; address < 127; address++) {
  12.     Wire.beginTransmission(address);
  13.     error = Wire.endTransmission();
  14.  
  15.     if (error == 0) {
  16.       Serial.print("I2C device found at address 0x");
  17.       if (address < 16)
  18.         Serial.print("0");
  19.       Serial.print(address, HEX);
  20.       Serial.println(" !");
  21.       devicesFound++;
  22.     } else if (error == 4) {
  23.       Serial.print("Unknown error at address 0x");
  24.       if (address < 16)
  25.         Serial.print("0");
  26.       Serial.println(address, HEX);
  27.     }
  28.   }
  29.   if (devicesFound == 0)
  30.     Serial.println("No I2C devices found.");
  31.   else
  32.     Serial.println("I2C scan complete.");
  33. }
  34.  
  35. void loop() {
  36.   // Nothing in loop for this scanner
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement