C 48
I2c scanner Guest on 26th March 2018 02:33:29 AM
  1. #include <Arduino.h>
  2. #include <Wire.h>
  3.  
  4. void setup() {
  5.   Serial.begin(115200);
  6.   Wire.begin();
  7. }
  8.  
  9. void loop() {
  10.   byte error, address;
  11.   int nDevices;
  12.  
  13.   Serial.println("Scanning...");
  14.  
  15.   nDevices = 0;
  16.   for (address = 1; address < 127; address++) {
  17.     // The i2c_scanner uses the return value of
  18.     // the Write.endTransmisstion to see if
  19.     // a device did acknowledge to the address.
  20.     Wire.beginTransmission(address);
  21.     error = Wire.endTransmission();
  22.  
  23.     if (error == 0) {
  24.       Serial.print("I2C device found at address 0x");
  25.       if (address < 16)
  26.         Serial.print("0");
  27.       Serial.print(address, HEX);
  28.       Serial.println("  !");
  29.  
  30.       nDevices++;
  31.     } else if (error == 4) {
  32.       Serial.print("Unknown error at address 0x");
  33.       if (address < 16)
  34.         Serial.print("0");
  35.       Serial.println(address, HEX);
  36.     }
  37.   }
  38.   if (nDevices == 0)
  39.     Serial.println("No I2C devices found\n");
  40.   else
  41.     Serial.println("done\n");
  42.  
  43.   delay(5000); // wait 5 seconds for next scan
  44. }

Paste is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.