microrobotics

Untitled

Oct 18th, 2021
867
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*I2C_scanner
  2.   This sketch tests standard 7-bit addresses.
  3.   Devices with higher bit address might not be seen properly.*/
  4.  
  5. #include <Wire.h>
  6. void setup() {
  7.   Wire.begin();
  8.   Serial.begin(9600);
  9.   while (!Serial);
  10.   Serial.println("\nI2C Scanner");
  11. }
  12. void loop() {
  13.   byte error, address;
  14.   int nDevices;
  15.   Serial.println("Scanning...");
  16.   nDevices = 0;
  17.   for (address = 1; address < 127; address++ ) {
  18.     Wire.beginTransmission(address);
  19.     error = Wire.endTransmission();
  20.     if (error == 0) {
  21.       Serial.print("I2C device found at address 0x");
  22.       if (address < 16)
  23.         Serial.print("0");
  24.       Serial.print(address, HEX);
  25.       Serial.println("  !");
  26.       nDevices++;
  27.     }
  28.     else if (error == 4) {
  29.       Serial.print("Unknown error at address 0x");
  30.       if (address < 16)
  31.         Serial.print("0");
  32.       Serial.println(address, HEX);
  33.     }
  34.   }
  35.   if (nDevices == 0)
  36.     Serial.println("No I2C devices found\n");
  37.   else
  38.     Serial.println("done\n");
  39.   delay(5000);
  40. }
  41.  
RAW Paste Data