Skip to main content

How to find out the address of your i2c device using Arduino IDE



This video shows how to determine the address of your i2c device using your Arduino and the Arduino IDE.

Below is the code referenced in the video:
---------------------------------------------------

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  while (!Serial);             
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}

Comments

  1. Thank you for this code,it helped me in getting the address of my I2C.

    ReplyDelete
  2. Thank you very much. Your video are really great. I was able to get the code off of 1 of my 12c 0_27 the other just one just "12C Scanner Scanning never gave address just blank could be display I did solder 4 wire pc board to it but check the continuity on each pin. On the one that gave me address I have tried several different liquid crystal display programs never display anything on a robot smart car also tried what you did in video same results. I will pursuing the issue I am very new to this thanks for help

    ReplyDelete

Post a Comment