I2C scanner for Arduino

The following code is an I2C scanner for Arduino. It requests data from each address within the range [0:255]. I2C slaves that answer are display on the terminal.

#include 


void setup() { 
 //Initialize serial and wait for port to open:
  Serial.begin(115200); 
  
  // prints title with ending line break 
  Serial.println("I2C scanner ::: www.lucidarme.me"); 
} 


void loop() 
{
  // Test each I2C address 
  for (int i=0;i<256;i++)
  {
    // Request data (read one byte)
    Wire.requestFrom(i, 1);

    // If slave answers, a device is found
    if (Wire.available())
    {
      // Display address
      Serial.print(i,DEC);
      Serial.print("\t0x");
      Serial.print(i,HEX);
      Serial.print("\t");    
      Serial.print("0b");
      Serial.print(i,BIN);
      Serial.print("\t");
      Serial.println("Device found");
    }
    
//    delay(100);
  }
  Serial.println("End of scan"); 
  while (1);
} 

Leave a Reply

Your email address will not be published. Required fields are marked *