Advertisement
elektronek

Normální i2c scanne :-)

Feb 17th, 2021
1,047
0
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.   while (!Serial);             // Leonardo: wait for serial monitor
  10.   Serial.println("\nI2C Scanner");
  11. }
  12.  
  13.  
  14. void loop()
  15. {
  16.   byte error, address;
  17.   int nDevices;
  18.  
  19.   Serial.println("Scanning...");
  20.  
  21.   nDevices = 0;
  22.   for(address = 1; address < 127; address++ )
  23.   {
  24.     // The i2c_scanner uses the return value of
  25.     // the Write.endTransmisstion to see if
  26.     // a device did acknowledge to the address.
  27.     Wire.beginTransmission(address);
  28.     error = Wire.endTransmission();
  29.  
  30.     if (error == 0)
  31.     {
  32.       Serial.print("I2C device found at address 0x");
  33.       if (address<16)
  34.         Serial.print("0");
  35.       Serial.print(address,HEX);
  36.       Serial.println("  !");
  37.  
  38.       nDevices++;
  39.     }
  40.     else if (error==4)
  41.     {
  42.       Serial.print("Unknown error at address 0x");
  43.       if (address<16)
  44.         Serial.print("0");
  45.       Serial.println(address,HEX);
  46.     }    
  47.   }
  48.   if (nDevices == 0)
  49.     Serial.println("No I2C devices found\n");
  50.   else
  51.     Serial.println("done\n");
  52.  
  53.   delay(5000);           // wait 5 seconds for next scan
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement