I2C Address Finder

Got a device connected to your Arduino and want to confirm what the address is? This code snippet (written for an Uno) gives you the I2C addresses that respond with an ACK on your I2C bus. I can't remember how I ended up with this code. I might have copied key portions from someone but I can't remember. If I did, thanks :slight_smile:

/*
  I2C Address Finder
  Runs through I2C addresses once from 0x00 to 0x7F looking for an acknowledge
  Lists all addresses that were found on the bus
*/

#include <Wire.h>

void setup()
{
  // PIN SETUP
  pinMode(A4, INPUT);      // define as input for now, will get redifined by I2C functions
  pinMode(A5, INPUT);      // define as input for now, will get redifined by I2C functions
   
  Serial.begin(115200); 
  printTimestamp();
  Serial.println("I2C Address Finder Running");
  
  Wire.begin();
  
  byte endTXreturnedValue, loopCounter = 0;
  
  for(loopCounter=0; loopCounter<=127; loopCounter++)
  {
    printTimestamp();
    Serial.print("Starting I2C write to address ");
    Serial.println(loopCounter, HEX);

    Wire.beginTransmission(loopCounter);

    printTimestamp();

    endTXreturnedValue = Wire.endTransmission();
    
    if (endTXreturnedValue == 0)
    {
      Serial.print("Correct address is = ");
      Serial.println(loopCounter, HEX);
    }
    
  }
    
}

void loop()
{
  
  // nothing in here, runs only once
    
}

void printTimestamp (void)
{
  Serial.print("[");
  Serial.print(millis());
  Serial.print("]");
}

Does it work well?

I'm familiar with the I2C scanner of Nick Gammon, see his great I2C pages - Gammon Forum : Electronics : Microprocessors : I2C - Two-Wire Peripheral Interface - for Arduino -
and the one from Tod Kurt - http://todbot.com/arduino/sketches/I2CScanner/I2CScanner.pde -
(Note the latter only checks up till device address 100, but in essence the same.

Next step could be to recognize type of devices by checking well known devices (e.g. ds1307, eeprom etc) when an address is recognized. NOt trivial but it would be a great improvement!