Advertisement
Dotterbart

Scan I2C Bus

Jan 21st, 2024
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.93 KB | None | 0 0
  1. void I2C_Adresses(){
  2.   byte error, address;
  3.   int nDevices;
  4.   Serial.println("Scanning...");
  5.   nDevices = 0;
  6.   for(address = 1; address < 127; address++ ){
  7.     // The i2c_scanner uses the return value of
  8.     // the Write.endTransmisstion to see if
  9.     // a device did acknowledge to the address.
  10.     Wire.beginTransmission(address);
  11.     error = Wire.endTransmission();
  12.     if (error == 0){
  13.       Serial.print("I2C device found at address 0x");
  14.       if (address<16){
  15.         Serial.print("0");
  16.       }
  17.         Serial.print(address,HEX);
  18.         Serial.println("  !");
  19.         nDevices++;
  20.     }
  21.     else if (error==4){
  22.       Serial.print("Unknown error at address 0x");
  23.       if (address<16){
  24.         Serial.print("0");
  25.       }
  26.       Serial.println(address,HEX);
  27.     }    
  28.   }  
  29.   if (nDevices == 0){
  30.     Serial.print(address,HEX);
  31.     Serial.println(" No I2C devices found\n");}
  32.   else{
  33.     Serial.println("done\\");}
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement