Skip to content Skip to sidebar Skip to footer

Widget HTML #1

How to Get I2C Address Using Arduino I2C Scanner Code

How to Get I2C Address From I2C Devices Using Arduino I2C Scanner

How to get and read an I2C Address from an I2C Device Using an Arduino - In this tutorial, I will share how to get or find an I2C address from an I2C device, in which we don't know the address of the device.

In the Sparkfun I2C tutorial, how the I2C system works, you can read it here.

How to Get I2C Address Using Arduino I2C Scanner Code

Prepare I2C Device

There is a simple program that can read the I2C address from the device automatically. The program is called I2C Scanner.

So that this program can read the I2C address from an I2C device, the first step is to connect the Arduino to the device.

For example,  I use the RTC Module with I2C communicaion. In the current state, I don't know the address of the module, so I can't read anything on the RTC.

Now connect the Arduino and the I2C device using the following configuration wiring:

ARDUINO I2C DEVICE
5V VCC
GND GND
A4 SDA
A5 SCL

 

I2C Scanner Code

After that, use the I2C Scanner program code, before upload that code, this code using Version 6.

Version 1

This program (or code that looks like it) can be found in many places. For example on the Arduino.cc forum. Original author unknown.

Version 2, June 2012, Using Arduino 1.0.1

Adapted to be as simple as possible by Arduino.cc user Krodal

Version 3, 26 Feb 2013, by louarnold

Version 4, March 3, 2013, Using Arduino 1.0.3

By Arduino.cc user Krodal. Changes by louarnold are removed. Scan address changed from 0 ... 127 to 1 ... 119, according to an i2c scanner by Nick Gammon (https://www.gammon.com.au/forum/?id=10896)

Version 5, 28 March 2013

As of version 4, but the scan address is now to 127. The sensor seems to use address 120.

Version 6, 27 November 2015

Added waiting for Leonardo's serial communication. This sketch tests a standard 7-bit address. Devices with a higher bit address may not appear correctly.

Now, upload the following code to Arduino Uno. 

 
#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++ )
  {
   
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
 
    if (error == 0)
    {
      Serial.print("I2C Address is 0x");
      if (address<16)
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");
 
      nDevices++;
    }
    else if (error==4)
    {
      Serial.print("Unknown address at 0x");
      if (address<16)
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("Not found I2C Address \n");
  else
    Serial.println("Done\n");
 
  delay(5000);           // wait 5 seconds for next scan
}

Then open the serial monitor and you will see the I2C address in a Hexadecimal number.

In the experiments that I did with this RTC module, I got the result with the address is 0x68.

If you find this article is useful, please share it with your friends using the share button below.

Post a Comment for "How to Get I2C Address Using Arduino I2C Scanner Code"