How to Scan I2C Address in Arduino

How to Scan I2C Address in Arduino

You will be able to know the I2C address of your module that's connected to SDA & SCL Arduino by using I2C scanner code.

  • 14,195 views
  • 8 comments
  • 3 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
You can use any version of Arduino, but i'll use Arduino UNO R3
×1
Sen0140 6w4a7240 450x300
DFRobot 10 DOF Mems IMU Sensor
You can use any I2C Device but here i am using GY-87 Module, because it has a lot of I2C Devices inside this.
×1
11026 02
Jumper wires (generic)
10cm or longer Female to Male Jumper Wires
×4

Apps and online services

Ide web
Arduino IDE
You can download this app in https://www.arduino.cc/en/main/software

About this project

When you are going to use I2C modules for your Arduino Projects, sometimes the manufacturer didn't give the address of I2C device inside the chip. It can be a serious problem if the address is unknown or you write the wrong address.

This method is used for scanning the I2C Device inside your module that connected to I2C bus in Arduino (SDA, SCL). In Arduino Uno / Nano / Pro Mini I2C bus is connected to A4 (SDA) and A5 (SCL), for Arduino Mega 2560 I2C bus is connected to D20 (SDA) and D21 (SCL)

Here I am going to show you how to scan I2C address with a simple code.

Code

I2C Scanner Arduino CodeC/C++
Write your code in Arduino IDE Software, save it. Select the port (i.e COM3) and choose your Arduino board type (i.e Arduino Uno / Genuino) then compile it to your Arduino.
/ ---------------------------------------------------------------- /
// Arduino I2C Scanner
// Re-writed by Arbi Abdul Jabbaar
// Using Arduino IDE 1.8.7
// Using GY-87 module for the target
// Tested on 10 September 2019
// This sketch tests the standard 7-bit addresses
// Devices with higher bit address might not be seen properly.
/ ---------------------------------------------------------------- /

#include <Wire.h> //include Wire.h library

void setup()
{
  Wire.begin(); // Wire communication begin
  Serial.begin(9600); // The baudrate of Serial monitor is set in 9600
  while (!Serial); // Waiting for Serial Monitor
  Serial.println("\nI2C Scanner");
}

void loop()
{
  byte error, address; //variable for error and I2C 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("Unknown 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 the next I2C scan
}

Schematics

Wiring diagram of Arduino and GY-87 Module
Uploads2ftmp2fff4add10 0ae0 4aca 8b0c e73df08763072funtitledsketch2 bb 1ulyw0u3b3

Comments

Similar projects you might like

Master Slave I2C Connection

by PIYUSH_K_SINGH

  • 11,688 views
  • 0 comments
  • 11 respects

2€ OLED I2C Analog Pin Dynamic Plotting

Project showcase by javier muñoz sáez

  • 8,309 views
  • 0 comments
  • 16 respects

Temperature Monitor with DHT22 and I2C 16x2 LCD

Project tutorial by adrakhmat

  • 9,593 views
  • 5 comments
  • 27 respects
Add projectSign up / Login